From 2724255b88c1dc198da23362a8c8e6e842b42786 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 25 Jul 2023 10:48:25 +0100 Subject: [PATCH] Create special case for T_BAD_CHARACTER constant --- .../Sniffs/Constants/NewConstantsSniff.php | 6 +- .../Constants/RemovedConstantsSniff.php | 5 +- .../TemporarilyRemovedConstantsSniff.php | 106 ++++++++++++++++ .../Tests/Constants/NewConstantsUnitTest.inc | 2 +- .../Tests/Constants/NewConstantsUnitTest.php | 1 - .../Constants/RemovedConstantsUnitTest.inc | 2 +- .../Constants/RemovedConstantsUnitTest.php | 2 +- .../TemporarilyRemovedConstantsUnitTest.inc | 4 + .../TemporarilyRemovedConstantsUnitTest.php | 115 ++++++++++++++++++ 9 files changed, 230 insertions(+), 13 deletions(-) create mode 100644 PHPCompatibility/Sniffs/Constants/TemporarilyRemovedConstantsSniff.php create mode 100644 PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.inc create mode 100644 PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.php diff --git a/PHPCompatibility/Sniffs/Constants/NewConstantsSniff.php b/PHPCompatibility/Sniffs/Constants/NewConstantsSniff.php index 8be4555d7..52c8cf327 100644 --- a/PHPCompatibility/Sniffs/Constants/NewConstantsSniff.php +++ b/PHPCompatibility/Sniffs/Constants/NewConstantsSniff.php @@ -32,7 +32,7 @@ class NewConstantsSniff extends Sniff * A list of new PHP Constants, not present in older versions. * * The array lists : version number with false (not present) or true (present). - * If's sufficient to list the first version where the constant appears. + * It's sufficient to list the first version where the constant appears. * * Note: PHP constants are case-sensitive! * @@ -6269,10 +6269,6 @@ class NewConstantsSniff extends Sniff '7.3' => false, '7.4' => true, ], - 'T_BAD_CHARACTER' => [ - '7.3' => false, - '7.4' => true, - ], 'T_COALESCE_EQUAL' => [ '7.3' => false, '7.4' => true, diff --git a/PHPCompatibility/Sniffs/Constants/RemovedConstantsSniff.php b/PHPCompatibility/Sniffs/Constants/RemovedConstantsSniff.php index c4ca6c4b4..0bc28dc36 100644 --- a/PHPCompatibility/Sniffs/Constants/RemovedConstantsSniff.php +++ b/PHPCompatibility/Sniffs/Constants/RemovedConstantsSniff.php @@ -33,7 +33,7 @@ class RemovedConstantsSniff extends Sniff * A list of removed PHP Constants. * * The array lists : version number with false (deprecated) or true (removed). - * If's sufficient to list the first version where the constant was deprecated/removed. + * It's sufficient to list the first version where the constant was deprecated/removed. * * Optional, the array can contain an `alternative` key listing an alternative constant * to be used instead. @@ -1923,9 +1923,6 @@ class RemovedConstantsSniff extends Sniff 'T_CHARACTER' => [ '7.0' => true, ], - 'T_BAD_CHARACTER' => [ - '7.0' => true, - ], 'MSSQL_ASSOC' => [ '7.0' => true, 'extension' => 'mssql', diff --git a/PHPCompatibility/Sniffs/Constants/TemporarilyRemovedConstantsSniff.php b/PHPCompatibility/Sniffs/Constants/TemporarilyRemovedConstantsSniff.php new file mode 100644 index 000000000..214787b59 --- /dev/null +++ b/PHPCompatibility/Sniffs/Constants/TemporarilyRemovedConstantsSniff.php @@ -0,0 +1,106 @@ + array(string => float)) + */ + protected $constants = [ + 'T_BAD_CHARACTER' => [ + 'removed' => 7.0, + 'last_absent' => 7.3, + 're-added' => 7.4, + ], + ]; + + /** + * Returns an array of tokens this test wants to listen for. + * + * @since 10.0.0 + * + * @return array + */ + public function register() + { + return [ + \T_STRING, + ]; + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @since 10.0.0 + * + * @param 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(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $constantName = $tokens[$stackPtr]['content']; + + if (isset($this->constants[$constantName]) === false) { + return; + } + + if (MiscHelper::isUseOfGlobalConstant($phpcsFile, $stackPtr) === false) { + return; + } + + $itemArray = $this->constants[$constantName]; + + if ( + ScannedCode::shouldRunOnOrAbove($itemArray['removed']) && + ScannedCode::shouldRunOnOrBelow($itemArray['last_absent']) + ) { + MessageHelper::addMessage( + $phpcsFile, + 'The constant "%s" was removed in PHP %.1f and re-added in PHP %.1f.', + $stackPtr, + true, + MessageHelper::stringToErrorCode($constantName, true), + [ + $constantName, + $itemArray['removed'], + $itemArray['re-added'], + ] + ); + } + } +} diff --git a/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.inc b/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.inc index eb2e1f30e..8e7d0d5a6 100644 --- a/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.inc +++ b/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.inc @@ -859,7 +859,7 @@ echo TIDY_TAG_TRACK; echo TIDY_TAG_VIDEO; echo CURL_VERSION_ALTSVC; echo CURL_VERSION_CURLDEBUG; -echo T_BAD_CHARACTER; + echo IMG_FILTER_SCATTER; echo PASSWORD_ARGON2_PROVIDER; echo SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13; diff --git a/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.php b/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.php index 2e331becf..9f3870756 100644 --- a/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.php +++ b/PHPCompatibility/Tests/Constants/NewConstantsUnitTest.php @@ -1437,7 +1437,6 @@ public static function dataNewConstant() ['PASSWORD_ARGON2_PROVIDER', '7.3', [864], '7.4'], ['PHP_WINDOWS_EVENT_CTRL_C', '7.3', [830], '7.4'], ['PHP_WINDOWS_EVENT_CTRL_BREAK', '7.3', [831], '7.4'], - ['T_BAD_CHARACTER', '7.3', [862], '7.4'], ['T_COALESCE_EQUAL', '7.3', [1048], '7.4'], ['T_FN', '7.3', [1049], '7.4'], ['TIDY_TAG_ARTICLE', '7.3', [832], '7.4'], diff --git a/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.inc b/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.inc index 063596c61..d37b4efef 100644 --- a/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.inc +++ b/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.inc @@ -137,7 +137,7 @@ echo IBASE_SVC_GET_USERS; echo FILTER_SANITIZE_MAGIC_QUOTES; echo CURLPIPE_HTTP1; echo T_CHARACTER; -echo T_BAD_CHARACTER; + echo NCURSES_COLOR_BLACK; echo NCURSES_COLOR_WHITE; diff --git a/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.php b/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.php index f1ebc998a..c8eaf280b 100644 --- a/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.php +++ b/PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.php @@ -635,7 +635,7 @@ public static function dataRemovedConstant() ['PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT', '7.0', [15], '5.6'], ['T_CHARACTER', '7.0', [139], '5.6'], - ['T_BAD_CHARACTER', '7.0', [140], '5.6'], + ['MSSQL_ASSOC', '7.0', [557], '5.6'], ['MSSQL_NUM', '7.0', [558], '5.6'], ['MSSQL_BOTH', '7.0', [559], '5.6'], diff --git a/PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.inc b/PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.inc new file mode 100644 index 000000000..860089108 --- /dev/null +++ b/PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.inc @@ -0,0 +1,4 @@ +sniffFile(__FILE__, $phpVersion); + foreach ($lines as $line) { + if ($shouldError) { + $this->assertError($file, $line, $error); + } else { + $this->assertNoViolation($file, $line); + } + } + } + + /** + * Data provider. + * + * @see testReaddedConstant() + * + * @return array + */ + public static function dataReaddedConstants() + { + return [ + ['T_BAD_CHARACTER', [4], '5.0', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.1', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.2', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.3', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.4', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.5', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.6', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.0', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.1', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.2', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.3', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.4', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '8.0', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '8.1', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '8.2', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '8.3', false, '7.0', '7.4'], + + ['T_BAD_CHARACTER', [4], '-5.1', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '-5.6', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '-7.2', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '-8.2', true, '7.0', '7.4'], + + ['T_BAD_CHARACTER', [4], '5.4-', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.2-', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.4-', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '8.0-', false, '7.0', '7.4'], + + ['T_BAD_CHARACTER', [4], '5.0-5.6', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '8.0-8.3', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.0-8.3', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '5.0-7.4', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.0-7.3', true, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.4-8.3', false, '7.0', '7.4'], + ['T_BAD_CHARACTER', [4], '7.2-8.1', true, '7.0', '7.4'], + ]; + } + + /** + * Verify no notices are thrown at all. + * + * @return void + * @covers ::process() + */ + public function testNoViolationsInFileOnValidVersion() + { + $file = $this->sniffFile(__FILE__, '5.0'); // Low version below the first deprecation. + $this->assertNoViolation($file); + + $file = $this->sniffFile(__FILE__, '99.0'); // High version, after all constants have been re-added. + $this->assertNoViolation($file); + } +}