diff --git a/src/main/php/PHPMD/Rule/CleanCode/IfStatementAssignment.php b/src/main/php/PHPMD/Rule/CleanCode/IfStatementAssignment.php index a5246b43f..910ecac84 100644 --- a/src/main/php/PHPMD/Rule/CleanCode/IfStatementAssignment.php +++ b/src/main/php/PHPMD/Rule/CleanCode/IfStatementAssignment.php @@ -40,6 +40,14 @@ */ class IfStatementAssignment extends AbstractRule implements MethodAware, FunctionAware { + /** + * @var array List of statement types where to forbid assignation. + */ + protected $ifStatements = array( + 'IfStatement', + 'ElseIfStatement', + ); + /** * This method checks if method/function has if clauses * that use assignment instead of comparison. @@ -64,10 +72,9 @@ public function apply(AbstractNode $node) */ private function getStatements(AbstractNode $node) { - $ifStatements = $node->findChildrenOfType('IfStatement'); - $elseIfStatements = $node->findChildrenOfType('ElseIfStatement'); - - return array_merge($ifStatements, $elseIfStatements); + return call_user_func_array('array_merge', array_map(function ($type) use ($node) { + return $node->findChildrenOfType($type); + }, $this->ifStatements)); } /** @@ -79,9 +86,12 @@ private function getStatements(AbstractNode $node) private function getExpressions(array $statements) { $expressions = array(); + /** @var ASTNode $statement */ foreach ($statements as $statement) { - $expressions = array_merge($expressions, $statement->findChildrenOfType('Expression')); + $expressions = array_merge($expressions, array_filter($statement->findChildrenOfType('Expression'), function (ASTNode $node) { + return !count($node->getParent()->findChildrenOfType('FunctionPostfix')); + })); } return $expressions; diff --git a/src/test/php/PHPMD/Rule/CleanCode/IfStatementAssignmentTest.php b/src/test/php/PHPMD/Rule/CleanCode/IfStatementAssignmentTest.php index a3eb3d9e0..69bf12414 100644 --- a/src/test/php/PHPMD/Rule/CleanCode/IfStatementAssignmentTest.php +++ b/src/test/php/PHPMD/Rule/CleanCode/IfStatementAssignmentTest.php @@ -21,6 +21,13 @@ class IfStatementAssignmentTest extends AbstractTest { + public function testRuleNotAppliesInsideClosure() + { + $rule = new IfStatementAssignment(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getMethod()); + } + public function testRuleNotAppliesToIfsWithoutAssignment() { $rule = new IfStatementAssignment(); diff --git a/src/test/resources/files/Rule/CleanCode/IfStatementAssignment/testRuleNotAppliesInsideClosure.php b/src/test/resources/files/Rule/CleanCode/IfStatementAssignment/testRuleNotAppliesInsideClosure.php new file mode 100644 index 000000000..6a69314fc --- /dev/null +++ b/src/test/resources/files/Rule/CleanCode/IfStatementAssignment/testRuleNotAppliesInsideClosure.php @@ -0,0 +1,31 @@ +. + * All rights reserved. + * + * Licensed under BSD License + * For full copyright and license information, please see the LICENSE file. + * Redistributions of files must retain the above copyright notice. + * + * @author Manuel Pichler + * @copyright Manuel Pichler. All rights reserved. + * @license https://opensource.org/licenses/bsd-license.php BSD License + * @link http://phpmd.org/ + */ + +namespace PHPMDTest; + +class Foo +{ + public function testRuleNotAppliesInsideClosure() + { + if (true) { + array_filter(array(), function ($foo) { + $bar = $foo; + echo $bar; + }); + } + } +}