Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'ignore-namespaces' option to DevelopmentCodeFragment #571

Merged
merged 7 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main/php/PHPMD/Rule/Design/DevelopmentCodeFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ class DevelopmentCodeFragment extends AbstractRule implements MethodAware, Funct
*/
public function apply(AbstractNode $node)
{
$ignoreNS = $this->getBooleanProperty('ignore-namespaces');
$namespace = $node->getNamespaceName();
foreach ($node->findChildrenOfType('FunctionPostfix') as $postfix) {
$image = strtolower($postfix->getImage());
if (false === in_array($image, $this->getSuspectImages())) {
$fragment = $postfix->getImage();
if ($ignoreNS) {
$fragment = str_replace("{$namespace}\\", "", $fragment);
}
$fragment = strtolower($fragment);
if (false === in_array($fragment, $this->getSuspectImages())) {
continue;
}

Expand All @@ -52,7 +58,7 @@ public function apply(AbstractNode $node)
$image = sprintf('%s::%s', $node->getParentName(), $node->getImage());
}

$this->addViolation($postfix, array($node->getType(), $image, $postfix->getImage()));
$this->addViolation($postfix, array($node->getType(), $image, $fragment));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/rulesets/design.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ just forgotten.
<priority>2</priority>
<properties>
<property name="unwanted-functions" value="var_dump,print_r,debug_zval_dump,debug_print_backtrace" description="Comma separated list of suspect function images." />
<property name="ignore-namespaces" value="false" description="Ignore namespaces when looking for dev. fragments" />
</properties>
<example>
<![CDATA[
Expand Down
54 changes: 42 additions & 12 deletions src/test/php/PHPMD/Rule/Design/DevelopmentCodeFragmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class DevelopmentCodeFragmentTest extends AbstractTest
*/
public function testRuleNotAppliesToMethodWithoutSuspectFunctionCall()
{
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule = $this->getRule();
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -49,8 +48,7 @@ public function testRuleNotAppliesToMethodWithoutSuspectFunctionCall()
*/
public function testRuleAppliesToMethodWithSuspectFunctionCall()
{
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule = $this->getRule();
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getMethod());
}
Expand All @@ -62,8 +60,7 @@ public function testRuleAppliesToMethodWithSuspectFunctionCall()
*/
public function testRuleAppliesToMethodWithMultipleSuspectFunctionCall()
{
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule = $this->getRule();
$rule->setReport($this->getReportMock(3));
$rule->apply($this->getMethod());
}
Expand All @@ -75,8 +72,7 @@ public function testRuleAppliesToMethodWithMultipleSuspectFunctionCall()
*/
public function testRuleNotAppliesToFunctionWithoutSuspectFunctionCall()
{
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule = $this->getRule();
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -88,8 +84,7 @@ public function testRuleNotAppliesToFunctionWithoutSuspectFunctionCall()
*/
public function testRuleAppliesToFunctionWithSuspectFunctionCall()
{
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule = $this->getRule();
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getFunction());
}
Expand All @@ -101,9 +96,44 @@ public function testRuleAppliesToFunctionWithSuspectFunctionCall()
*/
public function testRuleAppliesToFunctionWithMultipleSuspectFunctionCall()
{
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule = $this->getRule();
$rule->setReport($this->getReportMock(3));
$rule->apply($this->getFunction());
}

/**
* testRuleAppliesToMethodWithinNamespace
*
* @return void
*/
public function testRuleAppliesToMethodWithinNamespace()
{
$rule = $this->getRule();
$rule->addProperty('ignore-namespaces', 'true');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getClass());
}

/**
* testRuleNotAppliesToMethodWithinNamespaceByDefault
*
* @return void
*/
public function testRuleNotAppliesToMethodWithinNamespaceByDefault()
{
$rule = $this->getRule();
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getClass());
}

/**
* Get a configured DevelopmentCodeFragment rule
* @return DevelopmentCodeFragment
*/
private function getRule() {
$rule = new DevelopmentCodeFragment();
$rule->addProperty('unwanted-functions', 'var_dump,print_r,debug_zval_dump,debug_print_backtrace');
$rule->addProperty('ignore-namespaces', 'false');
return $rule;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Test\Namespace;

/**
* 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/
*/

class testRuleAppliesToMethodWithinNamespace {
public function __construct($test = 'Test') {
var_dump($test);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Test\Namespace;

/**
* 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/
*/

class testRuleNotAppliesToMethodWithinNamespaceByDefault {
public function __construct($test = 'Test') {
var_dump($test);
}
}