Skip to content

Commit

Permalink
Merge pull request #51 from jissereitsma/rule-deprecated-block-parents
Browse files Browse the repository at this point in the history
See #49
  • Loading branch information
Fabian Schmengler / authored Jul 30, 2018
2 parents 2db69a7 + 6cf3243 commit e287f41
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Extdn/Samples/Blocks/DeprecatedFormContainerParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Extdn\Samples\Blocks;

class DeprecatedFormContainerParent extends \Magento\Backend\Block\Widget\Form\Container
{
}
6 changes: 6 additions & 0 deletions Extdn/Samples/Blocks/DeprecatedFormGenericParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Extdn\Samples\Blocks;

class DeprecatedFormGenericParent extends \Magento\Backend\Block\Widget\Form\Generic
{
}
15 changes: 15 additions & 0 deletions Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Rule: Do not extend from deprecated block parents
## Background
Some parent classes have been deprecated in Magento 2.2 and should therefore no longer be used in code:
- `Magento\Backend\Block\Widget\Form\Generic`
- `Magento\Backend\Block\Widget\Grid\Container`

## Reasoning
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.
The main reason why a uiComponent is preferred over a regular Block-driven output, is that a uiComponent is much more extensible than Blocks.

## How it works
This rule uses PHP Reflection to determine the class its parent and then checks whether the parent matches a deprecated parent.

## How to fix
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.
79 changes: 79 additions & 0 deletions Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © ExtDN. All rights reserved.
*/

declare(strict_types=1);

namespace Extdn\Sniffs\Blocks;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use Extdn\Utils\Reflection;
use Zend\Server\Reflection\ReflectionClass;

/**
* Class DeprecatedParentsSniff
*
* @package Extdn\Sniffs\Classes\Constructor
*/
class DeprecatedParentsSniff implements Sniff
{
/**
* @var string
*/
protected $message = 'A Block class should not extend from deprecated parents';

/**
* @inheritdoc
*/
public function register()
{
return [T_CLASS];
}

/**
* {@inheritdoc}
*/
public function process(File $phpcsFile, $stackPtr)
{
$className = Reflection::findClassName($phpcsFile);
if (empty($className)) {
return false;
}

// Make sure to load the file itself, so that autoloading can be skipped
include_once($phpcsFile->getFilename());

$class = Reflection::getClass($className);
$parentClass = $class->getParentClass();

foreach ($this->getDeprecatedClasses() as $deprecatedClass) {
if ($parentClass->getName() !== $deprecatedClass['class']) {
continue;
}

$warning = sprintf('Block parent "%s" is deprecated. %s', $deprecatedClass['class'], $deprecatedClass['advice']);
$phpcsFile->addWarning($warning, null, 'deprecated-parent');
}
}

/**
* @return array
*/
private function getDeprecatedClasses(): array
{
$url = 'https://github.com/extdn/extdn-phpcs/blob/master/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md';

return [
[
'class' => 'Magento\Backend\Block\Widget\Form\Generic',
'advice' => 'See '.$url
],
[
'class' => 'Magento\Backend\Block\Widget\Grid\Container',
'advice' => 'See '.$url
]
];
}
}
2 changes: 2 additions & 0 deletions Extdn/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<exclude-pattern>*.php</exclude-pattern>
</rule>
<rule ref="Extdn.Blocks.SetTemplateInBlock"/>
<rule ref="Extdn.Blocks.DeprecatedParents"/>
<rule ref="Extdn.Classes.StrictTypes"/>
<rule ref="Extdn.Classes.ObjectManager"/>
<rule ref="Extdn.Templates.TemplateObjectManager"/>
</ruleset>

0 comments on commit e287f41

Please sign in to comment.