From 1d380aa40c92491d2c0b751aa3211f1e0c99ccb0 Mon Sep 17 00:00:00 2001 From: milindsingh Date: Sun, 24 May 2020 00:03:32 +0530 Subject: [PATCH 1/4] Added sniff for deprecated model methods. #187 --- .../Methods/DeprecatedModelMethodSniff.php | 79 +++++++++++++++++++ .../Methods/DeprecatedModelMethodUnitTest.inc | 4 + .../Methods/DeprecatedModelMethodUnitTest.php | 31 ++++++++ Magento2/ruleset.xml | 4 + 4 files changed, 118 insertions(+) create mode 100644 Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php create mode 100644 Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc create mode 100644 Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php diff --git a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php new file mode 100644 index 00000000..08ee94d3 --- /dev/null +++ b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php @@ -0,0 +1,79 @@ +getTokens(); + $methodPosition = $phpcsFile->findNext(T_STRING, $stackPtr + 1); + + if ($methodPosition !== false && + in_array($tokens[$methodPosition]['content'], $this->methods) + ) { + $resourcePosition = $phpcsFile->findPrevious([T_STRING, T_VARIABLE], $stackPtr - 1); + if ($resourcePosition !== false) { + $methodName = $tokens[$resourcePosition]['content']; + if ($methodName === "getResource") { + $phpcsFile->addWarning( + sprintf($this->warningMessage, $tokens[$methodPosition]['content']), + $stackPtr, + $this->warningCode + ); + } + } + } + } +} diff --git a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc new file mode 100644 index 00000000..e81954f3 --- /dev/null +++ b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc @@ -0,0 +1,4 @@ +getResource()->save(); +$model->getResource()->load(); +$model->getResource()->delete(); \ No newline at end of file diff --git a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php new file mode 100644 index 00000000..024c660a --- /dev/null +++ b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php @@ -0,0 +1,31 @@ + 1, + 3 => 1, + 4 => 1 + ]; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 96188ad7..0a0bed7b 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -215,6 +215,10 @@ 8 warning + + 8 + warning + From a5b372313356548e2e9183fabd7d3285e6e3182b Mon Sep 17 00:00:00 2001 From: milindsingh Date: Wed, 27 May 2020 09:52:22 +0530 Subject: [PATCH 2/4] code review fixes --- .../Methods/DeprecatedModelMethodSniff.php | 36 ++++++++++--------- .../Methods/DeprecatedModelMethodUnitTest.inc | 9 +++-- .../Methods/DeprecatedModelMethodUnitTest.php | 4 +-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php index 08ee94d3..283d74ad 100644 --- a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php +++ b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php @@ -14,6 +14,8 @@ */ class DeprecatedModelMethodSniff implements Sniff { + const RESOURCE_METHOD = "getResource"; + /** * String representation of warning. * @@ -48,8 +50,7 @@ class DeprecatedModelMethodSniff implements Sniff public function register() { return [ - T_OBJECT_OPERATOR, - T_DOUBLE_COLON + T_OBJECT_OPERATOR ]; } /** @@ -58,21 +59,22 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $methodPosition = $phpcsFile->findNext(T_STRING, $stackPtr + 1); - - if ($methodPosition !== false && - in_array($tokens[$methodPosition]['content'], $this->methods) - ) { - $resourcePosition = $phpcsFile->findPrevious([T_STRING, T_VARIABLE], $stackPtr - 1); - if ($resourcePosition !== false) { - $methodName = $tokens[$resourcePosition]['content']; - if ($methodName === "getResource") { - $phpcsFile->addWarning( - sprintf($this->warningMessage, $tokens[$methodPosition]['content']), - $stackPtr, - $this->warningCode - ); - } + $endOfStatement = $phpcsFile->findEndOfStatement($stackPtr); + $resourcePosition = $phpcsFile->findNext( + T_STRING, + $stackPtr + 1, + $endOfStatement, + false, + self::RESOURCE_METHOD + ); + if ($resourcePosition !== false) { + $methodPosition = $phpcsFile->findNext([T_STRING, T_VARIABLE], $resourcePosition + 1, $endOfStatement); + if ($methodPosition !== false && in_array($tokens[$methodPosition]['content'], $this->methods)) { + $phpcsFile->addWarning( + sprintf($this->warningMessage, $tokens[$methodPosition]['content']), + $stackPtr, + $this->warningCode + ); } } } diff --git a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc index e81954f3..322df84c 100644 --- a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc +++ b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc @@ -1,4 +1,7 @@ getResource()->save(); -$model->getResource()->load(); -$model->getResource()->delete(); \ No newline at end of file + +$model->getResource()->save($model); + +$model->getResource()->load($model, $id); + +$model->getResource()->delete($model); diff --git a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php index 024c660a..43d88dd3 100644 --- a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php +++ b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php @@ -23,9 +23,9 @@ public function getErrorList() public function getWarningList() { return [ - 2 => 1, 3 => 1, - 4 => 1 + 5 => 1, + 7 => 1, ]; } } From eb170637dc659f38095f0232dc6b1c5b98e1f445 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Mon, 17 Aug 2020 15:45:43 +0300 Subject: [PATCH 3/4] Added sniff for deprecated model methods Add few negative cases --- Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc index 322df84c..38e7a817 100644 --- a/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc +++ b/Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc @@ -5,3 +5,9 @@ $model->getResource()->save($model); $model->getResource()->load($model, $id); $model->getResource()->delete($model); + +$model->getResource()->myCustomMethod(); + +$model->myCustomMethod(); + +$model->anotherMethodWithResource()->save($model); From 902b134d4911ac2e29541b5521f4219b898d8cd1 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 17 Aug 2020 15:10:46 -0500 Subject: [PATCH 4/4] magento/magento-coding-standard#187: Added sniff for deprecated model methods - minor fixes --- Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php index 283d74ad..94bfc2d5 100644 --- a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php +++ b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php @@ -7,7 +7,6 @@ use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Util\Tokens; /** * Detects possible use of deprecated model methods. @@ -21,8 +20,7 @@ class DeprecatedModelMethodSniff implements Sniff * * @var string */ - protected $warningMessage = "Possible use of the deprecated model method 'getResource()'" . - " to '%s' the data detected."; + protected $warningMessage = "The use of the deprecated method 'getResource()' to '%s' the data detected."; /** * Warning violation code. @@ -69,7 +67,7 @@ public function process(File $phpcsFile, $stackPtr) ); if ($resourcePosition !== false) { $methodPosition = $phpcsFile->findNext([T_STRING, T_VARIABLE], $resourcePosition + 1, $endOfStatement); - if ($methodPosition !== false && in_array($tokens[$methodPosition]['content'], $this->methods)) { + if ($methodPosition !== false && in_array($tokens[$methodPosition]['content'], $this->methods, true)) { $phpcsFile->addWarning( sprintf($this->warningMessage, $tokens[$methodPosition]['content']), $stackPtr,