Skip to content

Commit

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

Fix #661 Scan only the statement inner expression (first one)
  • Loading branch information
tvbeek committed Sep 5, 2019
2 parents 02b22a2 + d458392 commit b9eaa0f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/main/php/PHPMD/Rule/CleanCode/IfStatementAssignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use PDepend\Source\AST\ASTAssignmentExpression;
use PDepend\Source\AST\ASTExpression;
use PDepend\Source\AST\ASTStatement;
use PHPMD\AbstractNode;
use PHPMD\AbstractRule;
use PHPMD\Node\ASTNode;
Expand Down Expand Up @@ -68,7 +67,7 @@ public function apply(AbstractNode $node)
* Extracts if and elseif statements from method/function body
*
* @param AbstractNode $node An instance of MethodNode or FunctionNode class
* @return ASTStatement[]
* @return ASTNode[]
*/
private function getStatements(AbstractNode $node)
{
Expand All @@ -80,21 +79,14 @@ private function getStatements(AbstractNode $node)
/**
* Extracts all expression from statements array
*
* @param ASTStatement[] $statements Array of if and elseif clauses
* @param ASTNode[] $statements Array of if and elseif clauses
* @return ASTExpression[]
*/
private function getExpressions(array $statements)
{
$expressions = array();

/** @var ASTNode $statement */
foreach ($statements as $statement) {
$expressions = array_merge($expressions, array_filter($statement->findChildrenOfType('Expression'), function (ASTNode $node) {
return !count($node->getParent()->findChildrenOfType('FunctionPostfix'));
}));
}

return $expressions;
return array_map(function (ASTNode $statement) {
return $statement->getFirstChildOfType('Expression');
}, $statements);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public function testRuleNotAppliesInsideClosure()
$rule->apply($this->getMethod());
}

public function testRuleNotAppliesInsideClosureCallbacks()
{
$rule = new IfStatementAssignment();
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}

public function testRuleNotAppliesToIfsWithoutAssignment()
{
$rule = new IfStatementAssignment();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 testRuleNotAppliesInsideClosureCallbacks($ids, $allocation)
{
if ('foo' === 'foo') {
$ids->each(function () use ($allocation) {
$allocation->status = 'paid';
$allocation->potato = 'round';
});

return true;
}
}
}

0 comments on commit b9eaa0f

Please sign in to comment.