From 24f475f5314601b0b43baa0e907f5f45270854d0 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 20 Mar 2023 21:56:44 +0000 Subject: [PATCH 1/3] Allow no comment for a constructor where needed --- Magento2/Sniffs/Annotation/MethodArgumentsSniff.php | 3 ++- Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index 27ac0dfd..72747048 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -567,7 +567,8 @@ public function process(File $phpcsFile, $stackPtr) $previousCommentOpenPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0); $previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0); if ($previousCommentClosePtr && $previousCommentOpenPtr) { - if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)) { + $methodName = $tokens[$stackPtr + 2]['content']; + if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) && $methodName !== '__construct') { $phpcsFile->addError('Comment block is missing', $stackPtr, 'NoCommentBlock'); return; } diff --git a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc index 6694c8cc..e0ca4ae0 100644 --- a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc +++ b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc @@ -51,3 +51,13 @@ public function methodWithAttributeAndWithoutDocblock(string $text): string { return $text; } + +class ConstructorCommentNotRequired +{ + private $property = false; + + public function __construct() + { + $this->property = true; + } +} From 52938c7518cb474e3f78dc865233f169d601f5a3 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 20 Mar 2023 22:06:52 +0000 Subject: [PATCH 2/3] Adhere to our own coding standards --- .../Annotation/MethodArgumentsSniff.php | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index 72747048..0608ad7a 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -1,8 +1,10 @@ addError( $value . ' duplicate found in method annotation', @@ -449,6 +463,7 @@ private function validateParameterAnnotationFormatIsCorrect( 'NotValidType' ); } + $this->validateParameterPresentInMethodSignature( $ptr, ltrim($paramDefinitions[1], '&'), @@ -568,13 +583,16 @@ public function process(File $phpcsFile, $stackPtr) $previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0); if ($previousCommentClosePtr && $previousCommentOpenPtr) { $methodName = $tokens[$stackPtr + 2]['content']; - if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) && $methodName !== '__construct') { + if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) + && $methodName !== '__construct' + ) { $phpcsFile->addError('Comment block is missing', $stackPtr, 'NoCommentBlock'); return; } } else { return; } + $openParenthesisPtr = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1, $numTokens); $closedParenthesisPtr = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr + 1, $numTokens); $methodArguments = $this->getMethodArguments($phpcsFile, $openParenthesisPtr, $closedParenthesisPtr); @@ -607,6 +625,7 @@ public function process(File $phpcsFile, $stackPtr) } } } + $this->validateMethodParameterAnnotations( $stackPtr, $paramDefinitions, @@ -644,12 +663,15 @@ private function validateFormattingConsistency( if (isset($paramDefinition['paramName'])) { $argumentPositions[] = strpos($paramContent, $paramDefinition['paramName']); } + $commentPositions[] = $paramDefinition['comment'] ? strrpos($paramContent, $paramDefinition['comment']) : null; } } + if (!$this->allParamsAligned($argumentPositions, $commentPositions) - && !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions)) { + && !$this->noneParamsAligned($argumentPositions, $commentPositions, $paramDefinitions) + ) { $phpcsFile->addError( 'Method arguments visual alignment must be consistent', $paramPointers[0], @@ -663,6 +685,7 @@ private function validateFormattingConsistency( * * @param array $argumentPositions * @param array $commentPositions + * * @return bool */ private function allParamsAligned(array $argumentPositions, array $commentPositions): bool @@ -677,6 +700,7 @@ private function allParamsAligned(array $argumentPositions, array $commentPositi * @param array $argumentPositions * @param array $commentPositions * @param array $paramDefinitions + * * @return bool */ private function noneParamsAligned(array $argumentPositions, array $commentPositions, array $paramDefinitions): bool @@ -688,9 +712,11 @@ private function noneParamsAligned(array $argumentPositions, array $commentPosit if ($type === null) { continue; } + $paramName = $paramDefinitions[$index]['paramName']; if (($argumentPosition !== strlen($type) + 1) || - (isset($commentPosition) && ($commentPosition !== $argumentPosition + strlen($paramName) + 1))) { + (isset($commentPosition) && ($commentPosition !== $argumentPosition + strlen($paramName) + 1)) + ) { $flag = false; break; } From a2e71f0f82ca77d4bd159f6b4024117b931eb10b Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 20 Mar 2023 22:09:43 +0000 Subject: [PATCH 3/3] Run inexpensive check first --- Magento2/Sniffs/Annotation/MethodArgumentsSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index 0608ad7a..d981b1f8 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -583,8 +583,8 @@ public function process(File $phpcsFile, $stackPtr) $previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0); if ($previousCommentClosePtr && $previousCommentOpenPtr) { $methodName = $tokens[$stackPtr + 2]['content']; - if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) - && $methodName !== '__construct' + if ($methodName !== '__construct' + && !$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) ) { $phpcsFile->addError('Comment block is missing', $stackPtr, 'NoCommentBlock'); return;