From 9222bc7f6f57473f2b776ce4a14b1b8b78fd1b3b Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 11 Aug 2019 17:02:13 +0530 Subject: [PATCH 1/5] Fixed issue in line length sniff for phrase and __ keyword --- Magento2/Sniffs/Files/LineLengthSniff.php | 24 ++++++++++++++++++--- Magento2/Tests/Files/LineLengthUnitTest.inc | 1 + Magento2/Tests/Files/LineLengthUnitTest.php | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Magento2/Sniffs/Files/LineLengthSniff.php b/Magento2/Sniffs/Files/LineLengthSniff.php index f8baefa9..b527b128 100644 --- a/Magento2/Sniffs/Files/LineLengthSniff.php +++ b/Magento2/Sniffs/Files/LineLengthSniff.php @@ -27,6 +27,20 @@ class LineLengthSniff extends FilesLineLengthSniff */ protected $lastLineRegExp = '~__\(.+\)|\bPhrase\(.+\)~'; + /** + * Regular expression for finding a __ keyword added in the translation string. + * + * @var string + */ + protected $underscoreKeywordRegExp = '~\'[^\']+\'(*SKIP)(*F)| __~'; + + /** + * Regular expression for finding a Phrase keyword added in the translation string. + * + * @var string + */ + protected $phaseKeywordRegExp = '~\'[^\']+\'(*SKIP)(*F)| \bPhrase~'; + /** * Having the next-to-last line content allows to ignore long lines in case of translations. * @@ -77,10 +91,14 @@ protected function checkLineLength($phpcsFile, $tokens, $stackPtr) */ protected function doesPreviousLineContainTranslationString() { - $lastLineMatch = preg_match($this->lastLineRegExp, $this->lastLineContent) !== 0; - $nextToLastLineMatch = preg_match($this->nextToLastLineRegexp, $this->nextToLastLineContent) !== 0; + if (preg_match($this->phaseKeywordRegExp, $this->lastLineContent) == 0 + || preg_match($this->underscoreKeywordRegExp, $this->lastLineContent) == 0 + ) { + $lastLineMatch = preg_match($this->lastLineRegExp, $this->lastLineContent) !== 0; + $nextToLastLineMatch = preg_match($this->nextToLastLineRegexp, $this->nextToLastLineContent) !== 0; + return $lastLineMatch || $nextToLastLineMatch; + } - return $lastLineMatch || $nextToLastLineMatch; } /** diff --git a/Magento2/Tests/Files/LineLengthUnitTest.inc b/Magento2/Tests/Files/LineLengthUnitTest.inc index 6bfd8083..11c33329 100644 --- a/Magento2/Tests/Files/LineLengthUnitTest.inc +++ b/Magento2/Tests/Files/LineLengthUnitTest.inc @@ -29,3 +29,4 @@ $phraseObjects = [ $test = '012344567890123445678901234456789012344567890123445678901234456789'; $test1 = '01234456789012344567890123445678901234456789012344567890123445678901234456789'; $test2 = '012344567890123445678901234456789012344567890123445678901234456789012344567890123445678901234456789'; +$test3 = "If a string exceeds 120 characters and a part of the string is Phrase('xy'), this would not be caught by the regular expression because it matches any line containing Phrase('xy') or __('xy')."; diff --git a/Magento2/Tests/Files/LineLengthUnitTest.php b/Magento2/Tests/Files/LineLengthUnitTest.php index 486b2021..202e978d 100644 --- a/Magento2/Tests/Files/LineLengthUnitTest.php +++ b/Magento2/Tests/Files/LineLengthUnitTest.php @@ -20,6 +20,7 @@ public function getErrorList() return [ 9 => 1, 19 => 1, + 32 => 1, ]; } From 2a5774ec35f0f9d2219cf6d32463f5f28330c598 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Sun, 11 Aug 2019 17:11:32 +0530 Subject: [PATCH 2/5] fixed Static build test --- Magento2/Sniffs/Files/LineLengthSniff.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Magento2/Sniffs/Files/LineLengthSniff.php b/Magento2/Sniffs/Files/LineLengthSniff.php index b527b128..f1e48a23 100644 --- a/Magento2/Sniffs/Files/LineLengthSniff.php +++ b/Magento2/Sniffs/Files/LineLengthSniff.php @@ -98,7 +98,6 @@ protected function doesPreviousLineContainTranslationString() $nextToLastLineMatch = preg_match($this->nextToLastLineRegexp, $this->nextToLastLineContent) !== 0; return $lastLineMatch || $nextToLastLineMatch; } - } /** From 75b60e9caeb06c3b9b316cf21353071109b66bb1 Mon Sep 17 00:00:00 2001 From: konarshankar07 Date: Fri, 16 Aug 2019 23:40:36 +0530 Subject: [PATCH 3/5] Removed magento line length sniff --- Magento2/Sniffs/Files/LineLengthSniff.php | 149 -------------------- Magento2/Tests/Files/LineLengthUnitTest.inc | 32 ----- Magento2/Tests/Files/LineLengthUnitTest.php | 34 ----- Magento2/ruleset.xml | 13 +- 4 files changed, 8 insertions(+), 220 deletions(-) delete mode 100644 Magento2/Sniffs/Files/LineLengthSniff.php delete mode 100644 Magento2/Tests/Files/LineLengthUnitTest.inc delete mode 100644 Magento2/Tests/Files/LineLengthUnitTest.php diff --git a/Magento2/Sniffs/Files/LineLengthSniff.php b/Magento2/Sniffs/Files/LineLengthSniff.php deleted file mode 100644 index f1e48a23..00000000 --- a/Magento2/Sniffs/Files/LineLengthSniff.php +++ /dev/null @@ -1,149 +0,0 @@ -doesPreviousLineContainTranslationString()) { - parent::checkLineLength($phpcsFile, $tokens, $stackPtr); - } - - $this->updateLineBuffer($phpcsFile, $tokens, $stackPtr); - } - - /** - * Checks whether the previous line is part of a translation. - * - * The generic line sniff (which we are falling back to if there is no translation) always checks the length of the - * last line, so we have to check the last and next-to-last line for translations. - * - * @return bool - */ - protected function doesPreviousLineContainTranslationString() - { - if (preg_match($this->phaseKeywordRegExp, $this->lastLineContent) == 0 - || preg_match($this->underscoreKeywordRegExp, $this->lastLineContent) == 0 - ) { - $lastLineMatch = preg_match($this->lastLineRegExp, $this->lastLineContent) !== 0; - $nextToLastLineMatch = preg_match($this->nextToLastLineRegexp, $this->nextToLastLineContent) !== 0; - return $lastLineMatch || $nextToLastLineMatch; - } - } - - /** - * Assembles and returns the content for the code line of the provided stack pointer. - * - * @param File $phpcsFile - * @param array $tokens - * @param int $stackPtr - * @return string - */ - protected function getLineContent(File $phpcsFile, array $tokens, $stackPtr) - { - $lineContent = ''; - - /* - * Avoid out of range error at the end of the file - */ - if (!array_key_exists($stackPtr, $tokens)) { - return $lineContent; - } - - $codeLine = $tokens[$stackPtr]['line']; - - /* - * Concatenate the string until we jump to the next line or reach the end of line character. - */ - while (array_key_exists($stackPtr, $tokens) && - $tokens[$stackPtr]['line'] === $codeLine && - $tokens[$stackPtr]['content'] !== $phpcsFile->eolChar) { - $lineContent .= $tokens[$stackPtr]['content']; - $stackPtr++; - } - - return $lineContent; - } - - /** - * Pre-fills the line buffer for the next iteration. - * - * @param File $phpcsFile - * @param array $tokens - * @param int $stackPtr - */ - protected function updateLineBuffer(File $phpcsFile, array $tokens, $stackPtr) - { - $this->nextToLastLineContent = $this->lastLineContent; - $this->lastLineContent = $this->getLineContent($phpcsFile, $tokens, $stackPtr); - } -} diff --git a/Magento2/Tests/Files/LineLengthUnitTest.inc b/Magento2/Tests/Files/LineLengthUnitTest.inc deleted file mode 100644 index 11c33329..00000000 --- a/Magento2/Tests/Files/LineLengthUnitTest.inc +++ /dev/null @@ -1,32 +0,0 @@ - 1, - 19 => 1, - 32 => 1, - ]; - } - - /** - * @inheritdoc - */ - public function getWarningList() - { - return []; - } -} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 98100eae..14555108 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -172,11 +172,6 @@ */Test/* *Test.php - - 8 - warning - *.phtml - 8 warning @@ -306,6 +301,14 @@ 6 warning + + + + + + 6 + warning + 6 warning From 35990c0079b96937851adc37f26ae7742e9e304d Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 16 Aug 2019 15:55:42 -0500 Subject: [PATCH 4/5] magento/magento-coding-standard#75: [BUG] LineLengthSniff RegEx misses long lines with Phrase/__ keyword in string - fixed code style --- Magento2/Sniffs/PHP/FinalImplementationSniff.php | 2 +- Magento2/Sniffs/Security/LanguageConstructSniff.php | 2 +- Magento2/Sniffs/Strings/ExecutableRegExSniff.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Magento2/Sniffs/PHP/FinalImplementationSniff.php b/Magento2/Sniffs/PHP/FinalImplementationSniff.php index 2010e06d..e2ab7b16 100644 --- a/Magento2/Sniffs/PHP/FinalImplementationSniff.php +++ b/Magento2/Sniffs/PHP/FinalImplementationSniff.php @@ -27,7 +27,7 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $phpcsFile->addError( - // phpcs:ignore Magento2.Files.LineLength.MaxExceeded + // phpcs:ignore Generic.Files.LineLength 'Final keyword is prohibited in Magento. It decreases extensibility and is not compatible with plugins and proxies.', $stackPtr, 'FoundFinal' diff --git a/Magento2/Sniffs/Security/LanguageConstructSniff.php b/Magento2/Sniffs/Security/LanguageConstructSniff.php index a0981988..acb71481 100644 --- a/Magento2/Sniffs/Security/LanguageConstructSniff.php +++ b/Magento2/Sniffs/Security/LanguageConstructSniff.php @@ -23,7 +23,7 @@ class LanguageConstructSniff implements Sniff /** * String representation of backtick error. * - * phpcs:disable Magento2.Files.LineLength.MaxExceeded + * phpcs:disable Generic.Files.LineLength * * @var string */ diff --git a/Magento2/Sniffs/Strings/ExecutableRegExSniff.php b/Magento2/Sniffs/Strings/ExecutableRegExSniff.php index fb129097..901419c5 100644 --- a/Magento2/Sniffs/Strings/ExecutableRegExSniff.php +++ b/Magento2/Sniffs/Strings/ExecutableRegExSniff.php @@ -17,7 +17,7 @@ class ExecutableRegExSniff implements Sniff /** * String representation of error. * - * phpcs:disable Magento2.Files.LineLength.MaxExceeded + * phpcs:disable Generic.Files.LineLength * * @var string */ From 5016c63c15a12220d62dacf9fcb4ecd0a67c878c Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 16 Aug 2019 15:59:15 -0500 Subject: [PATCH 5/5] magento/magento-coding-standard#130: Prevent none zero exit code - fixed readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5c784a1..f31135c2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ You can achieve this by adding the following to your project's `composer.json`: ```` "scripts": { "post-install-cmd": [ - "([ $COMPOSER_DEV_MODE -eq 1 ] && vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/) || true" + "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)" ], "post-update-cmd": [ "([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"