From dc69a01a5d9712a2ae418187e0d05858c825e5c0 Mon Sep 17 00:00:00 2001 From: Marc Ginesta Date: Thu, 2 Sep 2021 12:01:44 +0200 Subject: [PATCH 1/5] AC-664: Create phpcs static check for CodingStandardsIgnoreAnnotationUsageTest - Initial commit, copy the sniff across from magento repo --- ...ingStandardsIgnoreAnnotationUsageSniff.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php diff --git a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php new file mode 100644 index 00000000..cc67b6cd --- /dev/null +++ b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php @@ -0,0 +1,38 @@ +fail( + '@codingStandardsIgnoreFile annotation must be avoided. ' + . 'Use codingStandardsIgnoreStart/codingStandardsIgnoreEnd to suppress code fragment ' + . 'or use codingStandardsIgnoreLine to suppress line. ' + . $filename + ); + } + }, + Files::init()->getPhpFiles( + Files::INCLUDE_APP_CODE + | Files::INCLUDE_PUB_CODE + | Files::INCLUDE_LIBS + | Files::AS_DATA_SET + | Files::INCLUDE_NON_CLASSES + ) + ); + } +} From 1a3f16f029a0f728f6b21439ef0af8a8364f6216 Mon Sep 17 00:00:00 2001 From: Marc Ginesta Date: Thu, 2 Sep 2021 16:24:02 +0200 Subject: [PATCH 2/5] AC-664: Create phpcs static check for CodingStandardsIgnoreAnnotationUsageTest - Add methods register() and process() --- ...ingStandardsIgnoreAnnotationUsageSniff.php | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php index cc67b6cd..aec8b110 100644 --- a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php +++ b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php @@ -6,33 +6,43 @@ namespace Magento2\Sniffs\Legacy; -use PHPUnit\Framework\TestCase; -use Magento\Framework\App\Utility\Files; +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; -class CodingStandardsIgnoreAnnotationUsageSniff extends TestCase +class CodingStandardsIgnoreAnnotationUsageSniff implements Sniff { - public function testAnnotationUsage() + private const CODING_STANDARDS_IGNORE_FILE = '@codingStandardsIgnoreFile'; + + private const WARNING_CODE = self::CODING_STANDARDS_IGNORE_FILE . ' annotation must be avoided. '; + + private const WARNING_MESSAGE = + self::WARNING_CODE + . 'Use codingStandardsIgnoreStart/codingStandardsIgnoreEnd to suppress code fragment ' + . 'or use codingStandardsIgnoreLine to suppress line. '; + + /** + * @inheritDoc + */ + public function register(): array { - $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this); - $invoker( - function ($filename) { - $fileText = file_get_contents($filename); - if (strpos($fileText, '@codingStandardsIgnoreFile') !== false) { - $this->fail( - '@codingStandardsIgnoreFile annotation must be avoided. ' - . 'Use codingStandardsIgnoreStart/codingStandardsIgnoreEnd to suppress code fragment ' - . 'or use codingStandardsIgnoreLine to suppress line. ' - . $filename - ); - } - }, - Files::init()->getPhpFiles( - Files::INCLUDE_APP_CODE - | Files::INCLUDE_PUB_CODE - | Files::INCLUDE_LIBS - | Files::AS_DATA_SET - | Files::INCLUDE_NON_CLASSES - ) - ); + return [ + T_INLINE_HTML + ]; + } + + /** + * @inheritDoc + */ + public function process(File $phpcsFile, $stackPtr) + { + $content = $phpcsFile->getTokensAsString($stackPtr, 999999); + + if (strpos($content, self::CODING_STANDARDS_IGNORE_FILE) !== false) { + $phpcsFile->addWarning( + self::WARNING_MESSAGE . $phpcsFile->getFilename(), + $stackPtr, + self::WARNING_CODE + ); + } } } From 98b8cd8b2e1790fc989fcdb7ac9267d11bde71d3 Mon Sep 17 00:00:00 2001 From: Marc Ginesta Date: Fri, 3 Sep 2021 12:45:08 +0200 Subject: [PATCH 3/5] AC-664: Create phpcs static check for CodingStandardsIgnoreAnnotationUsageTest - Add unit tests and update ruleset.xml --- ...ingStandardsIgnoreAnnotationUsageSniff.php | 6 +-- ...StandardsIgnoreAnnotationUsageUnitTest.inc | 37 +++++++++++++++++++ ...StandardsIgnoreAnnotationUsageUnitTest.php | 24 ++++++++++++ Magento2/ruleset.xml | 4 ++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.inc create mode 100644 Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php diff --git a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php index aec8b110..1c4c5720 100644 --- a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php +++ b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php @@ -26,7 +26,7 @@ class CodingStandardsIgnoreAnnotationUsageSniff implements Sniff public function register(): array { return [ - T_INLINE_HTML + T_OBJECT_OPERATOR ]; } @@ -35,9 +35,9 @@ public function register(): array */ public function process(File $phpcsFile, $stackPtr) { - $content = $phpcsFile->getTokensAsString($stackPtr, 999999); + $lineContent = $phpcsFile->getTokensAsString($stackPtr, 1); - if (strpos($content, self::CODING_STANDARDS_IGNORE_FILE) !== false) { + if (strpos($lineContent, self::CODING_STANDARDS_IGNORE_FILE) !== false) { $phpcsFile->addWarning( self::WARNING_MESSAGE . $phpcsFile->getFilename(), $stackPtr, diff --git a/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.inc b/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.inc new file mode 100644 index 00000000..2ee18131 --- /dev/null +++ b/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.inc @@ -0,0 +1,37 @@ +randomNumber = random_int(0, 999); + } + + /** + * @return int + */ + public function getRandomNumber(): int + { + return $this->randomNumber; + } + + /** + * @param int $randomNumber + */ + public function setRandomNumber(int $randomNumber): void + { + $this->randomNumber = $randomNumber; + } +} diff --git a/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php b/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php new file mode 100644 index 00000000..86411fb4 --- /dev/null +++ b/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php @@ -0,0 +1,24 @@ + + + 9 + warning + 9 warning From 8207a11fba6dd98a71ba8774ed92b93522e6eaa3 Mon Sep 17 00:00:00 2001 From: Marc Ginesta Date: Fri, 3 Sep 2021 12:48:13 +0200 Subject: [PATCH 4/5] AC-664: Create phpcs static check for CodingStandardsIgnoreAnnotationUsageTest - Fix Typo --- Magento2/ruleset.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 3381a8c8..92ff8ac2 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -111,7 +111,7 @@ - + 9 warning From 0449dbd63b2dd7730d4219d74593ebe399e18bce Mon Sep 17 00:00:00 2001 From: Marc Ginesta Date: Fri, 3 Sep 2021 14:35:59 +0200 Subject: [PATCH 5/5] AC-664: Create phpcs static check for CodingStandardsIgnoreAnnotationUsageTest - Refactor: Fix small issues --- .../CodingStandardsIgnoreAnnotationUsageSniff.php | 11 +++++------ .../CodingStandardsIgnoreAnnotationUsageUnitTest.php | 6 ++++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php index 1c4c5720..338fc1aa 100644 --- a/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php +++ b/Magento2/Sniffs/Legacy/CodingStandardsIgnoreAnnotationUsageSniff.php @@ -13,10 +13,9 @@ class CodingStandardsIgnoreAnnotationUsageSniff implements Sniff { private const CODING_STANDARDS_IGNORE_FILE = '@codingStandardsIgnoreFile'; - private const WARNING_CODE = self::CODING_STANDARDS_IGNORE_FILE . ' annotation must be avoided. '; + private const WARNING_CODE = 'avoidAnnotation'; - private const WARNING_MESSAGE = - self::WARNING_CODE + private const WARNING_MESSAGE = '@codingStandardsIgnoreFile annotation must be avoided. ' . 'Use codingStandardsIgnoreStart/codingStandardsIgnoreEnd to suppress code fragment ' . 'or use codingStandardsIgnoreLine to suppress line. '; @@ -26,7 +25,7 @@ class CodingStandardsIgnoreAnnotationUsageSniff implements Sniff public function register(): array { return [ - T_OBJECT_OPERATOR + T_COMMENT ]; } @@ -35,9 +34,9 @@ public function register(): array */ public function process(File $phpcsFile, $stackPtr) { - $lineContent = $phpcsFile->getTokensAsString($stackPtr, 1); + $tokens = $phpcsFile->getTokens(); - if (strpos($lineContent, self::CODING_STANDARDS_IGNORE_FILE) !== false) { + if (strpos($tokens[$stackPtr - 1]['content'], self::CODING_STANDARDS_IGNORE_FILE) !== false) { $phpcsFile->addWarning( self::WARNING_MESSAGE . $phpcsFile->getFilename(), $stackPtr, diff --git a/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php b/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php index 86411fb4..ac9caece 100644 --- a/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php +++ b/Magento2/Tests/Legacy/CodingStandardsIgnoreAnnotationUsageUnitTest.php @@ -19,6 +19,8 @@ public function getErrorList() */ public function getWarningList() { - return []; + return [ + 7 => 1 + ]; } -} \ No newline at end of file +}