-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
{ | ||
} |
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 | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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 = 'The ObjectManager should not be injected into the constructor'; | ||
|
||
/** | ||
* @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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 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. |
||
|
||
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.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will not be clear for people who just started working with magento. I think it's better to add there any link to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use a standardized URL to document sniffs in the repo (it would be But good question: shouldn't we show this URL in the messages? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doc has now been added: jissereitsma@f831df3 And the rule warning now refers to the actual URL (which is not there yet): jissereitsma@078417c |
||
], | ||
[ | ||
'class' => 'Magento\Backend\Block\Widget\Grid\Container', | ||
'advice' => 'Refactor this to a UiComponent.' | ||
] | ||
]; | ||
} | ||
} |
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, but.. no 😁
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.
No, but now yes: jissereitsma@6cf3243