From 4a3fcfa91a038c899023e00265a4536aafbb6f6c Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 4 Dec 2023 17:16:54 +0000 Subject: [PATCH 1/2] Squiz/FunctionComment: specify more specific error code for missing type --- .../Commenting/FunctionCommentSniff.php | 3 +++ .../Commenting/FunctionCommentUnitTest.inc | 24 +++++++++++++++++++ .../FunctionCommentUnitTest.inc.fixed | 24 +++++++++++++++++++ .../Commenting/FunctionCommentUnitTest.php | 9 +++++++ 4 files changed, 60 insertions(+) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 644fd50c7c..d7a292bdce 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -366,6 +366,9 @@ 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 a44f5e0e2e..4fcbb6dbe0 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc @@ -1134,3 +1134,27 @@ public function variableOrderMismatch($bar, $baz, $foo) { * @return never */ function foo() {} + +/** + * @param $noTypeNoComment + * @return void + */ +function paramVariation1($noTypeNoComment): void {} + +/** + * @param $noTypeWithComment This parameter has no type specified. + * @return void + */ +function paramVariation2($noTypeWithComment): void {} + +/** + * @param integer $hasTypeNoComment + * @return void + */ +function paramVariation3($hasTypeNoComment): void {} + +/** + * @param integer $hasTypehasComment This parameter has type. + * @return void + */ +function paramVariation4($hasTypehasComment): void {} diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed index 3d2e10fd6a..817630b5ba 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed @@ -1134,3 +1134,27 @@ public function variableOrderMismatch($bar, $baz, $foo) { * @return never */ function foo() {} + +/** + * @param $noTypeNoComment + * @return void + */ +function paramVariation1($noTypeNoComment): void {} + +/** + * @param $noTypeWithComment This parameter has no type specified. + * @return void + */ +function paramVariation2($noTypeWithComment): void {} + +/** + * @param integer $hasTypeNoComment + * @return void + */ +function paramVariation3($hasTypeNoComment): void {} + +/** + * @param integer $hasTypehasComment This parameter has type. + * @return void + */ +function paramVariation4($hasTypehasComment): void {} diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php index 0d89db9e5d..a4f4ae2c0b 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php @@ -137,6 +137,13 @@ public function getErrorList() 1123 => 1, 1124 => 1, 1125 => 1, + 1138 => 1, + 1139 => 1, + 1142 => 1, + 1144 => 1, + 1145 => 1, + 1148 => 1, + 1151 => 1, ]; // Scalar type hints only work from PHP 7 onwards. @@ -156,6 +163,8 @@ public function getErrorList() $errors[1089] = 3; $errors[1107] = 8; $errors[1129] = 3; + $errors[1154] = 1; + $errors[1160] = 1; } else { $errors[729] = 4; $errors[740] = 2; From bf88611028a56a91ed1c5ec62055150bc4e0d688 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 4 Dec 2023 17:20:24 +0000 Subject: [PATCH 2/2] Squiz/FunctionComment: no longer suggest incorrect type hint --- src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php | 2 +- .../Squiz/Tests/Commenting/FunctionCommentUnitTest.php | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index d7a292bdce..672818337c 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -463,7 +463,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart) } } - if ($suggestedTypeHint !== '' && isset($realParams[$pos]) === true) { + if ($suggestedTypeHint !== '' && isset($realParams[$pos]) === true && $param['var'] !== '') { $typeHint = $realParams[$pos]['type_hint']; // Remove namespace prefixes when comparing. diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php index a4f4ae2c0b..036bdc622b 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php @@ -107,7 +107,6 @@ public function getErrorList() 792 => 1, 794 => 1, 797 => 1, - 801 => 1, 828 => 1, 840 => 1, 852 => 1, @@ -139,10 +138,8 @@ public function getErrorList() 1125 => 1, 1138 => 1, 1139 => 1, - 1142 => 1, 1144 => 1, 1145 => 1, - 1148 => 1, 1151 => 1, ];