Skip to content

Commit

Permalink
PHP7: return typehints are included in referenced names
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 25, 2015
1 parent 172a023 commit cf23871
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions SlevomatCodingStandard/Helpers/ReferencedNameHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
passthru="true"
checkreturn="true"
>
<arg value="--exclude"/>
<arg path="tests/Helpers/data/php7"/>
<arg path="SlevomatCodingStandard" />
<arg path="tests" />
</exec>
Expand Down
15 changes: 15 additions & 0 deletions tests/Helpers/ReferencedNameHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
18 changes: 18 additions & 0 deletions tests/Helpers/data/php7/return-typehint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ReturnTypehint;

class Foo
{

public function doFoo(): Bar
{

}

public function doBar(): \OtherNamespace\Lorem
{

}

}

0 comments on commit cf23871

Please sign in to comment.