Skip to content

Commit cf23871

Browse files
committed
PHP7: return typehints are included in referenced names
1 parent 172a023 commit cf23871

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static function createAllReferencedNames(PHP_CodeSniffer_File $phpcsFile
5656
T_DOC_COMMENT_TAG,
5757
];
5858

59-
$searchTypes = TokenHelper::$nameTokenCodes;
59+
$searchTypes = array_merge([T_RETURN_TYPE], TokenHelper::$nameTokenCodes);
6060
if ($searchAnnotations) {
6161
$searchTypes = array_merge($phpDocTypes, $searchTypes);
6262
}
@@ -118,7 +118,7 @@ private static function createAllReferencedNames(PHP_CodeSniffer_File $phpcsFile
118118
if ($nameEndPointer === null) {
119119
$beginSearchAtPointer = TokenHelper::findNextExcluding(
120120
$phpcsFile,
121-
array_merge([T_WHITESPACE], TokenHelper::$nameTokenCodes),
121+
array_merge([T_WHITESPACE, T_RETURN_TYPE], TokenHelper::$nameTokenCodes),
122122
$nameStartPointer
123123
);
124124
continue;
@@ -140,7 +140,7 @@ public static function findReferencedNameEndPointer(PHP_CodeSniffer_File $phpcsF
140140
return null;
141141
}
142142

143-
return TokenHelper::findNextExcluding($phpcsFile, TokenHelper::$nameTokenCodes, $startPointer + 1);
143+
return TokenHelper::findNextExcluding($phpcsFile, array_merge([T_RETURN_TYPE], TokenHelper::$nameTokenCodes), $startPointer + 1);
144144
}
145145

146146
/**

build.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
passthru="true"
2727
checkreturn="true"
2828
>
29+
<arg value="--exclude"/>
30+
<arg path="tests/Helpers/data/php7"/>
2931
<arg path="SlevomatCodingStandard" />
3032
<arg path="tests" />
3133
</exec>

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,19 @@ public function testFindReferencedNameEndPointer()
8383
$this->assertTokenPointer(T_OPEN_PARENTHESIS, 3, $codeSnifferFile, $endTokenPointer);
8484
}
8585

86+
public function testReturnTypehint()
87+
{
88+
if (PHP_VERSION_ID < 70000) {
89+
$this->markTestSkipped('Available on PHP7 only');
90+
}
91+
92+
$codeSnifferFile = $this->getCodeSnifferFile(
93+
__DIR__ . '/data/php7/return-typehint.php'
94+
);
95+
$names = ReferencedNameHelper::getAllReferencedNames($codeSnifferFile, 0);
96+
$this->assertCount(2, $names);
97+
$this->assertSame('Bar', $names[0]->getNameAsReferencedInFile());
98+
$this->assertSame('\OtherNamespace\Lorem', $names[1]->getNameAsReferencedInFile());
99+
}
100+
86101
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ReturnTypehint;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(): Bar
9+
{
10+
11+
}
12+
13+
public function doBar(): \OtherNamespace\Lorem
14+
{
15+
16+
}
17+
18+
}

0 commit comments

Comments
 (0)