Skip to content

Commit c8b665b

Browse files
devfreygsherwood
andauthored
Squiz/FunctionComment: support DNF types (#944)
* Squiz/FunctionComment: support intersection types Co-authored-by: Greg Sherwood <[email protected]>
1 parent 1e54a57 commit c8b665b

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
311311
$commentLines = [];
312312
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
313313
$matches = [];
314-
preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
314+
preg_match('/((?:(?![$.]|&(?=\$)).)*)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
315315

316316
if (empty($matches) === false) {
317317
$typeLen = strlen($matches[1]);
@@ -323,7 +323,10 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
323323
}
324324
}
325325

326-
if (isset($matches[2]) === true) {
326+
if ($tokens[($tag + 2)]['content'][0] === '$') {
327+
$error = 'Missing parameter type';
328+
$phpcsFile->addError($error, $tag, 'MissingParamType');
329+
} else if (isset($matches[2]) === true) {
327330
$var = $matches[2];
328331
$varLen = strlen($var);
329332
if ($varLen > $maxVar) {
@@ -366,9 +369,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
366369
$phpcsFile->addError($error, $tag, 'MissingParamComment');
367370
$commentLines[] = ['comment' => ''];
368371
}//end if
369-
} else if ($tokens[($tag + 2)]['content'][0] === '$') {
370-
$error = 'Missing parameter type';
371-
$phpcsFile->addError($error, $tag, 'MissingParamType');
372372
} else {
373373
$error = 'Missing parameter name';
374374
$phpcsFile->addError($error, $tag, 'MissingParamName');

src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc

+9
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,12 @@ function paramVariation3($hasTypeNoComment): void {}
11581158
* @return void
11591159
*/
11601160
function paramVariation4($hasTypehasComment): void {}
1161+
1162+
/**
1163+
* @param (Foo&Bar)|null $a Comment.
1164+
* @return void
1165+
*/
1166+
public function setTranslator($a): void
1167+
{
1168+
$this->translator = $translator;
1169+
}

src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,12 @@ function paramVariation3($hasTypeNoComment): void {}
11581158
* @return void
11591159
*/
11601160
function paramVariation4($hasTypehasComment): void {}
1161+
1162+
/**
1163+
* @param (Foo&Bar)|null $a Comment.
1164+
* @return void
1165+
*/
1166+
public function setTranslator($a): void
1167+
{
1168+
$this->translator = $translator;
1169+
}

0 commit comments

Comments
 (0)