Skip to content

Commit c51ebae

Browse files
author
Volodymyr Kublytskyi
committed
MAGETWO-64523: Add static test on forbidden "final" keyword and eliminate it usage in code
- implemented PHPMD rule for detecting final classes and methods - created PHPMD ruleset for Magento specific design rules
1 parent 213392f commit c51ebae

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Magento Specific Design Rules"
3+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
7+
<rule name="FinalImplementation"
8+
class="Magento\MessDetector\Rule\Design\FinalImplementation"
9+
message= "The {0} {1} declared as final.">
10+
<description>
11+
<![CDATA[
12+
Final keyword is prohibited in Magento as this decreases extensibility and customizability.
13+
Final classes and method are not compatible with plugins and proxies.
14+
]]>
15+
</description>
16+
<priority>1</priority>
17+
<properties />
18+
<example>
19+
<![CDATA[
20+
final class Foo
21+
{
22+
public function bar() {}
23+
}
24+
class Baz {
25+
final public function bad() {}
26+
}
27+
]]>
28+
</example>
29+
</rule>
30+
</ruleset>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\MessDetector\Rule\Design;
8+
9+
use PHPMD\AbstractNode;
10+
use PHPMD\AbstractRule;
11+
use PHPMD\Rule\ClassAware;
12+
use PHPMD\Rule\MethodAware;
13+
14+
/**
15+
* Magento is a highly extensible and customizable platform.
16+
* Usage of final classes and methods is prohibited.
17+
*/
18+
class FinalImplementation extends AbstractRule implements ClassAware, MethodAware
19+
{
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
public function apply(AbstractNode $node)
25+
{
26+
if ($node->isFinal()) {
27+
$this->addViolation($node, [$node->getType(), $node->getFullQualifiedName()]);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)