From 5f4e5d52563f1499575131a25cbc19bef37007dd Mon Sep 17 00:00:00 2001 From: Jeffrey Angenent <1571879+devfrey@users.noreply.github.com> Date: Tue, 8 Apr 2025 11:00:07 +0200 Subject: [PATCH 1/2] Squiz/FunctionComment: support intersection types Co-authored-by: Greg Sherwood --- .../Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 10 +++++----- .../Squiz/Tests/Commenting/FunctionCommentUnitTest.inc | 9 +++++++++ .../Tests/Commenting/FunctionCommentUnitTest.inc.fixed | 9 +++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index b0b7cafb32..e3c4310907 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -311,7 +311,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) $commentLines = []; if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) { $matches = []; - preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); + preg_match('/((?:(?![$.]|&(?=\$)).)*)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches); if (empty($matches) === false) { $typeLen = strlen($matches[1]); @@ -323,7 +323,10 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) } } - if (isset($matches[2]) === true) { + if ($type === '') { + $error = 'Missing parameter type'; + $phpcsFile->addError($error, $tag, 'MissingParamType'); + } else if (isset($matches[2]) === true) { $var = $matches[2]; $varLen = strlen($var); if ($varLen > $maxVar) { @@ -366,9 +369,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) $phpcsFile->addError($error, $tag, 'MissingParamComment'); $commentLines[] = ['comment' => '']; }//end if - } else if ($tokens[($tag + 2)]['content'][0] === '$') { - $error = 'Missing parameter type'; - $phpcsFile->addError($error, $tag, 'MissingParamType'); } else { $error = 'Missing parameter name'; $phpcsFile->addError($error, $tag, 'MissingParamName'); diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc index 4fcbb6dbe0..79060f4fe7 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc @@ -1158,3 +1158,12 @@ function paramVariation3($hasTypeNoComment): void {} * @return void */ function paramVariation4($hasTypehasComment): void {} + +/** + * @param (Foo&Bar)|null $a Comment. + * @return void + */ +public function setTranslator($a): void +{ + $this->translator = $translator; +} diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed index 817630b5ba..76fa862ab0 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed @@ -1158,3 +1158,12 @@ function paramVariation3($hasTypeNoComment): void {} * @return void */ function paramVariation4($hasTypehasComment): void {} + +/** + * @param (Foo&Bar)|null $a Comment. + * @return void + */ +public function setTranslator($a): void +{ + $this->translator = $translator; +} From 1b82ce8718e17bb14cb3c995e314282e2966b3ac Mon Sep 17 00:00:00 2001 From: Jeffrey Angenent <1571879+devfrey@users.noreply.github.com> Date: Wed, 9 Apr 2025 18:32:38 +0200 Subject: [PATCH 2/2] Preserve existing behaviour Co-authored-by: Juliette <663378+jrfnl@users.noreply.github.com> --- src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index e3c4310907..3531758a4d 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -323,7 +323,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) } } - if ($type === '') { + if ($tokens[($tag + 2)]['content'][0] === '$') { $error = 'Missing parameter type'; $phpcsFile->addError($error, $tag, 'MissingParamType'); } else if (isset($matches[2]) === true) {