-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create special case for T_BAD_CHARACTER constant
- Loading branch information
Showing
9 changed files
with
230 additions
and
13 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
106 changes: 106 additions & 0 deletions
106
PHPCompatibility/Sniffs/Constants/TemporarilyRemovedConstantsSniff.php
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,106 @@ | ||
<?php | ||
|
||
/** | ||
* PHPCompatibility, an external standard for PHP_CodeSniffer. | ||
* | ||
* @package PHPCompatibility | ||
* @copyright 2012-2023 PHPCompatibility Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCompatibility/PHPCompatibility | ||
*/ | ||
|
||
namespace PHPCompatibility\Sniffs\Constants; | ||
|
||
use PHPCompatibility\Helpers\ComplexVersionDeprecatedRemovedFeatureTrait; | ||
use PHPCompatibility\Helpers\MiscHelper; | ||
use PHPCompatibility\Helpers\ScannedCode; | ||
use PHPCompatibility\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHPCSUtils\Utils\MessageHelper; | ||
|
||
/** | ||
* Detect use of PHP native global constants which have been removed and re-added. | ||
* | ||
* PHP version All | ||
* | ||
* @since 10.0.0 | ||
*/ | ||
class TemporarilyRemovedConstantsSniff extends Sniff | ||
{ | ||
use ComplexVersionDeprecatedRemovedFeatureTrait; | ||
|
||
/** | ||
* A list of removed & re-added PHP Constants. | ||
* | ||
* Note: PHP Constants are case-sensitive! | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @var array(string => 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'], | ||
] | ||
); | ||
} | ||
} | ||
} |
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
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
4 changes: 4 additions & 0 deletions
4
PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.inc
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,4 @@ | ||
<?php | ||
|
||
// Constants which were present, then removed, then present again. | ||
echo T_BAD_CHARACTER; |
115 changes: 115 additions & 0 deletions
115
PHPCompatibility/Tests/Constants/TemporarilyRemovedConstantsUnitTest.php
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,115 @@ | ||
<?php | ||
|
||
/** | ||
* PHPCompatibility, an external standard for PHP_CodeSniffer. | ||
* | ||
* @package PHPCompatibility | ||
* @copyright 2012-2023 PHPCompatibility Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCompatibility/PHPCompatibility | ||
*/ | ||
|
||
namespace PHPCompatibility\Tests\Constants; | ||
|
||
use PHPCompatibility\Tests\BaseSniffTest; | ||
|
||
/** | ||
* Test the ReaddedConstants sniff. | ||
* | ||
* @group constants | ||
* @covers \PHPCompatibility\Sniffs\Constants\TemporarilyRemovedConstantsSniff | ||
* @since 10.0.0 | ||
*/ | ||
class TemporarilyRemovedConstantsUnitTest extends BaseSniffTest | ||
{ | ||
|
||
/** | ||
* Test re-added constant sniff | ||
* | ||
* @param string $constantName Name of the PHP constant. | ||
* @param int[] $lines Line numbers in the test file which apply to this constant. | ||
* @param string $phpVersion PHP version (or range) to test with. | ||
* @param bool $shouldError If we expect to get an error or not from the sniff. | ||
* @param string $removedIn PHP version this feature was removed (for the error string). | ||
* @param string $addedIn PHP version this feature was re-addded (for the error string). | ||
* | ||
* @return void | ||
* @dataProvider dataReaddedConstants | ||
* @covers ::process() | ||
*/ | ||
public function testReaddedConstant($constantName, $lines, $phpVersion, $shouldError, $removedIn, $addedIn) | ||
{ | ||
$error = "The constant \"$constantName\" was removed in PHP $removedIn and re-added in PHP $addedIn."; | ||
|
||
$file = $this->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); | ||
} | ||
} |