Skip to content

Commit

Permalink
Merge branch 'fix-namespaced-compact' of https://github.com/radmen/phpmd
Browse files Browse the repository at this point in the history
 into radmen-fix-namespaced-compact
  • Loading branch information
Manuel Pichler committed Jul 23, 2014
2 parents 210dff8 + a3c5ebd commit ffab9fc
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/main/php/PHPMD/Rule/AbstractLocalVariable.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
5 changes: 3 additions & 2 deletions src/main/php/PHPMD/Rule/UnusedFormalParameter.php
Expand Up @@ -109,7 +109,7 @@ private function isAbstractMethod(AbstractNode $node)
}
return false;
}

/**
* Returns <b>true</b> when the given node is method with signature declared as inherited using
* {@inheritdoc} annotation.
Expand Down Expand Up @@ -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(), '"\'')]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/UnusedLocalVariable.php
Expand Up @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/php/PHPMD/Rule/UnusedFormalParameterTest.php
Expand Up @@ -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());
}
}
35 changes: 35 additions & 0 deletions src/test/php/PHPMD/Rule/UnusedLocalVariableTest.php
Expand Up @@ -512,4 +512,39 @@ public function test_compact_function_rule_works_case_insensitive()
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}

/**
* testRuleDoesNotApplyToNamespacedCompactFunction
*
* <code>
* namespace Baz;
*
* class Foo {
* public function bar() {
* $key = 'ok';
* return compact('key');
* }
* }
* </code>
*
* @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());
}
}
@@ -0,0 +1,11 @@
<?php

namespace PHPMDTest;

class test_namespaced_compact_function_rule_does_not_apply
{
public function test_namespaced_compact_function_rule_does_not_apply($foo, $bar)
{
return compact('foo', 'bar');
}
}
@@ -0,0 +1,11 @@
<?php

namespace PHPMDTest;

class test_namespaced_compact_function_rule_only_applies_to_used_parameters
{
public function test_namespaced_compact_function_rule_only_applies_to_used_parameters($foo, $bar, $baz)
{
return compact('bar');
}
}
@@ -0,0 +1,11 @@
<?php

namespace PHPMDTest;

class test_namespaced_compact_function_rule_works_case_insensitive
{
public function test_namespaced_compact_function_rule_works_case_insensitive($foo, $bar)
{
return Compact('foo', 'bar');
}
}
@@ -0,0 +1,12 @@
<?php

namespace PHPMDTest;

class testRuleDoesNotApplyToNamespacedCompactFunction
{
public function testRuleDoesNotApplyToNamespacedCompactFunction()
{
$key = 'ok';
return compact('key');
}
}
@@ -0,0 +1,13 @@
<?php

namespace PHPMDTest;

class test_namespaced_compact_function_rule_works_case_insensitive
{
public function test_namespaced_compact_function_rule_works_case_insensitive()
{
$foo = 1; $bar = 2; $baz = 0;

return Compact('foo', 'bar', 'baz');
}
}

0 comments on commit ffab9fc

Please sign in to comment.