From 7024ae2be3d3e8747d62a6e79b1df54e39aef415 Mon Sep 17 00:00:00 2001 From: danielfilipek-ug <155445611+danielfilipek-ug@users.noreply.github.com> Date: Tue, 26 Aug 2025 11:24:46 +0200 Subject: [PATCH] Update MethodArgumentsSniff to allow PHP Attributes The getMethodArguments method in Magento Coding Standard was incorrectly counting PHP constructor arguments when PHP attributes (e.g., #[SerializedName('created_at')]) were present. The sniffer would stop counting parameters after encountering an attribute, resulting in incorrect code validations and false errors in the quality control system. --- .../Annotation/MethodArgumentsSniff.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index 830984cd..ba29a31d 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -105,6 +105,34 @@ private function isInvalidType(string $type): bool return in_array(strtolower($type), $this->invalidTypes); } + /** + * Find matching closing parenthesis for given opening parenthesis + * @param File $phpcsFile + * @param int $openParenthesisPtr + * @param int $numTokens + * @return int + */ + private function findMatchingParenthesis(File $phpcsFile, int $openParenthesisPtr, int $numTokens): int + { + $tokens = $phpcsFile->getTokens(); + $parenthesisCount = 1; + + for ($i = $openParenthesisPtr + 1; $i < $numTokens; $i++) { + $tokenCode = $tokens[$i]['code']; + + if ($tokenCode === T_OPEN_PARENTHESIS) { + $parenthesisCount++; + } elseif ($tokenCode === T_CLOSE_PARENTHESIS) { + $parenthesisCount--; + if ($parenthesisCount === 0) { + return $i; + } + } + } + + return $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $openParenthesisPtr + 1, $numTokens); + } + /** * Get arguments from method signature * @@ -575,7 +603,7 @@ public function process(File $phpcsFile, $stackPtr) return; } $openParenthesisPtr = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1, $numTokens); - $closedParenthesisPtr = $phpcsFile->findNext(T_CLOSE_PARENTHESIS, $stackPtr + 1, $numTokens); + $closedParenthesisPtr = $this->findMatchingParenthesis($phpcsFile, $openParenthesisPtr, $numTokens); $methodArguments = $this->getMethodArguments($phpcsFile, $openParenthesisPtr, $closedParenthesisPtr); $paramPointers = $paramDefinitions = []; for ($tempPtr = $previousCommentOpenPtr; $tempPtr < $previousCommentClosePtr; $tempPtr++) {