From 59361a6ca97ab36c0ad4229d53d94eb4e079ab96 Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Sat, 14 Jul 2018 22:14:13 +0200 Subject: [PATCH 1/4] New rule --- .../Blocks/DeprecatedFormContainerParent.php | 6 ++ .../Blocks/DeprecatedFormGenericParent.php | 6 ++ .../Sniffs/Blocks/DeprecatedParentsSniff.php | 77 +++++++++++++++++++ Extdn/ruleset.xml | 2 + 4 files changed, 91 insertions(+) create mode 100644 Extdn/Samples/Blocks/DeprecatedFormContainerParent.php create mode 100644 Extdn/Samples/Blocks/DeprecatedFormGenericParent.php create mode 100644 Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php diff --git a/Extdn/Samples/Blocks/DeprecatedFormContainerParent.php b/Extdn/Samples/Blocks/DeprecatedFormContainerParent.php new file mode 100644 index 0000000..f5c812c --- /dev/null +++ b/Extdn/Samples/Blocks/DeprecatedFormContainerParent.php @@ -0,0 +1,6 @@ +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 + { + return [ + [ + 'class' => 'Magento\Backend\Block\Widget\Form\Generic', + 'advice' => 'Refactor this to a UiComponent.' + ], + [ + 'class' => 'Magento\Backend\Block\Widget\Grid\Container', + 'advice' => 'Refactor this to a UiComponent.' + ] + ]; + } +} diff --git a/Extdn/ruleset.xml b/Extdn/ruleset.xml index 9490b4b..3a5c85e 100644 --- a/Extdn/ruleset.xml +++ b/Extdn/ruleset.xml @@ -7,6 +7,8 @@ *.php + + From f831df3134ee6a3f3b2707ebd11b31e218311668 Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Thu, 19 Jul 2018 09:15:38 +0200 Subject: [PATCH 2/4] Add proper doc --- Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md diff --git a/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md b/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md new file mode 100644 index 0000000..97cd41b --- /dev/null +++ b/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.md @@ -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. From 078417c1bf148d9214cc34a337eaf41162096ba9 Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Thu, 19 Jul 2018 09:15:46 +0200 Subject: [PATCH 3/4] Refer to URL doc instead of manual text --- Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php b/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php index b7e280d..350d962 100644 --- a/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php +++ b/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php @@ -63,14 +63,16 @@ public function process(File $phpcsFile, $stackPtr) */ 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' => 'Refactor this to a UiComponent.' + 'advice' => 'See '.$url ], [ 'class' => 'Magento\Backend\Block\Widget\Grid\Container', - 'advice' => 'Refactor this to a UiComponent.' + 'advice' => 'See '.$url ] ]; } From 6cf32436b003ae8a895792c6bac5e9281627dfdb Mon Sep 17 00:00:00 2001 From: Jisse Reitsma Date: Thu, 19 Jul 2018 09:18:25 +0200 Subject: [PATCH 4/4] Fix main message --- Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php b/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php index 350d962..71b4639 100644 --- a/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php +++ b/Extdn/Sniffs/Blocks/DeprecatedParentsSniff.php @@ -22,7 +22,7 @@ class DeprecatedParentsSniff implements Sniff /** * @var string */ - protected $message = 'The ObjectManager should not be injected into the constructor'; + protected $message = 'A Block class should not extend from deprecated parents'; /** * @inheritdoc