Skip to content

Commit

Permalink
Fixed bug #2530 : PEAR.Commenting.FunctionComment does not support in…
Browse files Browse the repository at this point in the history
…tersection types in comments
  • Loading branch information
gsherwood committed Oct 10, 2019
1 parent f5dc023 commit f797a35
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Boolean operators can now appear in the middle of the line
- PSR12.Files.FileHeader no longer ignores comments preceding a use, namespace, or declare statement
- PSR12.Files.FileHeader now allows a hashbang line at the top of the file
- Fixed bug #2530 : PEAR.Commenting.FunctionComment does not support intersection types in comments
- Fixed bug #2615 : Constant visibility false positive on non-class constants
- Fixed bug #2616 : PSR12.Files.FileHeader false positive when file only contains docblock
- Fixed bug #2619 : PSR12.Files.FileHeader locks up when inline comment is the last content in a file
Expand Down
64 changes: 33 additions & 31 deletions src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)

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]);
Expand Down Expand Up @@ -297,43 +297,45 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)

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

// Check number of spaces after the type.
$spaces = ($maxType - strlen($param['type']) + 1);
if ($param['type_space'] !== $spaces) {
$error = 'Expected %s spaces after parameter type; %s found';
$data = [
$spaces,
$param['type_space'],
];
if (trim($param['type']) !== '') {
// Check number of spaces after the type.
$spaces = ($maxType - strlen($param['type']) + 1);
if ($param['type_space'] !== $spaces) {
$error = 'Expected %s spaces after parameter type; %s found';
$data = [
$spaces,
$param['type_space'],
];

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

$content = $param['type'];
$content .= str_repeat(' ', $spaces);
$content .= $param['var'];
$content .= str_repeat(' ', $param['var_space']);
$content = $param['type'];
$content .= str_repeat(' ', $spaces);
$content .= $param['var'];
$content .= str_repeat(' ', $param['var_space']);

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

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

$padding = str_repeat(' ', ($tokens[$star]['column'] - 1));
$padding .= '* ';
$padding .= str_repeat(' ', $spaceLength);
$padding = str_repeat(' ', ($tokens[$star]['column'] - 1));
$padding .= '* ';
$padding .= str_repeat(' ', $spaceLength);

$content .= wordwrap(
$param['comment'],
$wrapLength,
$phpcsFile->eolChar.$padding
);
$content .= wordwrap(
$param['comment'],
$wrapLength,
$phpcsFile->eolChar.$padding
);

$phpcsFile->fixer->replaceToken($commentToken, $content);
for ($i = ($commentToken + 1); $i <= $param['comment_end']; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($commentToken, $content);
for ($i = ($commentToken + 1); $i <= $param['comment_end']; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
}//end if
}//end if
}//end if

Expand Down
11 changes: 11 additions & 0 deletions src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,14 @@ function process(File $phpcsFile, $stackPtr)
{

}//end process()

/**
* @param (Foo&Bar)|null $a Comment.
* @param string $b Comment.
*
* @return void
*/
public function setTranslator($a, &$b): void
{
$this->translator = $translator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,14 @@ function process(File $phpcsFile, $stackPtr)
{

}//end process()

/**
* @param (Foo&Bar)|null $a Comment.
* @param string $b Comment.
*
* @return void
*/
public function setTranslator($a, &$b): void
{
$this->translator = $translator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function getErrorList()
272 => 1,
313 => 1,
317 => 1,
324 => 1,
327 => 1,
329 => 1,
332 => 1,
Expand Down

0 comments on commit f797a35

Please sign in to comment.