diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index 27ac0dfd..d981b1f8 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], '&'), @@ -567,13 +582,17 @@ 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 ($methodName !== '__construct' + && !$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr) + ) { $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); @@ -606,6 +625,7 @@ public function process(File $phpcsFile, $stackPtr) } } } + $this->validateMethodParameterAnnotations( $stackPtr, $paramDefinitions, @@ -643,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], @@ -662,6 +685,7 @@ private function validateFormattingConsistency( * * @param array $argumentPositions * @param array $commentPositions + * * @return bool */ private function allParamsAligned(array $argumentPositions, array $commentPositions): bool @@ -676,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 @@ -687,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; } 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; + } +}