Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jrbasso/fix-unusedlocalvar-compact
Fix UnusedLocalVariable to recognize compact function.
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,13 @@ private function collectVariables(PHP_PMD_Node_AbstractCallable $node) | |
foreach ($node->findChildrenOfType('VariableDeclarator') as $variable) { | ||
$this->collectVariable($variable); | ||
} | ||
foreach ($node->findChildrenOfType('FunctionPostfix') as $func) { | ||
if ($func->getImage() === 'compact') { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rakeshtembhurne
|
||
foreach ($func->findChildrenOfType('Literal') as $literal) { | ||
$this->_collectLiteral($literal); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -157,6 +164,22 @@ private function collectVariable(PHP_PMD_Node_ASTNode $node) | |
$this->images[$node->getImage()][] = $node; | ||
} | ||
|
||
/** | ||
* Stores the given literal node in an internal list of found variables. | ||
* | ||
* @param PHP_PMD_Node_ASTNode $node The context variable node. | ||
* | ||
* @return void | ||
*/ | ||
private function _collectLiteral(PHP_PMD_Node_ASTNode $node) | ||
{ | ||
$variable = '$' . trim($node->getImage(), '\''); | ||
if (!isset($this->_images[$variable])) { | ||
$this->_images[$variable] = array(); | ||
} | ||
$this->_images[$variable][] = $node; | ||
} | ||
|
||
/** | ||
* Template method that performs the real node image check. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/test/resources/files/Rule/UnusedLocalVariable/testRuleDoesNotApplyToCompactFunction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
class testRuleDoesNotApplyToCompactFunction | ||
{ | ||
public function testRuleDoesNotApplyToCompactFunction() | ||
{ | ||
$key = 'ok'; | ||
return compact('key'); | ||
} | ||
} | ||
?> |
Here is a little bug. In Symfony 2.1 $func->getImage() returns a string like "Acme/SomethingBundle/compact". So to make it works I suggest to use:
if(strpos($func->getImage(),'compact'))
I will make a pull request for it.