From c9c072fdb244190c42abcfa36274aa3be9792300 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 20 Aug 2021 12:44:19 +0200 Subject: [PATCH 01/10] Prevent ImportsFromTestNamespaceSniff from hanging when group use declaration has trailing coma. If a grouped use statement has a trailing coma like this: ``` use Magento\Something{ Foo, Bar, }; ``` `findNext` on line 59 was returning false and next call was starting to search again from line 1 (`$next + 1`) and this function was looping indefinitely through input tokens. --- .../Namespaces/ImportsFromTestNamespaceSniff.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php index 3e5dbce7..1266c0dc 100644 --- a/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php +++ b/Magento2/Sniffs/Namespaces/ImportsFromTestNamespaceSniff.php @@ -57,11 +57,13 @@ public function process(File $phpcsFile, $stackPtr) $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1)); do { $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true); - $groupedAsContent = $baseUse. $tokens[$next]['content']; - $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); - if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) { - $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); - return; + if ($next !== false) { + $groupedAsContent = $baseUse. $tokens[$next]['content']; + $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly); + if (strpos($groupedAsContent, $this->prohibitNamespace) !== false) { + $phpcsFile->addWarning($this->warningMessage, $stackPtr, $this->warningCode); + return; + } } } while ($next !== false); } From 696c3aae04492499c249c4c0340bd0d64021d742 Mon Sep 17 00:00:00 2001 From: Franciszek Szczepan Wawrzak Date: Fri, 20 Aug 2021 14:26:06 +0200 Subject: [PATCH 02/10] add test case for #220 --- Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc index 50d339a1..b3ca9957 100644 --- a/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc +++ b/Magento2/Tests/Namespaces/ImportsFromTestNamespaceUnitTest.inc @@ -3,4 +3,5 @@ use Magento\Tests; use Magento\Foo\Tests as FakeTest; use Magento\Real\Classes; use \Magento\{Tests\String, Tests\Int}; -use \Magento\{Foo\string, Bar\float}; \ No newline at end of file +use \Magento\{Foo\string, Bar\float}; +use \Foo\{Trailing, Space,}; \ No newline at end of file From 98edd6adbcd6e176ebda04959a72c75ce57b0219 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Mon, 23 Aug 2021 15:35:25 +0200 Subject: [PATCH 03/10] AC-957: Create unit test for Magento2\Html\HtmlDirectiveSniff check --- Magento2/Sniffs/Html/HtmlDirectiveSniff.php | 8 +- ...itTest.1.inc => HtmlBindingUnitTest.1.inc} | 0 .../Tests/Html/HtmlDirectiveUnitTest.1.inc | 117 ++++++++++++++++++ .../Tests/Html/HtmlDirectiveUnitTest.2.inc | 12 ++ Magento2/Tests/Html/HtmlDirectiveUnitTest.php | 33 +++++ 5 files changed, 166 insertions(+), 4 deletions(-) rename Magento2/Tests/Html/{HtmlBindingSniffUnitTest.1.inc => HtmlBindingUnitTest.1.inc} (100%) create mode 100644 Magento2/Tests/Html/HtmlDirectiveUnitTest.1.inc create mode 100644 Magento2/Tests/Html/HtmlDirectiveUnitTest.2.inc create mode 100644 Magento2/Tests/Html/HtmlDirectiveUnitTest.php diff --git a/Magento2/Sniffs/Html/HtmlDirectiveSniff.php b/Magento2/Sniffs/Html/HtmlDirectiveSniff.php index 62267ec1..d140f96c 100644 --- a/Magento2/Sniffs/Html/HtmlDirectiveSniff.php +++ b/Magento2/Sniffs/Html/HtmlDirectiveSniff.php @@ -200,7 +200,7 @@ private function validateVariableUsage(File $phpcsFile, string $body): void 'Template directives may not invoke methods. Only scalar array access is allowed.' . PHP_EOL . 'Found "' . trim($body) . '"', null, - 'HtmlTemplates.DirectiveUsage.ProhibitedMethodCall' + 'HtmlTemplatesProhibitedMethodCall' ); } } @@ -224,7 +224,7 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText) $phpcsFile->addError( 'Template @vars comment block contains invalid JSON.', null, - 'HtmlTemplates.DirectiveUsage.InvalidVarsJSON' + 'HtmlTemplatesInvalidVarsJSON' ); return; } @@ -235,7 +235,7 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText) 'Template @vars comment block contains invalid label.' . PHP_EOL . 'Label for variable "' . $var . '" is empty.', null, - 'HtmlTemplates.DirectiveUsage.InvalidVariableLabel' + 'HtmlTemplatesInvalidVariableLabel' ); } } @@ -254,7 +254,7 @@ private function validateDefinedVariables(File $phpcsFile, string $templateText) 'Template @vars comment block is missing a variable used in the template.' . PHP_EOL . 'Missing variable: ' . $undefinedVariable, null, - 'HtmlTemplates.DirectiveUsage.UndefinedVariable' + 'HtmlTemplatesUndefinedVariable' ); } } diff --git a/Magento2/Tests/Html/HtmlBindingSniffUnitTest.1.inc b/Magento2/Tests/Html/HtmlBindingUnitTest.1.inc similarity index 100% rename from Magento2/Tests/Html/HtmlBindingSniffUnitTest.1.inc rename to Magento2/Tests/Html/HtmlBindingUnitTest.1.inc diff --git a/Magento2/Tests/Html/HtmlDirectiveUnitTest.1.inc b/Magento2/Tests/Html/HtmlDirectiveUnitTest.1.inc new file mode 100644 index 00000000..250b162f --- /dev/null +++ b/Magento2/Tests/Html/HtmlDirectiveUnitTest.1.inc @@ -0,0 +1,117 @@ + + + +
{{var foo.good}}
+
{{var foo.good|stillfine}}
+
{{var foo.bad()}}
+
{{var foo.bad()|alsobad}}
+
{{var foo.getGood()}}
+
{{var foo.foo.getGood()}}
+
{{var foo.getGood().fine}}
+
{{var foo.getGood().fine|alsofine}}
+
{{var foo.bad($bad.param())}}
+
{{var foo.bad.baz()}}
+
{{var foo.undeclared.baz}}
+
{{trans "foo %bar" bar=$foo.good.trans}}
+
{{trans "foo %bar" bar=$foo.bad.trans()}}
+
{{trans "foo %bar" bar="something"}}
+
{{trans "foo %bar" bar="something" bad="$bad.bad()" something=$undeclared.var.error}}
+
{{something " + foo %barblah + " bar="something" + }}
+
{{something " + foo %barblah + " bar="something" bad=$bad.multiline() + }}
+ +{{if foo.goodif}} +
{{var foo.goodif2}}
+{{/if}} + +{{if foo.goodif}} +
{{var foo.goodif2}}
+{{else}} +
{{var foo.goodif3}}
+{{/if}} + +{{if foo.badif().bad}} +
{{var foo.badif2()}}
+{{/if}} + +{{if foo.badif3()}} +
{{var foo.badif4()}}
+{{else}} +
{{var foo.badif5()}}
+{{/if}} + +{{depend foo.gooddepend}} +
{{var foo.gooddepend2}}
+{{/depend}} + +{{depend foo.badDepend().bad}} +
{{var foo.baddepend2()}}
+{{/depend}} + +{{for item in foo.goodFor}} +
{{var foo.goodFor}}
+
{{var foo.goodFor|stillfine}}
+
{{var foo.badFor()}}
+
{{var foo.badFor()|alsobad}}
+{{/for}} + +{{for item in foo.getGoodFor()}} +
loopy
+{{/for}} + +{{for item in foo.badForLoop()}} +
this loop has a bad variable
+{{/for}} + +{{depend iusefilterslater}} +{{var iusefilterslater|raw}} +{{/depend}} diff --git a/Magento2/Tests/Html/HtmlDirectiveUnitTest.2.inc b/Magento2/Tests/Html/HtmlDirectiveUnitTest.2.inc new file mode 100644 index 00000000..6a6b222c --- /dev/null +++ b/Magento2/Tests/Html/HtmlDirectiveUnitTest.2.inc @@ -0,0 +1,12 @@ + + + +Template content doesn't matter. The JSON is invalid. diff --git a/Magento2/Tests/Html/HtmlDirectiveUnitTest.php b/Magento2/Tests/Html/HtmlDirectiveUnitTest.php new file mode 100644 index 00000000..6d7a0034 --- /dev/null +++ b/Magento2/Tests/Html/HtmlDirectiveUnitTest.php @@ -0,0 +1,33 @@ + 20]; + } elseif ($testFile === 'HtmlDirectiveUnitTest.2.inc') { + return [1 => 1]; + } + + return []; + } + + /** + * @inheritdoc + */ + public function getWarningList() + { + return []; + } +} From 533dde2a8207587b4f2810e054a1e77112be1156 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Mon, 23 Aug 2021 15:46:15 +0200 Subject: [PATCH 04/10] AC-958: create unit test for Magento2\Annotation checks --- .../MethodAnnotationStructureUnitTest.inc | 309 ++++++++++++++++++ .../MethodAnnotationStructureUnitTest.php | 51 +++ .../Annotation/MethodArgumentsUnitTest.inc | 24 ++ .../Annotation/MethodArgumentsUnitTest.php | 30 ++ 4 files changed, 414 insertions(+) create mode 100644 Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc create mode 100644 Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php create mode 100644 Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc create mode 100644 Magento2/Tests/Annotation/MethodArgumentsUnitTest.php diff --git a/Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc b/Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc new file mode 100644 index 00000000..8124bc75 --- /dev/null +++ b/Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc @@ -0,0 +1,309 @@ +productVisibility = $productVisibility; + } + + /** + * block description + * + * {@inheritdoc} + * + * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection + * @return void + */ + public function construct(AbstractDb $collection) + { + /** @var */ + $collection->setVisibility($this->productVisibility->getVisibleInCatalogIds()); + } + + /** + * Move category + * + * + * @param int $parentId new parent category id + * + * @return $this + * @throws \Magento\Framework\Exception\LocalizedException|\Exception + */ + public function move($parentId) + { + /** + * Validate new parent category id. (category model is used for backward + * compatibility in event params) + */ + try { + $this->categoryRepository->get($parentId, $this->getStoreId()); + } + catch (NoSuchEntityException $e) { + throw new \Magento\Framework\Exception\LocalizedException( + __('Sorry, but we can\'t find the new parent category you selected.'), + $e + ); + } + return true; + } + + /** + * Block for short description + * + * This a long description {@inheritdoc} consists more lines as part of the long description + * on multi line. + * + * @param int $store + * + * + */ + public function getProductListDefaultSortBy26032($store) + { + return $store; + } + + /** + * + * + * + */ + public function getProductListDefaultSortBy2632() + { + } + + /** + * Block for short description + * + * This a long description {@inheritdoc} consists more lines as part of the long description + * on multi line. + * + * @param int $store + * + * + * + */ + public function getProductListDefaultSortBy2002($store) + { + return $store; + } + + /** + * + * block for short description + * + * @param int $store + * @return int + */ + public function getProductListDefaultSortBy3002($store) + { + return $store; + } + + /** + * Block for short description + * + * @see consists more lines as part of the long description + * on multi line. + * + * @param string $store + * @param string $foo + */ + public function getProductListDefaultSortBy12($store, $foo) + { + return $store === $foo; + } + + /** + * Block for short description + * + * {@inheritdoc} + * + * @param string $store + * @param string $foo + */ + public function getProductListDefaultSort2($store, $foo) + { + return $store === $foo; + } + + /** + * Block for short description + * + * a long description {@inheritdoc} consists more lines as part of the long description + * on multi line. + * + * @param string $store + * @param string $foo + */ + public function getProductListDefault($store, $foo) + { + return $store === $foo; + } + + /** + * Retrieve custom options + * + * @param ProductOptionInterface $productOption + * + * @return array + */ + protected function getCustomOptions(ProductOptionInterface $productOption) + { + if ($productOption + && $productOption->getExtensionAttributes() + && $productOption->getExtensionAttributes()->getCustomOptions() + ) { + return $productOption->getExtensionAttributes()->getCustomOptions(); + } + return []; + } + + /** + * This is the summary for a DocBlock. + * + * This is the description for a DocBlock. This text may contain + * multiple lines and even some _markdown_. + * * Markdown style lists function too + * * Just try this out once + * The section after the description contains the tags; which provide + * structured meta-data concerning the given element. + * + * @param int $example This is an example function/method parameter description. + * @param string $example2 This is a second example. + * + */ + public function getProductListDefaultSortBy2($example, $example2) + { + return $example === $example2; + } + + /** + * Returns the content of the tokens from the specified start position in + * the token stack for the specified length. + * + * @param int $start + * @param int $length + * + * @return string The token contents. + */ + public function getProductListDefaultSortBy($start, $length) + { + return $start === $length; + } + + /** + * Some text about this step/method returns the content of the tokens the token stack for the specified length + * + * @param string $name + * @param string $folder + * + * @see this file + * @When I create a file called :name in :folder + */ + public function getProductListDefaultSortBy222($name, $folder) + { + return $name === $folder; + } + + public function setExtensionAs(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) + { + return $this->_setExtensionAttributes($extensionAttributes); + } + + /** + * + * short description + * @param \Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes + * @return mixed + */ + public function setEn(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) + { + return $this->_setExtensionAttributes($extensionAttributes); + } + + /** + * @param \Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes + * @return mixed + */ + public function setExtenw(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) + { + return $this->_setExtensionAttributes($extensionAttributes); + } + + /** + * + * Short description + * @param \Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes + * @return mixed + */ + public function setExff(\Magento\Catalog\Api\Data\CategoryExtensionInterface $extensionAttributes) + { + return $this->_setExtensionAttributes($extensionAttributes); + } + + /** + * {@inheritdoc} + * + * @param int $start + * @param int $length + * + * @return string The token contents. + */ + public function getProductSortBy($start, $length) + { + return $start === $length; + } +} diff --git a/Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php b/Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php new file mode 100644 index 00000000..8c2ac4a1 --- /dev/null +++ b/Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php @@ -0,0 +1,51 @@ + 1, + 18 => 1, + 30 => 1, + 36 => 1, + 45 => 2, + 47 => 1, + 55 => 1, + 63 => 1, + 80 => 1, + 112 => 1, + 118 => 1, + 137 => 1, + 145 => 2, + 185 => 1, + 227 => 1, + 235 => 1, + 268 => 2, + 269 => 1, + 277 => 1, + 278 => 1, + 288 => 1, + 289 => 1, + 298 => 1 + ]; + } + + /** + * @inheritdoc + */ + public function getWarningList() + { + return []; + } +} diff --git a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc new file mode 100644 index 00000000..f3cbbb54 --- /dev/null +++ b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc @@ -0,0 +1,24 @@ +_setExtensionAttributes($extensionAttributes); +} diff --git a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php new file mode 100644 index 00000000..623c7539 --- /dev/null +++ b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php @@ -0,0 +1,30 @@ + 1, + 21 => 1 + ]; + } + + /** + * @inheritdoc + */ + public function getWarningList() + { + return []; + } +} From 38f32af3f77a3af999e5258d6f4e6dcea92e7c27 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Tue, 24 Aug 2021 12:55:40 +0100 Subject: [PATCH 05/10] AC-958: Corrected error reporting where fix is not provided --- .../Annotation/AnnotationFormatValidator.php | 26 +++++++++---------- .../Annotation/MethodArgumentsSniff.php | 24 ++++++++--------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Magento2/Sniffs/Annotation/AnnotationFormatValidator.php b/Magento2/Sniffs/Annotation/AnnotationFormatValidator.php index 220d1a19..b69d2ad6 100644 --- a/Magento2/Sniffs/Annotation/AnnotationFormatValidator.php +++ b/Magento2/Sniffs/Annotation/AnnotationFormatValidator.php @@ -64,7 +64,7 @@ private function validateMultiLinesInShortDescription( && $tokens[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG ) { $error = 'Short description should not be in multi lines'; - $phpcsFile->addFixableError($error, $shortPtrEnd + 1, 'MethodAnnotation'); + $phpcsFile->addError($error, $shortPtrEnd + 1, 'MethodAnnotation'); } } @@ -95,7 +95,7 @@ private function validateSpacingBetweenShortAndLongDescriptions( && $tokens[$shortPtrEnd]['code'] !== T_DOC_COMMENT_TAG ) { $error = 'There must be exactly one blank line between lines short and long descriptions'; - $phpcsFile->addFixableError($error, $shortPtrEnd + 1, 'MethodAnnotation'); + $phpcsFile->addError($error, $shortPtrEnd + 1, 'MethodAnnotation'); } if ($shortPtrEnd != $shortPtr) { $this->validateLongDescriptionFormat($phpcsFile, $shortPtrEnd, $commentEndPtr, $emptyTypeTokens); @@ -123,16 +123,16 @@ private function validateShortDescriptionFormat( $tokens = $phpcsFile->getTokens(); if ($tokens[$shortPtr]['line'] !== $tokens[$stackPtr]['line'] + 1) { $error = 'No blank lines are allowed before short description'; - $phpcsFile->addFixableError($error, $shortPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $shortPtr, 'MethodAnnotation'); } if (strtolower($tokens[$shortPtr]['content']) === '{@inheritdoc}') { $error = 'If the @inheritdoc not inline it shouldn’t have braces'; - $phpcsFile->addFixableError($error, $shortPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $shortPtr, 'MethodAnnotation'); } $shortPtrContent = $tokens[$shortPtr]['content']; if (preg_match('/^\p{Ll}/u', $shortPtrContent) === 1) { $error = 'Short description must start with a capital letter'; - $phpcsFile->addFixableError($error, $shortPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $shortPtr, 'MethodAnnotation'); } $this->validateNoExtraNewLineBeforeShortDescription( $phpcsFile, @@ -171,16 +171,16 @@ private function validateLongDescriptionFormat( $longPtr = $phpcsFile->findNext($emptyTypeTokens, $shortPtrEnd + 1, $commentEndPtr - 1, true); if (strtolower($tokens[$longPtr]['content']) === '@inheritdoc') { $error = '@inheritdoc imports only short description, annotation must have long description'; - $phpcsFile->addFixableError($error, $longPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $longPtr, 'MethodAnnotation'); } if ($longPtr !== false && $tokens[$longPtr]['code'] === T_DOC_COMMENT_STRING) { if ($tokens[$longPtr]['line'] !== $tokens[$shortPtrEnd]['line'] + 2) { $error = 'There must be exactly one blank line between descriptions'; - $phpcsFile->addFixableError($error, $longPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $longPtr, 'MethodAnnotation'); } if (preg_match('/^\p{Ll}/u', $tokens[$longPtr]['content']) === 1) { $error = 'Long description must start with a capital letter'; - $phpcsFile->addFixableError($error, $longPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $longPtr, 'MethodAnnotation'); } } } @@ -203,7 +203,7 @@ public function validateTagsSpacingFormat(File $phpcsFile, int $commentStartPtr, && strtolower($commentTagPtrContent) !== '@inheritdoc' ) { $error = 'There must be exactly one blank line before tags'; - $phpcsFile->addFixableError($error, $firstTagPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $firstTagPtr, 'MethodAnnotation'); } } } @@ -240,7 +240,7 @@ public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr) if ($paramGroupId !== null && $paramGroupId !== $groupId) { $error = 'Parameter tags must be grouped together'; - $phpcsFile->addFixableError($error, $tag, 'MethodAnnotation'); + $phpcsFile->addError($error, $tag, 'MethodAnnotation'); } if ($paramGroupId === null) { $paramGroupId = $groupId; @@ -273,7 +273,7 @@ public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr) if (!$this->allTagsAligned($actualPositions) && !$this->noneTagsAligned($actualPositions, $noAlignmentPositions)) { - $phpcsFile->addFixableError( + $phpcsFile->addError( 'Tags visual alignment must be consistent', $stackPtr, 'MethodArguments' @@ -322,7 +322,7 @@ private function validateNoExtraNewLineBeforeShortDescription( $prevPtr = $phpcsFile->findPrevious($emptyTypeTokens, $commentEndPtr - 1, $commentStartPtr, true); if ($tokens[$prevPtr]['line'] < ($tokens[$commentEndPtr]['line'] - 1)) { $error = 'Additional blank lines found at end of the annotation block'; - $phpcsFile->addFixableError($error, $commentEndPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $commentEndPtr, 'MethodAnnotation'); } } @@ -351,7 +351,7 @@ public function validateDescriptionFormatStructure( && strtolower($commentTagPtrContent) !== '@inheritdoc' ) { $error = 'Missing short description'; - $phpcsFile->addFixableError($error, $commentStartPtr, 'MethodAnnotation'); + $phpcsFile->addError($error, $commentStartPtr, 'MethodAnnotation'); } else { $this->validateShortDescriptionFormat( $phpcsFile, diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index 2ebb12d4..655bd37c 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -225,7 +225,7 @@ private function validateParameterAnnotationForArgumentExists( $previousCommentClosePtr ); if ($inheritdocAnnotationWithBracesExists) { - $phpcsFile->addFixableError( + $phpcsFile->addError( '{@inheritdoc} does not import parameter annotation', $stackPtr, 'MethodArguments' @@ -233,7 +233,7 @@ private function validateParameterAnnotationForArgumentExists( } elseif ($this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) && !$inheritdocAnnotationWithoutBracesExists ) { - $phpcsFile->addFixableError( + $phpcsFile->addError( 'Missing @param for argument in method annotation', $stackPtr, 'MethodArguments' @@ -257,13 +257,13 @@ private function validateCommentBlockDoesnotHaveExtraParameterAnnotation( int $stackPtr ): void { if ($argumentsCount < $parametersCount && $argumentsCount > 0) { - $phpcsFile->addFixableError( + $phpcsFile->addError( 'Extra @param found in method annotation', $stackPtr, 'MethodArguments' ); } elseif ($argumentsCount > 0 && $argumentsCount != $parametersCount && $parametersCount != 0) { - $phpcsFile->addFixableError( + $phpcsFile->addError( '@param is not found for one or more params in method annotation', $stackPtr, 'MethodArguments' @@ -290,7 +290,7 @@ private function validateArgumentNameInParameterAnnotationExists( $parameterNames = $this->getMethodParameters($paramDefinitions); if (!in_array($methodArguments[$ptr], $parameterNames)) { $error = $methodArguments[$ptr] . ' parameter is missing in method annotation'; - $phpcsFile->addFixableError($error, $stackPtr, 'MethodArguments'); + $phpcsFile->addError($error, $stackPtr, 'MethodArguments'); } } @@ -311,7 +311,7 @@ private function validateParameterPresentInMethodSignature( array $paramPointers ): void { if (!in_array($paramDefinitionsArguments, $methodArguments)) { - $phpcsFile->addFixableError( + $phpcsFile->addError( $paramDefinitionsArguments . ' parameter is missing in method arguments signature', $paramPointers[$ptr], 'MethodArguments' @@ -340,7 +340,7 @@ private function validateParameterOrderIsCorrect( && in_array($methodArguments[$ptr], $parameterNames) ) { if ($methodArguments[$ptr] != $parameterNames[$ptr]) { - $phpcsFile->addFixableError( + $phpcsFile->addError( $methodArguments[$ptr] . ' parameter is not in order', $paramPointers[$ptr], 'MethodArguments' @@ -383,7 +383,7 @@ private function validateDuplicateAnnotationDoesnotExists( } } foreach ($duplicateParameters as $value) { - $phpcsFile->addFixableError( + $phpcsFile->addError( $value . ' duplicate found in method annotation', $stackPtr, 'MethodArguments' @@ -410,7 +410,7 @@ private function validateParameterAnnotationFormatIsCorrect( ): void { switch (count($paramDefinitions)) { case 0: - $phpcsFile->addFixableError( + $phpcsFile->addError( 'Missing both type and parameter', $paramPointers[$ptr], 'MethodArguments' @@ -427,7 +427,7 @@ private function validateParameterAnnotationFormatIsCorrect( break; case 2: if ($this->isInvalidType($paramDefinitions[0])) { - $phpcsFile->addFixableError( + $phpcsFile->addError( $paramDefinitions[0] . ' is not a valid PHP type', $paramPointers[$ptr], 'MethodArguments' @@ -449,7 +449,7 @@ private function validateParameterAnnotationFormatIsCorrect( 'MethodArguments' ); if ($this->isInvalidType($paramDefinitions[0])) { - $phpcsFile->addFixableError( + $phpcsFile->addError( $paramDefinitions[0] . ' is not a valid PHP type', $paramPointers[$ptr], 'MethodArguments' @@ -633,7 +633,7 @@ private function validateFormattingConsistency( } if (!$this->allParamsAligned($argumentPositions, $commentPositions) && !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)) { - $phpcsFile->addFixableError( + $phpcsFile->addError( 'Method arguments visual alignment must be consistent', $paramPointers[0], 'MethodArguments' From 757171f8b74c841d2e2669fa731da7bc0e7b6490 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Tue, 24 Aug 2021 13:06:50 +0100 Subject: [PATCH 06/10] Moved abstract unit test to autoload-dev --- composer.json | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index d6004a88..cc1950eb 100644 --- a/composer.json +++ b/composer.json @@ -15,16 +15,18 @@ "require-dev": { "phpunit/phpunit": "^9.5.8" }, + "autoload-dev": { + "files": [ + "PHP_CodeSniffer/Tests/Standards/AbstractSniffUnitTest.php" + ] + }, "autoload": { "classmap": [ "PHP_CodeSniffer/Tokenizers/" ], "psr-4": { "Magento2\\": "Magento2/" - }, - "files": [ - "PHP_CodeSniffer/Tests/Standards/AbstractSniffUnitTest.php" - ] + } }, "scripts": { "post-install-cmd": "vendor/bin/phpcs --config-set installed_paths ../../..", From e6a43fe26035a8a402f822b861f88f3529131db0 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 24 Aug 2021 14:40:57 +0200 Subject: [PATCH 07/10] AC-939: Create unit test for Magento2\Less\AvoidIdSniff check --- Magento2/Sniffs/Less/AvoidIdSniff.php | 1 + Magento2/Tests/Less/AvoidIdUnitTest.inc | 39 ++++++++++++++++++++ Magento2/Tests/Less/AvoidIdUnitTest.php | 48 +++++++++++++++++++++++++ Magento2/ruleset.xml | 6 ++-- 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 Magento2/Tests/Less/AvoidIdUnitTest.inc create mode 100644 Magento2/Tests/Less/AvoidIdUnitTest.php diff --git a/Magento2/Sniffs/Less/AvoidIdSniff.php b/Magento2/Sniffs/Less/AvoidIdSniff.php index d4e1b213..387b5ed8 100644 --- a/Magento2/Sniffs/Less/AvoidIdSniff.php +++ b/Magento2/Sniffs/Less/AvoidIdSniff.php @@ -51,6 +51,7 @@ class AvoidIdSniff implements Sniff T_PLUS, T_NS_SEPARATOR, T_LNUMBER, + T_BITWISE_NOT ]; /** diff --git a/Magento2/Tests/Less/AvoidIdUnitTest.inc b/Magento2/Tests/Less/AvoidIdUnitTest.inc new file mode 100644 index 00000000..070c2eec --- /dev/null +++ b/Magento2/Tests/Less/AvoidIdUnitTest.inc @@ -0,0 +1,39 @@ +// /** +// * Copyright © Magento, Inc. All rights reserved. +// * See COPYING.txt for license details. +// */ + +#foo[bar], +#foo[bar=bash], +#foo[bar~=bash], +#foo[bar$=bash], +#foo[bar*=bash], +#foo[bar|=bash], +#foo[bar='bash'], +#foo:hover, +#foo:nth-last-of-type(n), +#foo::before, +#foo + div, +#foo > div, +#foo ~ div, +#foo\3Abar ~ div, +#foo\:bar ~ div, +#foo.bar .baz, +div#foo { +blah: 'abc'; +} + +.my #foo, +#foo .blah, +.my #foo .blah { +some: 'stuff'; +} +.blah { +#bar .baz(); +.foo #bar .baz(); +#bar .baz(); + +#bar .baz; +.foo #bar .baz; +#bar .baz; +} diff --git a/Magento2/Tests/Less/AvoidIdUnitTest.php b/Magento2/Tests/Less/AvoidIdUnitTest.php new file mode 100644 index 00000000..962af35f --- /dev/null +++ b/Magento2/Tests/Less/AvoidIdUnitTest.php @@ -0,0 +1,48 @@ + 1, + 7 => 1, + 8 => 1, + 9 => 1, + 10 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 16 => 1, + 17 => 1, + 18 => 1, + 19 => 1, + 20 => 1, + 21 => 3, + 22 => 1, + 26 => 1, + 27 => 1, + 28 => 1 + ]; + } + + /** + * @inheritdoc + */ + public function getWarningList() + { + return []; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 3ea0c969..daf54da8 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -3,7 +3,7 @@ Magento Coding Standard - + @@ -590,7 +590,9 @@ *Test.php - + + *\.less$ + 0 From a9ef69fb91391df629b460adcd106dff70c511e6 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Tue, 24 Aug 2021 14:45:19 +0200 Subject: [PATCH 08/10] AC-939: Create unit test for Magento2\Less\AvoidIdSniff check --- Magento2/Tests/Less/{AvoidIdUnitTest.inc => AvoidIdUnitTest.less} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Magento2/Tests/Less/{AvoidIdUnitTest.inc => AvoidIdUnitTest.less} (100%) diff --git a/Magento2/Tests/Less/AvoidIdUnitTest.inc b/Magento2/Tests/Less/AvoidIdUnitTest.less similarity index 100% rename from Magento2/Tests/Less/AvoidIdUnitTest.inc rename to Magento2/Tests/Less/AvoidIdUnitTest.less From e538e6f9fac9eaabba920c70c1ed4bfdbeadbbb4 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Wed, 25 Aug 2021 12:39:03 +0200 Subject: [PATCH 09/10] AC-939: Create unit test for Magento2\Less\AvoidIdSniff check --- .../Less/AbstractLessSniffUnitTestCase.php | 33 +++++++++++++++++++ Magento2/Tests/Less/AvoidIdUnitTest.php | 6 ++-- Magento2/ruleset.xml | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php diff --git a/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php b/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php new file mode 100644 index 00000000..71edcbee --- /dev/null +++ b/Magento2/Tests/Less/AbstractLessSniffUnitTestCase.php @@ -0,0 +1,33 @@ +extensions = array_merge( + $config->extensions, + [ + 'less' => 'CSS' + ] + ); + + $GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config; + } +} diff --git a/Magento2/Tests/Less/AvoidIdUnitTest.php b/Magento2/Tests/Less/AvoidIdUnitTest.php index 962af35f..33012879 100644 --- a/Magento2/Tests/Less/AvoidIdUnitTest.php +++ b/Magento2/Tests/Less/AvoidIdUnitTest.php @@ -5,9 +5,7 @@ */ namespace Magento2\Tests\Less; -use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; - -class AvoidIdUnitTest extends AbstractSniffUnitTest +class AvoidIdUnitTest extends AbstractLessSniffUnitTestCase { /** * @inheritdoc @@ -30,7 +28,7 @@ public function getErrorList() 18 => 1, 19 => 1, 20 => 1, - 21 => 3, + 21 => 1, 22 => 1, 26 => 1, 27 => 1, diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index daf54da8..204946d0 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -3,7 +3,7 @@ Magento Coding Standard - + From b1c005c3f5ed687f149c9b0f285818c2123dd6de Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Wed, 25 Aug 2021 11:47:31 +0100 Subject: [PATCH 10/10] Version 8 --- composer.json | 2 +- composer.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index cc1950eb..f30fd7fd 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "AFL-3.0" ], "type": "phpcodesniffer-standard", - "version": "7", + "version": "8", "require": { "php": ">=7.3", "squizlabs/php_codesniffer": "^3.6", diff --git a/composer.lock b/composer.lock index 72a2ad6a..c26f9e74 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": "1ed3638000f0e172d4e01c45333fcfb6", + "content-hash": "93bec1b3a36cf67f2511aec0219bcc06", "packages": [ { "name": "squizlabs/php_codesniffer",