Skip to content

Commit c7107a9

Browse files
committed
Squiz/FunctionComment: bug fix - classnames with underscores truncate the return type
If a return type included a class name containing an underscore, the type would be truncated at the underscore and would not take anything after it into account. Bug introduced in PHPCS 2.8.1 via PR squizlabs/PHP_CodeSniffer 1310 and for some reason never reported... 😳 Fixed now. Includes tests. Fixes 1197
1 parent 38a40fd commit c7107a9

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
7676
$phpcsFile->addError($error, $return, 'MissingReturnType');
7777
} else {
7878
// Support both a return type and a description.
79-
preg_match('`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9\[\]]+))*)( .*)?`i', $content, $returnParts);
79+
preg_match('`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9_\[\]]+))*)( .*)?`i', $content, $returnParts);
8080
if (isset($returnParts[1]) === false) {
8181
return;
8282
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,3 +1167,30 @@ public function setTranslator($a): void
11671167
{
11681168
$this->translator = $translator;
11691169
}
1170+
1171+
/**
1172+
* Issue PHPCSStandards/PHP_CodeSniffer#1197.
1173+
*
1174+
* @return int|WP_Error
1175+
*/
1176+
function doNotTruncateClassNamesContainingUnderscoresAtEndOfType() {
1177+
return 0;
1178+
}
1179+
1180+
/**
1181+
* Issue PHPCSStandards/PHP_CodeSniffer#1197.
1182+
*
1183+
* @return WP_Error|int
1184+
*/
1185+
function doNotTruncateClassNamesContainingUnderscoresAtStartOfType() {
1186+
return 0;
1187+
}
1188+
1189+
/**
1190+
* Issue PHPCSStandards/PHP_CodeSniffer#1197.
1191+
*
1192+
* @return int|WP_Error|false
1193+
*/
1194+
function doNotTruncateClassNamesContainingUnderscoresInTheMiddelOfType() {
1195+
return 0;
1196+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,3 +1167,30 @@ public function setTranslator($a): void
11671167
{
11681168
$this->translator = $translator;
11691169
}
1170+
1171+
/**
1172+
* Issue PHPCSStandards/PHP_CodeSniffer#1197.
1173+
*
1174+
* @return integer|WP_Error
1175+
*/
1176+
function doNotTruncateClassNamesContainingUnderscoresAtEndOfType() {
1177+
return 0;
1178+
}
1179+
1180+
/**
1181+
* Issue PHPCSStandards/PHP_CodeSniffer#1197.
1182+
*
1183+
* @return WP_Error|integer
1184+
*/
1185+
function doNotTruncateClassNamesContainingUnderscoresAtStartOfType() {
1186+
return 0;
1187+
}
1188+
1189+
/**
1190+
* Issue PHPCSStandards/PHP_CodeSniffer#1197.
1191+
*
1192+
* @return integer|WP_Error|false
1193+
*/
1194+
function doNotTruncateClassNamesContainingUnderscoresInTheMiddelOfType() {
1195+
return 0;
1196+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public function getErrorList()
141141
1144 => 1,
142142
1145 => 1,
143143
1151 => 1,
144+
1174 => 1,
145+
1183 => 1,
146+
1192 => 1,
144147
];
145148

146149
// Scalar type hints only work from PHP 7 onwards.

0 commit comments

Comments
 (0)