diff --git a/src/main/php/PHPMD/Rule/AbstractLocalVariable.php b/src/main/php/PHPMD/Rule/AbstractLocalVariable.php index c0f19377a..9045543bd 100644 --- a/src/main/php/PHPMD/Rule/AbstractLocalVariable.php +++ b/src/main/php/PHPMD/Rule/AbstractLocalVariable.php @@ -144,7 +144,7 @@ protected function stripWrappedIndexExpression(ASTNode $node) if (false === $this->isWrappedByIndexExpression($node)) { return $node; } - + $parent = $node->getParent(); if ($parent->getChild(0)->getNode() === $node->getNode()) { return $this->stripWrappedIndexExpression($parent); @@ -177,4 +177,19 @@ protected function isFunctionNameEqual(AbstractNode $node, $name) { return (0 === strcasecmp(trim($node->getImage(), '\\'), $name)); } + + /** + * AST puts namespace prefix to global functions called from a namespace. + * This method checks if the last part of function fully qualified name is equal to $name + * + * @param \PHPMD\AbstractNode $node + * @param string $name + * @return boolean + */ + protected function isFunctionNameEndingWith(AbstractNode $node, $name) + { + $parts = explode('\\', trim($node->getImage(), '\\')); + + return (0 === strcasecmp(array_pop($parts), $name)); + } } diff --git a/src/main/php/PHPMD/Rule/UnusedFormalParameter.php b/src/main/php/PHPMD/Rule/UnusedFormalParameter.php index 4cd1a8db7..38a2b4cc3 100644 --- a/src/main/php/PHPMD/Rule/UnusedFormalParameter.php +++ b/src/main/php/PHPMD/Rule/UnusedFormalParameter.php @@ -109,7 +109,7 @@ private function isAbstractMethod(AbstractNode $node) } return false; } - + /** * Returns true when the given node is method with signature declared as inherited using * {@inheritdoc} annotation. @@ -185,7 +185,8 @@ private function removeUsedParameters(AbstractNode $node) if ($this->isFunctionNameEqual($functionCall, 'func_get_args')) { $this->nodes = array(); } - if ($this->isFunctionNameEqual($functionCall, 'compact')) { + + if ($this->isFunctionNameEndingWith($functionCall, 'compact')) { foreach ($functionCall->findChildrenOfType('Literal') as $literal) { unset($this->nodes['$' . trim($literal->getImage(), '"\'')]); } diff --git a/src/main/php/PHPMD/Rule/UnusedLocalVariable.php b/src/main/php/PHPMD/Rule/UnusedLocalVariable.php index 6a4533afc..ecebd3f02 100644 --- a/src/main/php/PHPMD/Rule/UnusedLocalVariable.php +++ b/src/main/php/PHPMD/Rule/UnusedLocalVariable.php @@ -128,7 +128,7 @@ private function collectVariables(AbstractCallableNode $node) $this->collectVariable($variable); } foreach ($node->findChildrenOfType('FunctionPostfix') as $func) { - if ($this->isFunctionNameEqual($func, 'compact')) { + if ($this->isFunctionNameEndingWith($func, 'compact')) { foreach ($func->findChildrenOfType('Literal') as $literal) { $this->collectLiteral($literal); } diff --git a/src/test/php/PHPMD/Rule/UnusedFormalParameterTest.php b/src/test/php/PHPMD/Rule/UnusedFormalParameterTest.php index 7e30406a5..4b7405064 100644 --- a/src/test/php/PHPMD/Rule/UnusedFormalParameterTest.php +++ b/src/test/php/PHPMD/Rule/UnusedFormalParameterTest.php @@ -420,4 +420,40 @@ public function test_compact_function_rule_works_case_insensitive() $rule->setReport($this->getReportMock(0)); $rule->apply($this->getMethod()); } + + /** + * @test + * @return void + * @since 2.0.1 + */ + public function test_namespaced_compact_function_rule_does_not_apply() + { + $rule = new UnusedFormalParameter(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getMethod()); + } + + /** + * @test + * @return void + * @since 2.0.1 + */ + public function test_namespaced_compact_function_rule_only_applies_to_used_parameters() + { + $rule = new UnusedFormalParameter(); + $rule->setReport($this->getReportMock(2)); + $rule->apply($this->getMethod()); + } + + /** + * @test + * @return void + * @since 2.0.1 + */ + public function test_namespaced_compact_function_rule_works_case_insensitive() + { + $rule = new UnusedFormalParameter(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getMethod()); + } } diff --git a/src/test/php/PHPMD/Rule/UnusedLocalVariableTest.php b/src/test/php/PHPMD/Rule/UnusedLocalVariableTest.php index 25a25a0f3..e0fb6160f 100644 --- a/src/test/php/PHPMD/Rule/UnusedLocalVariableTest.php +++ b/src/test/php/PHPMD/Rule/UnusedLocalVariableTest.php @@ -512,4 +512,39 @@ public function test_compact_function_rule_works_case_insensitive() $rule->setReport($this->getReportMock(0)); $rule->apply($this->getMethod()); } + + /** + * testRuleDoesNotApplyToNamespacedCompactFunction + * + * + * namespace Baz; + * + * class Foo { + * public function bar() { + * $key = 'ok'; + * return compact('key'); + * } + * } + * + * + * @return void + */ + public function testRuleDoesNotApplyToNamespacedCompactFunction() + { + $rule = new UnusedLocalVariable(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getMethod()); + } + + /** + * @test + * @return void + * @since 2.0.1 + */ + public function test_namespaced_compact_function_rule_works_case_insensitive() + { + $rule = new UnusedLocalVariable(); + $rule->setReport($this->getReportMock(0)); + $rule->apply($this->getMethod()); + } } diff --git a/src/test/resources/files/Rule/UnusedFormalParameter/test_namespaced_compact_function_rule_does_not_apply.php b/src/test/resources/files/Rule/UnusedFormalParameter/test_namespaced_compact_function_rule_does_not_apply.php new file mode 100644 index 000000000..a15ec1029 --- /dev/null +++ b/src/test/resources/files/Rule/UnusedFormalParameter/test_namespaced_compact_function_rule_does_not_apply.php @@ -0,0 +1,11 @@ +