Skip to content

Commit

Permalink
Merge pull request #668 from kylekatarnls/feature/fix-661-assignation…
Browse files Browse the repository at this point in the history
…s-in-closures

Fix #661 Exclude FunctionPostfix from IfStatementAssignment
  • Loading branch information
tvbeek committed Sep 3, 2019
2 parents d365af4 + ecd3015 commit 02b22a2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/php/PHPMD/Rule/CleanCode/IfStatementAssignment.php
Expand Up @@ -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.
Expand All @@ -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));
}

/**
Expand All @@ -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;
Expand Down
Expand Up @@ -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();
Expand Down
@@ -0,0 +1,31 @@
<?php
/**
* This file is part of PHP Mess Detector.
*
* Copyright (c) Manuel Pichler <mapi@phpmd.org>.
* 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 <mapi@phpmd.org>
* @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;
});
}
}
}

0 comments on commit 02b22a2

Please sign in to comment.