diff --git a/SlevomatCodingStandard/Helpers/ReferencedNameHelper.php b/SlevomatCodingStandard/Helpers/ReferencedNameHelper.php index 7043c190b..9b975768f 100644 --- a/SlevomatCodingStandard/Helpers/ReferencedNameHelper.php +++ b/SlevomatCodingStandard/Helpers/ReferencedNameHelper.php @@ -56,7 +56,7 @@ private static function createAllReferencedNames(PHP_CodeSniffer_File $phpcsFile T_DOC_COMMENT_TAG, ]; - $searchTypes = TokenHelper::$nameTokenCodes; + $searchTypes = array_merge([T_RETURN_TYPE], TokenHelper::$nameTokenCodes); if ($searchAnnotations) { $searchTypes = array_merge($phpDocTypes, $searchTypes); } @@ -118,7 +118,7 @@ private static function createAllReferencedNames(PHP_CodeSniffer_File $phpcsFile if ($nameEndPointer === null) { $beginSearchAtPointer = TokenHelper::findNextExcluding( $phpcsFile, - array_merge([T_WHITESPACE], TokenHelper::$nameTokenCodes), + array_merge([T_WHITESPACE, T_RETURN_TYPE], TokenHelper::$nameTokenCodes), $nameStartPointer ); continue; @@ -140,7 +140,7 @@ public static function findReferencedNameEndPointer(PHP_CodeSniffer_File $phpcsF return null; } - return TokenHelper::findNextExcluding($phpcsFile, TokenHelper::$nameTokenCodes, $startPointer + 1); + return TokenHelper::findNextExcluding($phpcsFile, array_merge([T_RETURN_TYPE], TokenHelper::$nameTokenCodes), $startPointer + 1); } /** diff --git a/build.xml b/build.xml index e744b04ab..bbd8b4b5b 100644 --- a/build.xml +++ b/build.xml @@ -26,6 +26,8 @@ passthru="true" checkreturn="true" > + + diff --git a/tests/Helpers/ReferencedNameHelperTest.php b/tests/Helpers/ReferencedNameHelperTest.php index 248da7309..8b263f333 100644 --- a/tests/Helpers/ReferencedNameHelperTest.php +++ b/tests/Helpers/ReferencedNameHelperTest.php @@ -83,4 +83,19 @@ public function testFindReferencedNameEndPointer() $this->assertTokenPointer(T_OPEN_PARENTHESIS, 3, $codeSnifferFile, $endTokenPointer); } + public function testReturnTypehint() + { + if (PHP_VERSION_ID < 70000) { + $this->markTestSkipped('Available on PHP7 only'); + } + + $codeSnifferFile = $this->getCodeSnifferFile( + __DIR__ . '/data/php7/return-typehint.php' + ); + $names = ReferencedNameHelper::getAllReferencedNames($codeSnifferFile, 0); + $this->assertCount(2, $names); + $this->assertSame('Bar', $names[0]->getNameAsReferencedInFile()); + $this->assertSame('\OtherNamespace\Lorem', $names[1]->getNameAsReferencedInFile()); + } + } diff --git a/tests/Helpers/data/php7/return-typehint.php b/tests/Helpers/data/php7/return-typehint.php new file mode 100644 index 000000000..753b62837 --- /dev/null +++ b/tests/Helpers/data/php7/return-typehint.php @@ -0,0 +1,18 @@ +