From a516bb37c4ae031e206452ceab3c7021160fa7c6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 30 Dec 2016 02:23:41 +0100 Subject: [PATCH] Add additional return types to the `NewReturnTypeDeclarationsSniff` 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. --- Sniffs/PHP/NewReturnTypeDeclarationsSniff.php | 27 +++++++++++++++++++ .../NewReturnTypeDeclarationsSniffTest.php | 17 ++++++++---- .../new_return_type_declarations.php | 12 ++++++--- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Sniffs/PHP/NewReturnTypeDeclarationsSniff.php b/Sniffs/PHP/NewReturnTypeDeclarationsSniff.php index 12d25dba5..c12487a8e 100644 --- a/Sniffs/PHP/NewReturnTypeDeclarationsSniff.php +++ b/Sniffs/PHP/NewReturnTypeDeclarationsSniff.php @@ -2,6 +2,8 @@ /** * PHPCompatibility_Sniffs_PHP_NewReturnTypeDeclarationsSniff. * + * PHP version 7.0 + * * @category PHP * @package PHPCompatibility * @author Wim Godden @@ -10,6 +12,8 @@ /** * PHPCompatibility_Sniffs_PHP_NewReturnTypeDeclarationsSniff. * + * PHP version 7.0 + * * @category PHP * @package PHPCompatibility * @author Wim Godden @@ -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, @@ -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() diff --git a/Tests/Sniffs/PHP/NewReturnTypeDeclarationsSniffTest.php b/Tests/Sniffs/PHP/NewReturnTypeDeclarationsSniffTest.php index 911f130f2..706aef46b 100644 --- a/Tests/Sniffs/PHP/NewReturnTypeDeclarationsSniffTest.php +++ b/Tests/Sniffs/PHP/NewReturnTypeDeclarationsSniffTest.php @@ -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'), ); } } - diff --git a/Tests/sniff-examples/new_return_type_declarations.php b/Tests/sniff-examples/new_return_type_declarations.php index a16615912..4f177ab9e 100644 --- a/Tests/sniff-examples/new_return_type_declarations.php +++ b/Tests/sniff-examples/new_return_type_declarations.php @@ -1,11 +1,17 @@