-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
See https://github.com/extdn/extdn-phpcs/issues/49 #51
Merged
schmengler
merged 4 commits into
extdn:master
from
jissereitsma:rule-deprecated-block-parents
Jul 30, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does reflection with the parent class work if it is not present?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in my case it did. The benefit here is that you don't need to parse the
use
statement andclass X extends Y implements Z
construct. So no dependency with Magento.In the Reflection utilities that I've used here, I've added exception handling so that when Magento is not present (or something else fails) the rule can be bypassed: https://github.com/extdn/extdn-phpcs/blob/master/Extdn/Utils/Reflection.php#L35 So, the rules that depend on this Reflection could work (report stuff) when Magento is present, but they will also work (but not report stuff) when Magento is not there. Kind of makes sense to me personally.