Skip to content

Commit 56de7a3

Browse files
committed
MAGETWO-94122: Independent blocks rule
1 parent 2d9c1e6 commit 56de7a3

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CodeMessDetector\Rule\Design;
8+
9+
use PHPMD\AbstractNode;
10+
use PHPMD\AbstractRule;
11+
use PHPMD\Node\ClassNode;
12+
use PHPMD\Node\MethodNode;
13+
use PDepend\Source\AST\ASTMethod;
14+
use PHPMD\Rule\MethodAware;
15+
16+
/**
17+
* Detect direct request usages.
18+
*/
19+
class RequestAwareBlockMethod extends AbstractRule implements MethodAware
20+
{
21+
/**
22+
* @inheritdoc
23+
*
24+
* @param ASTMethod|MethodNode $method
25+
*/
26+
public function apply(AbstractNode $method)
27+
{
28+
$definedIn = $method->getParentType();
29+
try {
30+
$isBlock = ($definedIn instanceof ClassNode)
31+
&& is_subclass_of(
32+
$definedIn->getFullQualifiedName(),
33+
\Magento\Framework\View\Element\AbstractBlock::class
34+
);
35+
} catch (\Throwable $exception) {
36+
//Failed to load classes.
37+
return;
38+
}
39+
40+
if ($isBlock) {
41+
$nodes = $method->findChildrenOfType('PropertyPostfix') + $method->findChildrenOfType('MethodPostfix');
42+
foreach ($nodes as $node) {
43+
$name = mb_strtolower($node->getFirstChildOfType('Identifier')->getImage());
44+
if ($name === '_request' || $name === 'getrequest') {
45+
$this->addViolation($method, [$method->getFullQualifiedName()]);
46+
break;
47+
}
48+
}
49+
}
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<ruleset name="Magento Specific Design Rules" 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" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
8+
<rule name="RequestAwareBlockMethod" class="Magento\CodeMessDetector\Rule\Design\RequestAwareBlockMethod" message="{0} uses request object directly. Add user input validation and suppress this warning.">
9+
<description><![CDATA[
10+
11+
Blocks must not depend on being used with certain controllers.
12+
If you use request object in a block directly you must validate all user input inside the block.
13+
14+
]]></description>
15+
<priority>2</priority>
16+
<properties/>
17+
<example><![CDATA[
18+
19+
class MyOrder extends AbstractBlock
20+
{
21+
22+
.......
23+
24+
public function getOrder()
25+
{
26+
$orderId = $this->getRequest()->getParam('order_id');
27+
//Validate customer having such order.
28+
if (!$this->hasOrder($this->getCustomerId(), $orderId)) {
29+
...deny access...
30+
}
31+
.....
32+
}
33+
}
34+
35+
]]></example>
36+
</rule>
37+
</ruleset>

dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength" />
1818
<rule ref="rulesets/codesize.xml/ExcessiveParameterList" />
1919
<rule ref="rulesets/codesize.xml/ExcessivePublicCount" />
20-
<rule ref="rulesets/codesize.xml/TooManyFields" />
20+
<rule ref="rulesets/codesize.xml/TooManyFields" />
2121
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
2222
<properties>
2323
<property name="maximum" value="100" />
@@ -43,4 +43,7 @@
4343
<rule ref="rulesets/naming.xml/ShortMethodName" />
4444
<rule ref="rulesets/naming.xml/ConstantNamingConventions" />
4545
<rule ref="rulesets/naming.xml/BooleanGetMethodName" />
46+
47+
<!-- Magento Specific Rules -->
48+
<rule ref="Magento/CodeMessDetector/resources/rulesets/design.xml/RequestAwareBlockMethod" />
4649
</ruleset>

0 commit comments

Comments
 (0)