Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
danielfilipek-ug authored Aug 26, 2025
commit 7024ae2be3d3e8747d62a6e79b1df54e39aef415
30 changes: 29 additions & 1 deletion Magento2/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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++) {
Expand Down