Skip to content

Commit

Permalink
Add additional return types to the NewReturnTypeDeclarationsSniff
Browse files Browse the repository at this point in the history
As return type hints didn't exist at all for PHP < 7.0, this sniff should also alert on return type hints for `array`, `callable`, `self` or arbitrary (namespaced) class names.

Includes additional unit tests.
  • Loading branch information
jrfnl committed Dec 30, 2016
1 parent 96a139f commit a516bb3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
27 changes: 27 additions & 0 deletions Sniffs/PHP/NewReturnTypeDeclarationsSniff.php
Expand Up @@ -2,6 +2,8 @@
/**
* PHPCompatibility_Sniffs_PHP_NewReturnTypeDeclarationsSniff.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
Expand All @@ -10,6 +12,8 @@
/**
* PHPCompatibility_Sniffs_PHP_NewReturnTypeDeclarationsSniff.
*
* PHP version 7.0
*
* @category PHP
* @package PHPCompatibility
* @author Wim Godden <wim.godden@cu.be>
Expand Down Expand Up @@ -42,6 +46,22 @@ class PHPCompatibility_Sniffs_PHP_NewReturnTypeDeclarationsSniff extends PHPComp
'5.6' => false,
'7.0' => true,
),
'array' => array(
'5.6' => false,
'7.0' => true,
),
'callable' => array(
'5.6' => false,
'7.0' => true,
),
'self' => array(
'5.6' => false,
'7.0' => true,
),
'Class name' => array(
'5.6' => false,
'7.0' => true,
),

'void' => array(
'7.0' => false,
Expand Down Expand Up @@ -96,6 +116,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
);
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
}
// Handle class name based return types.
elseif ($tokens[$stackPtr]['code'] === T_STRING || (defined('T_RETURN_TYPE') && $tokens[$stackPtr]['code'] === T_RETURN_TYPE)) {
$itemInfo = array(
'name' => 'Class name',
);
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
}
}//end process()


Expand Down
17 changes: 12 additions & 5 deletions Tests/Sniffs/PHP/NewReturnTypeDeclarationsSniffTest.php
Expand Up @@ -53,12 +53,19 @@ public function testReturnType($returnType, $lastVersionBefore, $line, $okVersio
public function dataReturnType()
{
return array(
array('bool', '5.6', 3, '7.0'),
array('bool', '5.6', 4, '7.0'),
array('int', '5.6', 5, '7.0'),
array('float', '5.6', 7, '7.0'),
array('string', '5.6', 9, '7.0'),
array('void', '7.0', 11, '7.1'),
array('float', '5.6', 6, '7.0'),
array('string', '5.6', 7, '7.0'),
array('array', '5.6', 8, '7.0'),
array('callable', '5.6', 9, '7.0'),
array('self', '5.6', 10, '7.0'),
array('Class name', '5.6', 11, '7.0'),
array('Class name', '5.6', 12, '7.0'),
array('Class name', '5.6', 13, '7.0'),
array('Class name', '5.6', 14, '7.0'),

array('void', '7.0', 17, '7.1'),
);
}
}

12 changes: 9 additions & 3 deletions Tests/sniff-examples/new_return_type_declarations.php
@@ -1,11 +1,17 @@
<?php

// PHP 7.0+
function foo($a): bool {}

function foo($a): int {}

function foo($a): float {}

function foo($a): string {}
function foo($a): array {}
function foo($a): callable {}
function foo($a): self {}
function foo($a): Baz {}
function foo($a): \Baz {}
function foo($a): myNamespace\Baz {}
function foo($a): \myNamespace\Baz {}

// PHP 7.1+
function foo($a): void {}

0 comments on commit a516bb3

Please sign in to comment.