Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP7: return typehints are included in referenced names #8

Merged
merged 1 commit into from
Dec 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{

}

}