Skip to content

Commit

Permalink
Fixed bug squizlabs#1018 : The Squiz.Commenting.FunctionComment sniff…
Browse files Browse the repository at this point in the history
… doesn't detect incorrect types when @return tag has description
  • Loading branch information
gsherwood committed May 24, 2016
1 parent d9a3dea commit 34cbd79
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$phpcsFile->addError($error, $return, 'MissingReturnType');
} else {
// Check return type (can be multiple, separated by '|').
$typeNames = explode('|', $content);
// Support both a return type and a description. The return type
// is anything up to the first space.
$returnParts = explode(' ', $content, 2);
$returnType = $returnParts[0];

$typeNames = explode('|', $returnType);
$suggestedNames = array();
foreach ($typeNames as $i => $typeName) {
$suggestedName = PHP_CodeSniffer::suggestType($typeName);
Expand All @@ -85,23 +90,18 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
}

$suggestedType = implode('|', $suggestedNames);
if ($content !== $suggestedType) {
if ($returnType !== $suggestedType) {
$error = 'Expected "%s" but found "%s" for function return type';
$data = array(
$suggestedType,
$content,
$returnType,
);
$fix = $phpcsFile->addFixableError($error, $return, 'InvalidReturn', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($return + 2), $suggestedType);
}
}

// Support both a return type and a description. The return type
// is anything up to the first space.
$returnParts = explode(' ', $content, 2);
$returnType = $returnParts[0];

// If the return type is void, make sure there is
// no return statement in the function.
if ($returnType === 'void') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,8 @@ public function myFunction() {
/**
* Return description function + mixed return.
*
* @return mixed This is a description.
* @return boolean|int This is a description.
*/
public function myFunction() {
return 5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public function getErrorList()
669 => 1,
744 => 1,
748 => 1,
767 => 1,
784 => 1,
);

// The yield tests will only work in PHP versions where yield exists and
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Derek Henderson for the patch
- Fixed bug #1015 : The Squiz.Commenting.FunctionComment sniff doesn't allow description in @return tag
-- Thanks to Alexander Obuhovich for the patch
- Fixed bug #1018 : The Squiz.Commenting.FunctionComment sniff doesn't detect incorrect types when @return tag has description
</notes>
<contents>
<dir name="/">
Expand Down

0 comments on commit 34cbd79

Please sign in to comment.