Skip to content

Commit

Permalink
PEAR.Commenting.FunctionComment fixers now correctly handle multi-lin…
Browse files Browse the repository at this point in the history
…e param comments
  • Loading branch information
gsherwood committed Jan 17, 2017
1 parent 521692b commit 7e92909
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
continue;
}

$type = '';
$typeSpace = 0;
$var = '';
$varSpace = 0;
$comment = '';
$type = '';
$typeSpace = 0;
$var = '';
$varSpace = 0;
$comment = '';
$commentEnd = 0;
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
$matches = array();
preg_match('/([^$&.]+)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
Expand Down Expand Up @@ -257,13 +258,14 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co

for ($i = ($tag + 3); $i < $end; $i++) {
if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
$comment .= ' '.$tokens[$i]['content'];
$comment .= ' '.$tokens[$i]['content'];
$commentEnd = $i;
}
}
} else {
$error = 'Missing parameter comment';
$phpcsFile->addError($error, $tag, 'MissingParamComment');
}
}//end if
} else {
$error = 'Missing parameter name';
$phpcsFile->addError($error, $tag, 'MissingParamName');
Expand All @@ -274,12 +276,13 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
}//end if

$params[] = array(
'tag' => $tag,
'type' => $type,
'var' => $var,
'comment' => $comment,
'type_space' => $typeSpace,
'var_space' => $varSpace,
'tag' => $tag,
'type' => $type,
'var' => $var,
'comment' => $comment,
'comment_end' => $commentEnd,
'type_space' => $typeSpace,
'var_space' => $varSpace,
);
}//end foreach

Expand Down Expand Up @@ -312,14 +315,36 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co

$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['comment'];
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
}
}

$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']
);

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

$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, '');
}
}//end if
}//end if

// Make sure the param name is correct.
if (isset($realParams[$pos]) === true) {
Expand Down Expand Up @@ -362,14 +387,36 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co

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

$content = $param['type'];
$content .= str_repeat(' ', $param['type_space']);
$content .= $param['var'];
$content .= str_repeat(' ', $spaces);
$content .= $param['comment'];
$phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
}
}

$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']
);

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

$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, '');
}
}//end if
}//end if
}//end foreach

$realNames = array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,20 @@ public function myFunction(string $name1, string ...$name2) {
*/
function foo($bar) {
}

/**
* Processes the test.
*
* @param PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token occurred.
* @param int $stackPtr The position in the tokens stack
* where the listening token type
* was found.
*
* @return void
* @see register()
*/
function process(File $phpcsFile, $stackPtr)
{

}//end process()
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function getErrorList()
327 => 1,
329 => 1,
332 => 1,
344 => 1,
);

}//end getErrorList()
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- The return array now contains a new "nullable_type" index set to true or false for each method param
-- Thanks to Juliette Reinders Folmer for the patch
- Added more guard code for JS files with syntax errors (request #1271 and request #1272)
- PEAR.Commenting.FunctionComment fixers now correctly handle multi-line param comments
- Fixed bug #1230 : JS tokeniser incorrectly tokenises bitwise shifts as comparison
-- Thanks to Ryan McCue for the patch
- Fixed bug #1237 : Uninitialized string offset in PHP Tokenizer on PHP 5.2
Expand Down

0 comments on commit 7e92909

Please sign in to comment.