diff --git a/Magento2/Sniffs/Legacy/AbstractBlockSniff.php b/Magento2/Sniffs/Legacy/AbstractBlockSniff.php new file mode 100644 index 00000000..41a7da00 --- /dev/null +++ b/Magento2/Sniffs/Legacy/AbstractBlockSniff.php @@ -0,0 +1,98 @@ +getTokens()[$stackPtr + 1]['content'])) { + return; + } + + $content = $phpcsFile->getTokens()[$stackPtr + 1]['content']; + + if (!$this->isApplicable($content)) { + return; + } + + $paramsCount = $this->getParametersCount($phpcsFile, $stackPtr + 1); + if ($content === self::CHILD_HTML_METHOD && $paramsCount >= 3) { + $phpcsFile->addError( + '3rd parameter is not needed anymore for getChildHtml()', + $stackPtr, + $this->errorCode + ); + } + if ($content === self::CHILD_CHILD_HTML_METHOD && $paramsCount >= 4) { + $phpcsFile->addError( + '4th parameter is not needed anymore for getChildChildHtml()', + $stackPtr, + $this->errorCode + ); + } + } + + /** + * Return if it is applicable to do the check + * + * @param string $content + * @return bool + */ + private function isApplicable(string $content): bool + { + return in_array($content, [self::CHILD_HTML_METHOD, self::CHILD_CHILD_HTML_METHOD]); + } + + /** + * Get the quantity of parameters on a method + * + * @param File $phpcsFile + * @param int $methodHtmlPosition + * @return int + */ + private function getParametersCount(File $phpcsFile, int $methodHtmlPosition): int + { + $closePosition = $phpcsFile->getTokens()[$methodHtmlPosition +1]['parenthesis_closer']; + $getTokenAsContent = $phpcsFile->getTokensAsString( + $methodHtmlPosition + 2, + ($closePosition - $methodHtmlPosition) - 2 + ); + if ($getTokenAsContent) { + $parameters = explode(',', $getTokenAsContent); + return count($parameters); + } + return 0; + } +} diff --git a/Magento2/Tests/Legacy/AbstractBlockUnitTest.inc b/Magento2/Tests/Legacy/AbstractBlockUnitTest.inc new file mode 100644 index 00000000..ca350548 --- /dev/null +++ b/Magento2/Tests/Legacy/AbstractBlockUnitTest.inc @@ -0,0 +1,36 @@ +getChildHtml( + function($param){ + $param->something(); + }, + 'aa' +); + +$this->getChildHtml( + function($param){ + $param->something(); + }, + 'aa', + 2 +); + +return $this->getChildHtml('aa', 'bb'); + +return $this->getChildHtml('aa', 'bb', 1, true); + +$this->getChildHtml(); + +$this->testMethod()->getChildHtml('aa', 'bb', 'cc'); + +$this->getChildChildHtml('aa', true, 'cc'); + +$this->getChildChildHtml('aa', true, 'cc', 1); + +$this->getChildChildHtml('aa' . 'bb', !(1 + 1) * (int) $this, 'cc', 1); + +private function getChildHtml($aa, $bb, $cc = 'cc') +{ +} + +$this->getChilChilddHtml(); diff --git a/Magento2/Tests/Legacy/AbstractBlockUnitTest.php b/Magento2/Tests/Legacy/AbstractBlockUnitTest.php new file mode 100644 index 00000000..62e9517c --- /dev/null +++ b/Magento2/Tests/Legacy/AbstractBlockUnitTest.php @@ -0,0 +1,33 @@ + 1, + 20 => 1, + 24 => 1, + 28 => 1, + 30 => 1 + ]; + } + + /** + * @inheritdoc + */ + public function getWarningList() + { + return []; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index f77859e9..3ea0c969 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -105,6 +105,10 @@ 10 error + + 10 + error +