Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create special case for T_BAD_CHARACTER constant #1586

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion PHPCompatibility/Sniffs/Constants/NewConstantsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*
Expand Down Expand Up @@ -6803,6 +6803,10 @@ class NewConstantsSniff extends Sniff
'7.4' => true,
],
'T_BAD_CHARACTER' => [
'5.6' => true,
'7.0' => false,
'7.1' => false,
'7.2' => false,
fredden marked this conversation as resolved.
Show resolved Hide resolved
'7.3' => false,
'7.4' => true,
'extension' => 'tokenizer',
Expand Down Expand Up @@ -8085,6 +8089,31 @@ protected function handleFeature(File $phpcsFile, $stackPtr, array $itemInfo)
$itemArray = $this->newConstants[$itemInfo['name']];
$versionInfo = $this->getVersionInfo($itemArray);

if ($itemInfo['name'] === 'T_BAD_CHARACTER') {
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
// T_BAD_CHARACTER is a special case. It was removed in 7.0.0 and re-added in 7.4.0
// See also PHPCompatibility.Constants.RemovedConstants
$displayError = false;
$message = 'The constant "T_BAD_CHARACTER" is not present in PHP versions 7.0 through 7.3';
foreach ($itemArray as $version => $present) {
if ($present !== false) {
// We only need to show an error if the constant is _missing_ from the version under test
continue;
}

if (ScannedCode::shouldRunOnOrAbove($version) && ScannedCode::shouldRunOnOrBelow($version)) {
$displayError = true;
break;
}
}

if ($displayError === true) {
$msgInfo = $this->getMessageInfo($itemInfo['name'], $itemInfo['name'], $versionInfo);
$phpcsFile->addError($message, $stackPtr, $msgInfo['errorcode'], $msgInfo['data']);
}

return;
}

if (empty($versionInfo['not_in_version'])
|| ScannedCode::shouldRunOnOrBelow($versionInfo['not_in_version']) === false
) {
Expand Down
38 changes: 36 additions & 2 deletions PHPCompatibility/Sniffs/Constants/RemovedConstantsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1933,7 +1933,12 @@ class RemovedConstantsSniff extends Sniff
'extension' => 'tokenizer',
],
'T_BAD_CHARACTER' => [
'7.0' => true,
'5.6' => false,
'7.0' => true,
'7.1' => true,
'7.2' => true,
'7.3' => true,
'7.4' => false,
fredden marked this conversation as resolved.
Show resolved Hide resolved
'extension' => 'tokenizer',
],
'MSSQL_ASSOC' => [
Expand Down Expand Up @@ -2718,6 +2723,35 @@ protected function handleFeature(File $phpcsFile, $stackPtr, array $itemInfo)
$versionInfo = $this->getVersionInfo($itemArray);
$isError = null;

if ($itemInfo['name'] === 'T_BAD_CHARACTER') {
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
// T_BAD_CHARACTER is a special case. It was removed in 7.0.0 and re-added in 7.4.0
// See also PHPCompatibility.Constants.NewConstants
$displayError = false;
$message = 'The constant "T_BAD_CHARACTER" is not present in PHP versions 7.0 through 7.3';

foreach ($itemArray as $version => $absent) {
if ($absent !== true) {
// We only need to show an error if the constant is _missing_ from the version under test
continue;
}

if (ScannedCode::shouldRunOnOrAbove($version) && ScannedCode::shouldRunOnOrBelow($version)) {
$displayError = true;
break;
}
}

if ($displayError === true) {
// Remove this flag as it's not correct and its presence alters the error code
$versionInfo['deprecated'] = '';

$msgInfo = $this->getMessageInfo($itemInfo['name'], $itemInfo['name'], $versionInfo);
$phpcsFile->addError($message, $stackPtr, $msgInfo['errorcode'], $msgInfo['data']);
}

return;
}

if (empty($versionInfo['removed']) === false
&& ScannedCode::shouldRunOnOrAbove($versionInfo['removed']) === true
) {
Expand Down
60 changes: 59 additions & 1 deletion PHPCompatibility/Tests/Constants/NewConstantsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,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'],
Expand Down Expand Up @@ -1725,4 +1724,63 @@ public function testNoViolationsInFileOnValidVersion()
$file = $this->sniffFile(__FILE__, '99.0'); // High version beyond newest addition.
$this->assertNoViolation($file);
}

/**
* The T_BAD_CHARACTER constant is a special case
*
* @param string $phpVersion PHP version (or range) tot test with.
* @param bool $shouldError If we expect to get an error or not from the sniff
*
* @return void
*
* @dataProvider dataTBadCharacter
*/
public function testTBadCharacter($phpVersion, $shouldError)
{
$line = 862;

$message = 'The constant "T_BAD_CHARACTER" is not present in PHP versions 7.0 through 7.3';

$file = $this->sniffFile(__FILE__, $phpVersion);
if ($shouldError) {
$this->assertError($file, $line, $message);
} else {
$this->assertNoViolation($file, $line);
}
}

/**
* Data provider
*
* @see testTBadCharacter
*
* @return array<array<string, bool>>
*/
public static function dataTBadCharacter()
{
// This could be more elegantly written with a generator, but this project supports PHP v5.4 which is before generators were introduced (in PHP 5.5).
return [
['5.6', false], // Last version before removal
['7.0', true], // Removed
['7.1', true], // Removed
['7.2', true], // Removed
['7.3', true], // Removed
['7.4', false], // Added again

['-5.6', false], // Before
['-7.2', true], // Inside
['-8.2', true], // After

['5.4-', true], // Before
['7.2-', true], // Inside
['7.4-', false], // After

['5.0-5.6', false], // Before
['7.4-8.3', false], // After
['5.0-8.3', true], // Inside and both sides
['5.0-7.2', true], // Inside and before
['7.0-7.3', true], // Inside only
['7.2-8.1', true], // Inside and after
];
}
}
60 changes: 59 additions & 1 deletion PHPCompatibility/Tests/Constants/RemovedConstantsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@ 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'],
Expand Down Expand Up @@ -928,4 +927,63 @@ public function testNoViolationsInFileOnValidVersion()
$file = $this->sniffFile(__FILE__, '5.0'); // Low version below the first deprecation.
$this->assertNoViolation($file);
}

/**
* The T_BAD_CHARACTER constant is a special case
*
* @param string $phpVersion PHP version (or range) tot test with.
* @param bool $shouldError If we expect to get an error or not from the sniff
*
* @return void
*
* @dataProvider dataTBadCharacter
*/
public function testTBadCharacter($phpVersion, $shouldError)
{
$line = 140;

$message = 'The constant "T_BAD_CHARACTER" is not present in PHP versions 7.0 through 7.3';

$file = $this->sniffFile(__FILE__, $phpVersion);
if ($shouldError) {
$this->assertError($file, $line, $message);
} else {
$this->assertNoViolation($file, $line);
}
}

/**
* Data provider
*
* @see testTBadCharacter
*
* @return array<array<string, bool>>
*/
public static function dataTBadCharacter()
{
// This could be more elegantly written with a generator, but this project supports PHP v5.4 which is before generators were introduced (in PHP 5.5).
return [
['5.6', false], // Last version before removal
['7.0', true], // Removed
['7.1', true], // Removed
['7.2', true], // Removed
['7.3', true], // Removed
['7.4', false], // Added again

['-5.6', false], // Before
['-7.2', true], // Inside
['-8.2', true], // After

['5.4-', true], // Before
['7.2-', true], // Inside
['7.4-', false], // After

['5.0-5.6', false], // Before
['7.4-8.3', false], // After
['5.0-8.3', true], // Inside and both sides
['5.0-7.2', true], // Inside and before
['7.0-7.3', true], // Inside only
['7.2-8.1', true], // Inside and after
];
}
}
Loading