Skip to content

Commit

Permalink
Make the NewReturnTypeDeclarationsSniff compatible with older PHPCS…
Browse files Browse the repository at this point in the history
… versions.
  • Loading branch information
jrfnl committed Dec 30, 2016
1 parent 801df4d commit 96a139f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
20 changes: 16 additions & 4 deletions Sniffs/PHP/NewReturnTypeDeclarationsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ class PHPCompatibility_Sniffs_PHP_NewReturnTypeDeclarationsSniff extends PHPComp
*/
public function register()
{
if (version_compare(PHP_CodeSniffer::VERSION, '2.3.4') >= 0) {
return array(T_RETURN_TYPE);
} else {
return array();
$tokens = array(
T_FUNCTION,
);

if (defined('T_RETURN_TYPE')) {
$tokens[] = T_RETURN_TYPE;
}

return $tokens;
}//end register()


Expand All @@ -78,6 +82,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Deal with older PHPCS version which don't recognize return type hints.
if ($tokens[$stackPtr]['code'] === T_FUNCTION) {
$returnTypeHint = $this->getReturnTypeHintToken($phpcsFile, $stackPtr);
if ($returnTypeHint !== false) {
$stackPtr = $returnTypeHint;
}
}

if (isset($this->newTypes[$tokens[$stackPtr]['content']]) === true) {
$itemInfo = array(
'name' => $tokens[$stackPtr]['content'],
Expand Down
13 changes: 0 additions & 13 deletions Tests/Sniffs/PHP/NewReturnTypeDeclarationsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ class NewReturnTypeDeclarationsSniffTest extends BaseSniffTest
{
const TEST_FILE = 'sniff-examples/new_return_type_declarations.php';

/**
* Set up: skip these tests if the PHPCS version isn't high enough.
*/
protected function setUp()
{
if (version_compare(PHP_CodeSniffer::VERSION, '2.3.4', '<')) {
$this->markTestSkipped();
}
else {
parent::setUp();
}
}

/**
* testReturnType
*
Expand Down

0 comments on commit 96a139f

Please sign in to comment.