Skip to content

Commit e287f41

Browse files
author
Fabian Schmengler /
authored
Merge pull request #51 from jissereitsma/rule-deprecated-block-parents
See #49
2 parents 2db69a7 + 6cf3243 commit e287f41

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Extdn\Samples\Blocks;
3+
4+
class DeprecatedFormContainerParent extends \Magento\Backend\Block\Widget\Form\Container
5+
{
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Extdn\Samples\Blocks;
3+
4+
class DeprecatedFormGenericParent extends \Magento\Backend\Block\Widget\Form\Generic
5+
{
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Rule: Do not extend from deprecated block parents
2+
## Background
3+
Some parent classes have been deprecated in Magento 2.2 and should therefore no longer be used in code:
4+
- `Magento\Backend\Block\Widget\Form\Generic`
5+
- `Magento\Backend\Block\Widget\Grid\Container`
6+
7+
## Reasoning
8+
Once a Block class is extending upon one of these deprecated parents, they should be refactored into something else instead. Ideally, this is a uiComponent.
9+
The main reason why a uiComponent is preferred over a regular Block-driven output, is that a uiComponent is much more extensible than Blocks.
10+
11+
## How it works
12+
This rule uses PHP Reflection to determine the class its parent and then checks whether the parent matches a deprecated parent.
13+
14+
## How to fix
15+
The Block class should ideally be removed. Instead, a uiComponent should be created instead, consisting of an XML file in the `ui_component` folder plus PHP sources.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © ExtDN. All rights reserved.
4+
*/
5+
6+
declare(strict_types=1);
7+
8+
namespace Extdn\Sniffs\Blocks;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use Extdn\Utils\Reflection;
13+
use Zend\Server\Reflection\ReflectionClass;
14+
15+
/**
16+
* Class DeprecatedParentsSniff
17+
*
18+
* @package Extdn\Sniffs\Classes\Constructor
19+
*/
20+
class DeprecatedParentsSniff implements Sniff
21+
{
22+
/**
23+
* @var string
24+
*/
25+
protected $message = 'A Block class should not extend from deprecated parents';
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function register()
31+
{
32+
return [T_CLASS];
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function process(File $phpcsFile, $stackPtr)
39+
{
40+
$className = Reflection::findClassName($phpcsFile);
41+
if (empty($className)) {
42+
return false;
43+
}
44+
45+
// Make sure to load the file itself, so that autoloading can be skipped
46+
include_once($phpcsFile->getFilename());
47+
48+
$class = Reflection::getClass($className);
49+
$parentClass = $class->getParentClass();
50+
51+
foreach ($this->getDeprecatedClasses() as $deprecatedClass) {
52+
if ($parentClass->getName() !== $deprecatedClass['class']) {
53+
continue;
54+
}
55+
56+
$warning = sprintf('Block parent "%s" is deprecated. %s', $deprecatedClass['class'], $deprecatedClass['advice']);
57+
$phpcsFile->addWarning($warning, null, 'deprecated-parent');
58+
}
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
private function getDeprecatedClasses(): array
65+
{
66+
$url = 'https://github.com/extdn/extdn-phpcs/blob/master/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md';
67+
68+
return [
69+
[
70+
'class' => 'Magento\Backend\Block\Widget\Form\Generic',
71+
'advice' => 'See '.$url
72+
],
73+
[
74+
'class' => 'Magento\Backend\Block\Widget\Grid\Container',
75+
'advice' => 'See '.$url
76+
]
77+
];
78+
}
79+
}

Extdn/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<exclude-pattern>*.php</exclude-pattern>
88
</rule>
99
<rule ref="Extdn.Blocks.SetTemplateInBlock"/>
10+
<rule ref="Extdn.Blocks.DeprecatedParents"/>
1011
<rule ref="Extdn.Classes.StrictTypes"/>
12+
<rule ref="Extdn.Classes.ObjectManager"/>
1113
<rule ref="Extdn.Templates.TemplateObjectManager"/>
1214
</ruleset>

0 commit comments

Comments
 (0)