ovr/phpsa

[Analyzer] Avoid yoda conditions

ddmler opened this issue · 1 comments

Simple Task: Statement Analyzer for conditions, detects:

if (true == $someParameter) {
}

How to do it:

  1. Create Analyzer (you can use this one as a template: https://github.com/ovr/phpsa/blob/master/src/Analyzer/Pass/Statement/AssignmentInCondition.php )
    and register it to the same things. Now:
    Check that $condition instanceof Expr\BinaryOp\ {Equal, NotEqual, Identical, NotIdentical}

If yes check that:
$condition->left->isScalar()
$condition->right instanceof Expr\Variable
If yes write a nice notice message, else return false.

  1. Add it to PHPSA\Analyzer\Factory (registerStatementPasses)
  2. Write Tests (analyze-fixtures directory): One example with yoda and one without (again look at the existing ones for statements)
  3. Write a chapter in docs/05_Analyzers.md (its sorted alphabetically)

Check our Contribution Guide and create a pull request.
Thanks 🍰

closed - merged in 230