- Notifications
You must be signed in to change notification settings - Fork 9.4k
[PHPMD] Add phpmd rule with ignoring of unused required parameters in the plugins #33918
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
Merged
magento-devops-reposync-svc merged 13 commits into magento:2.4-develop from Den4ik:add-phpmd-rule-unusedformalparameter-with-plugin-ignore Aug 31, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits Select commit Hold shift + click to select a range
56f3fb8
[PHPMD] Add phpmd rule with ignoring of unused required parameters in…
Den4ik 90b33d8
Add copyright
Den4ik b8bf6fd
Detect plugin in namespace
Den4ik b4742d8
Remove interface declaration
Den4ik da17d7e
Implement suggested changes
Den4ik b51317b
Refactoring + added unit tests
Den4ik 4c14ab0
Fix static tests
Den4ik 827fdbf
Remove nused code
Den4ik f41db4e
Update doc block
Den4ik 6fb2f5e
Update doc block
Den4ik f129921
Merge branch '2.4-develop' into add-phpmd-rule-unusedformalparameter-…
Den4ik 8156a18
Merge branch '2.4-develop' into add-phpmd-rule-unusedformalparameter-…
Den4ik dcfc489
Merge branch '2.4-develop' into add-phpmd-rule-unusedformalparameter-…
engcom-Echo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
103 changes: 103 additions & 0 deletions 103 ...tests/static/framework/Magento/CodeMessDetector/Rule/UnusedCode/UnusedFormalParameter.php
This file contains hidden or 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,103 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
| ||
namespace Magento\CodeMessDetector\Rule\UnusedCode; | ||
| ||
use PHPMD\AbstractNode; | ||
use PHPMD\Node\ClassNode; | ||
use PHPMD\Node\MethodNode; | ||
use PHPMD\Rule\UnusedFormalParameter as PhpmdUnusedFormalParameter; | ||
| ||
class UnusedFormalParameter extends PhpmdUnusedFormalParameter | ||
{ | ||
/** | ||
* This method collects all local variables in the body of the currently | ||
* analyzed method or function and removes those parameters that are | ||
* referenced by one of the collected variables. | ||
* | ||
* @param AbstractNode $node | ||
* @return void | ||
*/ | ||
protected function removeUsedParameters(AbstractNode $node) | ||
{ | ||
parent::removeUsedParameters($node); | ||
$this->removeVariablesUsedInPlugins($node); | ||
} | ||
| ||
/** | ||
* Remove required method variables used in plugins from given node | ||
* | ||
* @param AbstractNode $node | ||
*/ | ||
private function removeVariablesUsedInPlugins(AbstractNode $node) | ||
{ | ||
if (!$node instanceof MethodNode) { | ||
return; | ||
} | ||
| ||
/** @var ClassNode $classNode */ | ||
$classNode = $node->getParentType(); | ||
if (!$this->isPluginClass($classNode->getNamespaceName())) { | ||
return; | ||
} | ||
| ||
/** | ||
* Around and After plugins has 2 required params $subject and $proceed or $result | ||
* that should be ignored | ||
*/ | ||
foreach (['around', 'after'] as $pluginMethodPrefix) { | ||
if ($this->isFunctionNameStartingWith($node, $pluginMethodPrefix)) { | ||
$this->removeVariablesByCount(2); | ||
| ||
break; | ||
} | ||
} | ||
| ||
/** | ||
* Before plugins has 1 required params $subject | ||
* that should be ignored | ||
*/ | ||
if ($this->isFunctionNameStartingWith($node, 'before')) { | ||
$this->removeVariablesByCount(1); | ||
} | ||
} | ||
| ||
/** | ||
* Check if the first part of function fully qualified name is equal to $name | ||
* | ||
* Methods getImage and getName are equal. getImage used prior to usage in phpmd source | ||
* | ||
* @param MethodNode $node | ||
* @param string $name | ||
* @return boolean | ||
*/ | ||
private function isFunctionNameStartingWith(MethodNode $node, string $name): bool | ||
{ | ||
return (0 === strpos($node->getImage(), $name)); | ||
} | ||
| ||
/** | ||
* Remove first $countOfRemovingVariables from given node | ||
* | ||
* @param int $countOfRemovingVariables | ||
*/ | ||
private function removeVariablesByCount(int $countOfRemovingVariables) | ||
{ | ||
array_splice($this->nodes, 0, $countOfRemovingVariables); | ||
} | ||
| ||
/** | ||
* Check if namespace contain "Plugin". Case-sensitive ignored | ||
* | ||
* @param string $class | ||
* @return bool | ||
*/ | ||
private function isPluginClass(string $class): bool | ||
{ | ||
return (stripos($class, 'plugin') !== false); | ||
} | ||
} |
178 changes: 178 additions & 0 deletions 178 ...ramework/Magento/CodeMessDetector/Test/Unit/Rule/UnusedCode/UnusedFormalParameterTest.php
This file contains hidden or 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,178 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
| ||
namespace Magento\CodeMessDetector\Test\Unit\Rule\UnusedCode; | ||
| ||
use Magento\CodeMessDetector\Rule\UnusedCode\UnusedFormalParameter; | ||
use PHPMD\Node\ASTNode; | ||
use PHPMD\Node\MethodNode; | ||
use PHPMD\Report; | ||
use PHPUnit\Framework\MockObject\MockObject as MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
| ||
/** | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
class UnusedFormalParameterTest extends TestCase | ||
{ | ||
private const FAKE_PLUGIN_NAMESPACE = 'Magento\CodeMessDetector\Test\UnusedCode\Plugin'; | ||
private const FAKE_NAMESPACE = 'Magento\CodeMessDetector\Test\UnusedCode'; | ||
| ||
/** | ||
* | ||
* @dataProvider getCases | ||
*/ | ||
public function testApply($methodName, $methodParams, $namespace, $expectViolation) | ||
{ | ||
$node = $this->createMethodNodeMock($methodName, $methodParams, $namespace); | ||
$rule = new UnusedFormalParameter(); | ||
$this->expectsRuleViolation($rule, $expectViolation); | ||
$rule->apply($node); | ||
} | ||
| ||
/** | ||
* Prepare method node mock | ||
* | ||
* @param $methodName | ||
* @param $methodParams | ||
* @param $namespace | ||
* @return MethodNode|MockObject | ||
*/ | ||
private function createMethodNodeMock($methodName, $methodParams, $namespace) | ||
{ | ||
$methodNode = $this->createConfiguredMock( | ||
MethodNode::class, | ||
[ | ||
'getName' => $methodName, | ||
'getImage' => $methodName, | ||
'isAbstract' => false, | ||
'isDeclaration' => true | ||
] | ||
); | ||
| ||
$variableDeclarators = []; | ||
foreach ($methodParams as $methodParam) { | ||
$variableDeclarator = $this->createASTNodeMock(); | ||
$variableDeclarator->method('getImage') | ||
->willReturn($methodParam); | ||
| ||
$variableDeclarators[] = $variableDeclarator; | ||
} | ||
$parametersMock = $this->createASTNodeMock(); | ||
$parametersMock->expects($this->once()) | ||
->method('findChildrenOfType') | ||
->with('VariableDeclarator') | ||
->willReturn($variableDeclarators); | ||
| ||
/** | ||
* Declare mock result for findChildrenOfType | ||
* with Dummy for removeCompoundVariables and removeVariablesUsedByFuncGetArgs | ||
*/ | ||
$methodNode->expects($this->atLeastOnce()) | ||
->method('findChildrenOfType') | ||
->withConsecutive(['FormalParameters'], ['CompoundVariable'], ['FunctionPostfix']) | ||
->willReturnOnConsecutiveCalls([$parametersMock], [], []); | ||
| ||
// Dummy result for removeRegularVariables | ||
$methodNode->expects($this->once()) | ||
->method('findChildrenOfTypeVariable') | ||
->willReturn([]); | ||
| ||
$classNode = $this->createASTNodeMock(); | ||
$classNode->expects($this->once()) | ||
->method('getNamespaceName') | ||
->willReturn($namespace); | ||
$methodNode->expects($this->once()) | ||
->method('getParentType') | ||
->willReturn($classNode); | ||
| ||
return $methodNode; | ||
} | ||
| ||
/** | ||
* Create ASTNode mock | ||
* | ||
* @return ASTNode|MockObject | ||
*/ | ||
private function createASTNodeMock() | ||
{ | ||
return $this->createMock(ASTNode::class); | ||
} | ||
| ||
/** | ||
* @param UnusedFormalParameter $rule | ||
* @param bool $expects | ||
*/ | ||
private function expectsRuleViolation(UnusedFormalParameter $rule, bool $expects) | ||
{ | ||
/** @var Report|MockObject $reportMock */ | ||
$reportMock = $this->createMock(Report::class); | ||
if ($expects) { | ||
$violationExpectation = $this->atLeastOnce(); | ||
} else { | ||
$violationExpectation = $this->never(); | ||
} | ||
$reportMock->expects($violationExpectation) | ||
->method('addRuleViolation'); | ||
$rule->setReport($reportMock); | ||
} | ||
| ||
/** | ||
* @return array | ||
*/ | ||
public function getCases(): array | ||
{ | ||
return [ | ||
// Plugin methods | ||
[ | ||
'beforePluginMethod', | ||
[ | ||
'subject' | ||
], | ||
self::FAKE_PLUGIN_NAMESPACE, | ||
false | ||
], | ||
[ | ||
'aroundPluginMethod', | ||
[ | ||
'subject', | ||
'proceed' | ||
], | ||
self::FAKE_PLUGIN_NAMESPACE, | ||
false | ||
], | ||
[ | ||
'aroundPluginMethod', | ||
[ | ||
'subject', | ||
'result' | ||
], | ||
self::FAKE_PLUGIN_NAMESPACE, | ||
false | ||
], | ||
// Plugin method that contain unused parameter | ||
[ | ||
'someMethod', | ||
[ | ||
'unusedParameter' | ||
], | ||
self::FAKE_PLUGIN_NAMESPACE, | ||
true | ||
], | ||
// Non plugin method | ||
[ | ||
'someMethod', | ||
[ | ||
'subject', | ||
'result' | ||
], | ||
self::FAKE_NAMESPACE, | ||
true | ||
] | ||
]; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions 31 dev/tests/static/framework/Magento/CodeMessDetector/resources/rulesets/unusedcode.xml
This file contains hidden or 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,31 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<ruleset name="Unused Code Rules" | ||
xmlns="http://pmd.sf.net/ruleset/1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" | ||
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> | ||
| ||
<rule name="UnusedFormalParameter" | ||
message="Avoid unused parameters such as '{0}'." | ||
class="Magento\CodeMessDetector\Rule\UnusedCode\UnusedFormalParameter"> | ||
<description><![CDATA[Avoid passing parameters to methods or constructors and then not using those parameters except on plugins]]></description> | ||
<example> | ||
<![CDATA[ | ||
class Foo | ||
{ | ||
private function bar($howdy) | ||
{ | ||
// $howdy is not used | ||
} | ||
} | ||
]]> | ||
</example> | ||
</rule> | ||
| ||
</ruleset> |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.