-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from jrfnl/7.1/nullable-types
PHP 7.1: New sniff for nullable types.
- Loading branch information
Showing
4 changed files
with
351 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* PHPCompatibility_Sniffs_PHP_NewNullableTypes. | ||
* | ||
* PHP version 7.1 | ||
* | ||
* @category PHP | ||
* @package PHPCompatibility | ||
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl> | ||
*/ | ||
|
||
/** | ||
* PHPCompatibility_Sniffs_PHP_NewNullableTypes. | ||
* | ||
* Nullable type hints and return types are available since PHP 7.1. | ||
* | ||
* PHP version 7.1 | ||
* | ||
* @category PHP | ||
* @package PHPCompatibility | ||
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl> | ||
*/ | ||
class PHPCompatibility_Sniffs_PHP_NewNullableTypesSniff extends PHPCompatibility_Sniff | ||
{ | ||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
$tokens = array( | ||
T_FUNCTION, | ||
); | ||
|
||
if (version_compare(PHP_CodeSniffer::VERSION, '2.3.4') >= 0) { | ||
$tokens[] = T_RETURN_TYPE; | ||
} | ||
|
||
return $tokens; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
if ($this->supportsBelow('7.0') === false) { | ||
return; | ||
} | ||
|
||
$tokens = $phpcsFile->getTokens(); | ||
$tokenCode = $tokens[$stackPtr]['code']; | ||
|
||
if ($tokenCode === T_FUNCTION) { | ||
$this->processFunctionDeclaration($phpcsFile, $stackPtr); | ||
} else { | ||
$this->processReturnType($phpcsFile, $stackPtr); | ||
} | ||
}//end process() | ||
|
||
|
||
/** | ||
* Process this test for function tokens. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
protected function processFunctionDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$params = $this->getMethodParameters($phpcsFile, $stackPtr); | ||
|
||
if (empty($params) === false && is_array($params)) { | ||
foreach ($params as $param) { | ||
if ($param['nullable_type'] === true) { | ||
$phpcsFile->addError( | ||
'Nullable type declarations are not supported in PHP 7.0 or earlier. Found: %s', | ||
$stackPtr, | ||
'typeDeclarationFound', | ||
array($param['type_hint']) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Process this test for return type tokens. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
protected function processReturnType(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
if ($tokens[($stackPtr - 1)]['code'] === T_INLINE_THEN) { | ||
$phpcsFile->addError( | ||
'Nullable return types are not supported in PHP 7.0 or earlier.', | ||
$stackPtr, | ||
'returnTypeFound' | ||
); | ||
} | ||
} | ||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/** | ||
* New nullable type hints / return types sniff test file | ||
* | ||
* @package PHPCompatibility | ||
*/ | ||
|
||
|
||
/** | ||
* New nullable type hints / return types sniff test file | ||
* | ||
* @uses BaseSniffTest | ||
* @package PHPCompatibility | ||
* @author Juliette Reinders Folmer <phpcompatibility_nospam@adviesenzo.nl> | ||
*/ | ||
class NewNullableTypesSniffTest extends BaseSniffTest | ||
{ | ||
const TEST_FILE = 'sniff-examples/new_nullable_types.php'; | ||
|
||
/** | ||
* testNewNullableReturnTypes | ||
* | ||
* @group nullableTypes | ||
* | ||
* @dataProvider dataNewNullableReturnTypes | ||
* | ||
* @param int $line The line number. | ||
* | ||
* @return void | ||
*/ | ||
public function testNewNullableReturnTypes($line) | ||
{ | ||
// Skip this test for low PHPCS versions. | ||
if (version_compare(PHP_CodeSniffer::VERSION, '2.3.4', '<')) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$file = $this->sniffFile(self::TEST_FILE, '7.0'); | ||
$this->assertError($file, $line, 'Nullable return types are not supported in PHP 7.0 or earlier.'); | ||
|
||
$file = $this->sniffFile(self::TEST_FILE, '7.1'); | ||
$this->assertNoViolation($file, $line); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testNewNullableReturnTypes() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNewNullableReturnTypes() | ||
{ | ||
return array( | ||
array(20), | ||
array(21), | ||
array(22), | ||
array(23), | ||
array(24), | ||
array(25), | ||
array(26), | ||
array(27), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* testNewNullableTypeHints | ||
* | ||
* @group nullableTypes | ||
* | ||
* @dataProvider dataNewNullableTypeHints | ||
* | ||
* @param int $line The line number. | ||
* | ||
* @return void | ||
*/ | ||
public function testNewNullableTypeHints($line) | ||
{ | ||
$file = $this->sniffFile(self::TEST_FILE, '7.0'); | ||
$this->assertError($file, $line, 'Nullable type declarations are not supported in PHP 7.0 or earlier.'); | ||
|
||
$file = $this->sniffFile(self::TEST_FILE, '7.1'); | ||
$this->assertNoViolation($file, $line); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testNewNullableTypeHints() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNewNullableTypeHints() | ||
{ | ||
return array( | ||
array(45), | ||
array(46), | ||
array(47), | ||
array(48), | ||
array(49), | ||
array(50), | ||
array(51), | ||
array(52), | ||
|
||
array(55), // Three errors of the same. | ||
); | ||
} | ||
|
||
|
||
/** | ||
* testNoViolation | ||
* | ||
* @group nullableTypes | ||
* | ||
* @dataProvider dataNoViolation | ||
* | ||
* @param int $line The line number. | ||
* | ||
* @return void | ||
*/ | ||
public function testNoViolation($line) | ||
{ | ||
$file = $this->sniffFile(self::TEST_FILE, '7.0'); // Arbitrary pre-PHP 7.1 version. | ||
$this->assertNoViolation($file, $line); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testNoViolation() | ||
* | ||
* @return array | ||
*/ | ||
public function dataNoViolation() | ||
{ | ||
return array( | ||
array(8), | ||
array(9), | ||
array(10), | ||
array(11), | ||
array(12), | ||
array(13), | ||
array(14), | ||
array(15), | ||
|
||
array(33), | ||
array(34), | ||
array(35), | ||
array(36), | ||
array(37), | ||
array(38), | ||
array(39), | ||
array(40), | ||
); | ||
} | ||
} |
Oops, something went wrong.