Skip to content

Commit 34cbd79

Browse files
committed
Fixed bug #1018 : The Squiz.Commenting.FunctionComment sniff doesn't detect incorrect types when @return tag has description
1 parent d9a3dea commit 34cbd79

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
7575
$phpcsFile->addError($error, $return, 'MissingReturnType');
7676
} else {
7777
// Check return type (can be multiple, separated by '|').
78-
$typeNames = explode('|', $content);
78+
// Support both a return type and a description. The return type
79+
// is anything up to the first space.
80+
$returnParts = explode(' ', $content, 2);
81+
$returnType = $returnParts[0];
82+
83+
$typeNames = explode('|', $returnType);
7984
$suggestedNames = array();
8085
foreach ($typeNames as $i => $typeName) {
8186
$suggestedName = PHP_CodeSniffer::suggestType($typeName);
@@ -85,23 +90,18 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
8590
}
8691

8792
$suggestedType = implode('|', $suggestedNames);
88-
if ($content !== $suggestedType) {
93+
if ($returnType !== $suggestedType) {
8994
$error = 'Expected "%s" but found "%s" for function return type';
9095
$data = array(
9196
$suggestedType,
92-
$content,
97+
$returnType,
9398
);
9499
$fix = $phpcsFile->addFixableError($error, $return, 'InvalidReturn', $data);
95100
if ($fix === true) {
96101
$phpcsFile->fixer->replaceToken(($return + 2), $suggestedType);
97102
}
98103
}
99104

100-
// Support both a return type and a description. The return type
101-
// is anything up to the first space.
102-
$returnParts = explode(' ', $content, 2);
103-
$returnType = $returnParts[0];
104-
105105
// If the return type is void, make sure there is
106106
// no return statement in the function.
107107
if ($returnType === 'void') {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,8 @@ public function myFunction() {
781781
/**
782782
* Return description function + mixed return.
783783
*
784-
* @return mixed This is a description.
784+
* @return boolean|int This is a description.
785785
*/
786786
public function myFunction() {
787+
return 5;
787788
}

CodeSniffer/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public function getErrorList()
111111
669 => 1,
112112
744 => 1,
113113
748 => 1,
114+
767 => 1,
115+
784 => 1,
114116
);
115117

116118
// The yield tests will only work in PHP versions where yield exists and

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5151
-- Thanks to Derek Henderson for the patch
5252
- Fixed bug #1015 : The Squiz.Commenting.FunctionComment sniff doesn't allow description in @return tag
5353
-- Thanks to Alexander Obuhovich for the patch
54+
- Fixed bug #1018 : The Squiz.Commenting.FunctionComment sniff doesn't detect incorrect types when @return tag has description
5455
</notes>
5556
<contents>
5657
<dir name="/">

0 commit comments

Comments
 (0)