diff --git a/Magento2/Helpers/Commenting/PHPDocFormattingValidator.php b/Magento2/Helpers/Commenting/PHPDocFormattingValidator.php index fb98db7c..745ec587 100644 --- a/Magento2/Helpers/Commenting/PHPDocFormattingValidator.php +++ b/Magento2/Helpers/Commenting/PHPDocFormattingValidator.php @@ -1,7 +1,7 @@ addWarning($error, $isShortDescriptionAfterVar, 'AlreadyHaveMeaningFulNameVar'); + $error = 'Short description must be before @var tag.'; + $phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'ShortDescriptionAfterVar'); return; } + // Check if class has already have meaningful description before @var tag $isShortDescriptionPreviousVar = $phpcsFile->findPrevious( T_DOC_COMMENT_STRING, @@ -121,23 +126,37 @@ public function processMemberVar(File $phpcsFile, $stackPtr) null, false ); - if ($this->PHPDocFormattingValidator->providesMeaning( - $isShortDescriptionPreviousVar, - $commentStart, - $tokens - ) !== true) { - preg_match( - '`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i', - $tokens[($foundVar + 2)]['content'], - $varParts - ); - if ($varParts[1]) { - return; - } + + if ($isShortDescriptionPreviousVar === false) { + return; + } + + $propertyNamePosition = $phpcsFile->findNext( + T_VARIABLE, + $foundVar, + null, + false, + null, + false + ); + if ($propertyNamePosition === false) { + return; + }; + $propertyName = trim($tokens[$propertyNamePosition]['content'], '$'); + $shortDescription = strtolower($tokens[$isShortDescriptionPreviousVar]['content']); + + if ($shortDescription === strtolower($propertyName)) { $error = 'Short description duplicates class property name.'; - $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar'); + $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar'); return; } + + $propertyNameParts = array_filter(preg_split('/(?=[A-Z])/', $propertyName)); + + if ($shortDescription === strtolower(implode(' ', $propertyNameParts))) { + $error = 'Short description duplicates class property name.'; + $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningfulNameVar'); + } } /** diff --git a/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php b/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php index c56e3c65..5d715af5 100644 --- a/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php +++ b/Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php @@ -1,6 +1,6 @@ 0) { + return; + } + + $fileText = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens())); + $adobeCopyrightFound = preg_match(self::COPYRIGHT_ADOBE, $fileText); + + if (strpos($fileText, self::COPYRIGHT_MAGENTO_TEXT) !== false || $adobeCopyrightFound) { + return; + } + + $phpcsFile->addWarningOnLine( + 'Copyright is missing or has wrong format', + null, + self::WARNING_CODE + ); + } +} diff --git a/Magento2/Sniffs/Legacy/CopyrightSniff.php b/Magento2/Sniffs/Legacy/CopyrightSniff.php new file mode 100644 index 00000000..f4ccd647 --- /dev/null +++ b/Magento2/Sniffs/Legacy/CopyrightSniff.php @@ -0,0 +1,61 @@ +findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) { + return; + } + + $positionComment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $stackPtr); + + if ($positionComment === false) { + $phpcsFile->addWarning( + 'Copyright is missing', + $stackPtr, + self::WARNING_CODE + ); + return; + } + + $content = $phpcsFile->getTokens()[$positionComment]['content']; + $adobeCopyrightFound = preg_match(self::COPYRIGHT_ADOBE, $content); + + if (strpos($content, self::COPYRIGHT_MAGENTO_TEXT) !== false || $adobeCopyrightFound) { + return; + } + + $phpcsFile->addWarningOnLine( + 'Copyright is missing or has wrong format', + $phpcsFile->getTokens()[$positionComment]['line'], + self::WARNING_CODE + ); + } +} diff --git a/Magento2/Sniffs/Legacy/EmailTemplateSniff.php b/Magento2/Sniffs/Legacy/EmailTemplateSniff.php new file mode 100644 index 00000000..17ebbf08 --- /dev/null +++ b/Magento2/Sniffs/Legacy/EmailTemplateSniff.php @@ -0,0 +1,50 @@ + 'Directive {{htmlescape}} is obsolete. Use {{var}} instead.', + '/\{\{escapehtml.*?\}\}/i' => 'Directive {{escapehtml}} is obsolete. Use {{var}} instead.', + ]; + + private const ERROR_CODE = 'FoundObsoleteEmailDirective'; + + /** + * @inheritdoc + */ + public function register(): array + { + return [ + T_INLINE_HTML + ]; + } + + /** + * @inheritDoc + */ + public function process(File $phpcsFile, $stackPtr) + { + $content = $phpcsFile->getTokens()[$stackPtr]['content']; + foreach (self::OBSOLETE_EMAIL_DIRECTIVES as $directiveRegex => $errorMessage) { + if (preg_match($directiveRegex, $content)) { + $phpcsFile->addError( + $errorMessage, + $stackPtr, + self::ERROR_CODE + ); + } + } + } +} diff --git a/Magento2/Sniffs/Legacy/MageEntitySniff.php b/Magento2/Sniffs/Legacy/MageEntitySniff.php index 637c3b1c..de8b7dbf 100644 --- a/Magento2/Sniffs/Legacy/MageEntitySniff.php +++ b/Magento2/Sniffs/Legacy/MageEntitySniff.php @@ -1,6 +1,6 @@ 0) { + return; + } + + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); + + if ($xml === false) { + $this->invalidXML($phpcsFile, $stackPtr); + return; + } + + $foundElements = $xml->xpath('/widgets/*[@type]'); + + foreach ($foundElements as $element) { + if (!property_exists($element->attributes(), 'type')) { + continue; + } + $type = $element['type']; + if (preg_match('/\//', $type)) { + $phpcsFile->addError( + "Factory name detected: {$type}.", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_FACTORY + ); + } + } + + $foundElements = $xml->xpath('/widgets/*/supported_blocks'); + foreach ($foundElements as $element) { + $phpcsFile->addError( + "Obsolete node: . To be replaced with ", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_OBSOLETE + ); + } + + $foundElements = $xml->xpath('/widgets/*/*/*/block_name'); + foreach ($foundElements as $element) { + $phpcsFile->addError( + "Obsolete node: . To be replaced with ", + dom_import_simplexml($element)->getLineNo() - 1, + self::ERROR_CODE_OBSOLETE + ); + } + } + + /** + * Adds an invalid XML error + * + * @param File $phpcsFile + * @param int $stackPtr + */ + protected function invalidXML(File $phpcsFile, int $stackPtr): void + { + $phpcsFile->addError( + sprintf( + "Couldn't parse contents of '%s', check that they are in valid XML format", + $phpcsFile->getFilename(), + ), + $stackPtr, + self::ERROR_CODE_XML + ); + } + + /** + * Format the incoming XML to avoid tags split into several lines. + * + * @param File $phpcsFile + * @return false|string + */ + private function getFormattedXML(File $phpcsFile) + { + $doc = new DomDocument('1.0'); + $doc->formatOutput = true; + $doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); + return $doc->saveXML(); + } +} diff --git a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php index df8fba14..f094c353 100644 --- a/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php +++ b/Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php @@ -1,6 +1,6 @@ 1, 56 => 1, 63 => 1, - 68 => 1 + 68 => 1, + 75 => 1, ]; } } diff --git a/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php index c5056dd7..16cd4d85 100644 --- a/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php +++ b/Magento2/Tests/Commenting/ConstantsPHPDocFormattingUnitTest.php @@ -1,6 +1,6 @@ extensions = array_merge( + $config->extensions, + [ + 'js' => 'PHP' + ] + ); + + $GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config; + } +} diff --git a/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.1.xml b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.1.xml new file mode 100644 index 00000000..5bbf8807 --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.1.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.2.js b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.2.js new file mode 100644 index 00000000..511f7b94 --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.2.js @@ -0,0 +1,10 @@ +/** + * Copyright Adobe. + * See COPYING.txt for license details. + */ + +define([ + 'jquery' +], function (){ + +}); \ No newline at end of file diff --git a/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.4.js b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.4.js new file mode 100644 index 00000000..2eb60f49 --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.4.js @@ -0,0 +1,10 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'jquery' +], function (){ + +}); \ No newline at end of file diff --git a/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.php b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.php new file mode 100644 index 00000000..8796abb9 --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.php @@ -0,0 +1,41 @@ + 1, + ]; + } + if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.2.js') { + return [ + null => 1, + ]; + } + if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.3.xml') { + return []; + } + if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.4.js') { + return []; + } + return []; + } +} diff --git a/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.xml b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.xml new file mode 100644 index 00000000..d1dcf359 --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/Magento2/Tests/Legacy/CopyrightUnitTest.1.inc b/Magento2/Tests/Legacy/CopyrightUnitTest.1.inc new file mode 100644 index 00000000..a4abe2da --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightUnitTest.1.inc @@ -0,0 +1,2 @@ + + + diff --git a/Magento2/Tests/Legacy/CopyrightUnitTest.4.inc b/Magento2/Tests/Legacy/CopyrightUnitTest.4.inc new file mode 100644 index 00000000..1cadcad0 --- /dev/null +++ b/Magento2/Tests/Legacy/CopyrightUnitTest.4.inc @@ -0,0 +1,7 @@ + 1, + ]; + } + if ($testFile === 'CopyrightUnitTest.2.inc' || $testFile === 'CopyrightUnitTest.3.inc') { + return [ + 3 => 1, + ]; + } + + return []; + } +} diff --git a/Magento2/Tests/Legacy/DiConfigUnitTest.php b/Magento2/Tests/Legacy/DiConfigUnitTest.php index d5dad883..8f3c81f7 100644 --- a/Magento2/Tests/Legacy/DiConfigUnitTest.php +++ b/Magento2/Tests/Legacy/DiConfigUnitTest.php @@ -1,6 +1,6 @@ {{var "H1"}} +

{{var "H2"}}

+

{{var "p"}} {{var "p"}}

+ diff --git a/Magento2/Tests/Legacy/EmailTemplateUnitTest.2.html b/Magento2/Tests/Legacy/EmailTemplateUnitTest.2.html new file mode 100644 index 00000000..422cea21 --- /dev/null +++ b/Magento2/Tests/Legacy/EmailTemplateUnitTest.2.html @@ -0,0 +1,4 @@ +

{{htmlescape "H1"}}

+

{{escapehtml "H2"}}

+

{{escapehtml "p"}} {{htmlescape "p"}}

+

{{trans "Translateme"}}

diff --git a/Magento2/Tests/Legacy/EmailTemplateUnitTest.php b/Magento2/Tests/Legacy/EmailTemplateUnitTest.php new file mode 100644 index 00000000..7732f0aa --- /dev/null +++ b/Magento2/Tests/Legacy/EmailTemplateUnitTest.php @@ -0,0 +1,39 @@ + 1, + 2 => 1, + 3 => 2, + ]; + } + + return []; + } + + /** + * @inheritdoc + */ + public function getWarningList($testFile = '') + { + return []; + } +} diff --git a/Magento2/Tests/Legacy/MageEntityUnitTest.php b/Magento2/Tests/Legacy/MageEntityUnitTest.php index 7e44b074..5a231473 100644 --- a/Magento2/Tests/Legacy/MageEntityUnitTest.php +++ b/Magento2/Tests/Legacy/MageEntityUnitTest.php @@ -1,6 +1,6 @@ + + + + + List of Products that are set as New + Deprecated + + Deprecated + + + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml b/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml new file mode 100644 index 00000000..f99ae1ca --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.2.xml @@ -0,0 +1,33 @@ + + + + + + + + List of Products that are set as New + + + + + Deprecated + + + + Deprecated + + + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml b/Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml new file mode 100644 index 00000000..ef90e28b --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.3.xml @@ -0,0 +1,22 @@ + + + + + + + diff --git a/Magento2/Tests/Legacy/WidgetXMLUnitTest.php b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php new file mode 100644 index 00000000..c52b920b --- /dev/null +++ b/Magento2/Tests/Legacy/WidgetXMLUnitTest.php @@ -0,0 +1,41 @@ + 1, + 12 => 1, + 14 => 1, + ]; + } + if ($testFile === 'WidgetXMLUnitTest.2.xml') { + return [ + 9 => 1, + 17 => 1, + 24 => 1, + ]; + } + return []; + } + + /** + * @inheritdoc + */ + public function getWarningList($testFile = '') + { + return []; + } +} diff --git a/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php b/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php index 71edcbee..d493b585 100644 --- a/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php +++ b/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php @@ -1,6 +1,6 @@ Magento Coding Standard - + @@ -109,6 +109,12 @@ 10 error + + view/email/*.html + view/*/email/*.html + 10 + error + @@ -241,6 +247,11 @@ 8 warning + + *\/widget.xml$ + 8 + warning + @@ -623,6 +634,14 @@ */Test/* *Test.php + + 5 + warning + + + 5 + warning + 0 diff --git a/PHP_CodeSniffer/Tokenizers/GRAPHQL.php b/PHP_CodeSniffer/Tokenizers/GRAPHQL.php index 8dd5fcae..05c38d50 100644 --- a/PHP_CodeSniffer/Tokenizers/GRAPHQL.php +++ b/PHP_CodeSniffer/Tokenizers/GRAPHQL.php @@ -1,6 +1,6 @@ =7.3", "squizlabs/php_codesniffer": "^3.6", diff --git a/composer.lock b/composer.lock index 025e27ee..f9b3efca 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9f04a53a3b02845918c714a149ab00b2", + "content-hash": "2c33aea65b2ef629308fd92cad58dfad", "packages": [ { "name": "squizlabs/php_codesniffer",