Skip to content

Commit f797a35

Browse files
committed
Fixed bug #2530 : PEAR.Commenting.FunctionComment does not support intersection types in comments
1 parent f5dc023 commit f797a35

File tree

5 files changed

+56
-32
lines changed

5 files changed

+56
-32
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3939
-- Boolean operators can now appear in the middle of the line
4040
- PSR12.Files.FileHeader no longer ignores comments preceding a use, namespace, or declare statement
4141
- PSR12.Files.FileHeader now allows a hashbang line at the top of the file
42+
- Fixed bug #2530 : PEAR.Commenting.FunctionComment does not support intersection types in comments
4243
- Fixed bug #2615 : Constant visibility false positive on non-class constants
4344
- Fixed bug #2616 : PSR12.Files.FileHeader false positive when file only contains docblock
4445
- Fixed bug #2619 : PSR12.Files.FileHeader locks up when inline comment is the last content in a file

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

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
217217

218218
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
219219
$matches = [];
220-
preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
220+
preg_match('/((?:(?![$.]|&(?=\$)).)*)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
221221

222222
if (empty($matches) === false) {
223223
$typeLen = strlen($matches[1]);
@@ -297,43 +297,45 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
297297

298298
$foundParams[] = $param['var'];
299299

300-
// Check number of spaces after the type.
301-
$spaces = ($maxType - strlen($param['type']) + 1);
302-
if ($param['type_space'] !== $spaces) {
303-
$error = 'Expected %s spaces after parameter type; %s found';
304-
$data = [
305-
$spaces,
306-
$param['type_space'],
307-
];
300+
if (trim($param['type']) !== '') {
301+
// Check number of spaces after the type.
302+
$spaces = ($maxType - strlen($param['type']) + 1);
303+
if ($param['type_space'] !== $spaces) {
304+
$error = 'Expected %s spaces after parameter type; %s found';
305+
$data = [
306+
$spaces,
307+
$param['type_space'],
308+
];
308309

309-
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
310-
if ($fix === true) {
311-
$commentToken = ($param['tag'] + 2);
310+
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
311+
if ($fix === true) {
312+
$commentToken = ($param['tag'] + 2);
312313

313-
$content = $param['type'];
314-
$content .= str_repeat(' ', $spaces);
315-
$content .= $param['var'];
316-
$content .= str_repeat(' ', $param['var_space']);
314+
$content = $param['type'];
315+
$content .= str_repeat(' ', $spaces);
316+
$content .= $param['var'];
317+
$content .= str_repeat(' ', $param['var_space']);
317318

318-
$wrapLength = ($tokens[$commentToken]['length'] - $param['type_space'] - $param['var_space'] - strlen($param['type']) - strlen($param['var']));
319+
$wrapLength = ($tokens[$commentToken]['length'] - $param['type_space'] - $param['var_space'] - strlen($param['type']) - strlen($param['var']));
319320

320-
$star = $phpcsFile->findPrevious(T_DOC_COMMENT_STAR, $param['tag']);
321-
$spaceLength = (strlen($content) + $tokens[($commentToken - 1)]['length'] + $tokens[($commentToken - 2)]['length']);
321+
$star = $phpcsFile->findPrevious(T_DOC_COMMENT_STAR, $param['tag']);
322+
$spaceLength = (strlen($content) + $tokens[($commentToken - 1)]['length'] + $tokens[($commentToken - 2)]['length']);
322323

323-
$padding = str_repeat(' ', ($tokens[$star]['column'] - 1));
324-
$padding .= '* ';
325-
$padding .= str_repeat(' ', $spaceLength);
324+
$padding = str_repeat(' ', ($tokens[$star]['column'] - 1));
325+
$padding .= '* ';
326+
$padding .= str_repeat(' ', $spaceLength);
326327

327-
$content .= wordwrap(
328-
$param['comment'],
329-
$wrapLength,
330-
$phpcsFile->eolChar.$padding
331-
);
328+
$content .= wordwrap(
329+
$param['comment'],
330+
$wrapLength,
331+
$phpcsFile->eolChar.$padding
332+
);
332333

333-
$phpcsFile->fixer->replaceToken($commentToken, $content);
334-
for ($i = ($commentToken + 1); $i <= $param['comment_end']; $i++) {
335-
$phpcsFile->fixer->replaceToken($i, '');
336-
}
334+
$phpcsFile->fixer->replaceToken($commentToken, $content);
335+
for ($i = ($commentToken + 1); $i <= $param['comment_end']; $i++) {
336+
$phpcsFile->fixer->replaceToken($i, '');
337+
}
338+
}//end if
337339
}//end if
338340
}//end if
339341

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,14 @@ function process(File $phpcsFile, $stackPtr)
370370
{
371371

372372
}//end process()
373+
374+
/**
375+
* @param (Foo&Bar)|null $a Comment.
376+
* @param string $b Comment.
377+
*
378+
* @return void
379+
*/
380+
public function setTranslator($a, &$b): void
381+
{
382+
$this->translator = $translator;
383+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,14 @@ function process(File $phpcsFile, $stackPtr)
370370
{
371371

372372
}//end process()
373+
374+
/**
375+
* @param (Foo&Bar)|null $a Comment.
376+
* @param string $b Comment.
377+
*
378+
* @return void
379+
*/
380+
public function setTranslator($a, &$b): void
381+
{
382+
$this->translator = $translator;
383+
}

src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public function getErrorList()
5757
272 => 1,
5858
313 => 1,
5959
317 => 1,
60-
324 => 1,
6160
327 => 1,
6261
329 => 1,
6362
332 => 1,

0 commit comments

Comments
 (0)