-
-
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
243 additions
and
15 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
102 changes: 102 additions & 0 deletions
102
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,102 @@ | ||
<?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\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 | ||
{ | ||
|
||
/** | ||
* A list of removed & re-added PHP Constants. | ||
* | ||
* Note: PHP Constants are case-sensitive! | ||
* | ||
* @since 10.0.0 | ||
* | ||
* @var array(string => array(string => string)) | ||
*/ | ||
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']) | ||
) { | ||
$phpcsFile->addError( | ||
'The constant "%s" was removed in PHP %s and re-added in PHP %s.', | ||
$stackPtr, | ||
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
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
<?php | ||
// Constants which were present, then removed, then present again. | ||
|
||
/* T_BAD_CHARACTER */ | ||
echo T_BAD_CHARACTER; // Constant itself. | ||
echo t_bad_character; // Not the uppercase constant. | ||
echo BAD_CHARACTER; // Not the right constant. | ||
echo My\NS\T_BAD_CHARACTER; // Not the global constant. | ||
echo self::T_BAD_CHARACTER; // Not the global constant. | ||
echo $obj->T_BAD_CHARACTER; // Not a constant. | ||
function T_BAD_CHARACTER() {} // Not a constant. | ||
echo \T_BAD_CHARACTER; // This is the global constant and should be flagged. |
125 changes: 125 additions & 0 deletions
125
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,125 @@ | ||
<?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\BaseSniffTestCase; | ||
|
||
/** | ||
* Test the ReaddedConstants sniff. | ||
* | ||
* @group constants | ||
* @covers \PHPCompatibility\Sniffs\Constants\TemporarilyRemovedConstantsSniff | ||
* @since 10.0.0 | ||
*/ | ||
class TemporarilyRemovedConstantsUnitTest extends BaseSniffTestCase | ||
{ | ||
|
||
/** | ||
* Test re-added constant sniff | ||
* | ||
* @dataProvider dataReaddedConstants | ||
* | ||
* @param string $constantName Name of the PHP constant. | ||
* @param int $line Line number in the test file which applies 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 | ||
*/ | ||
public function testReaddedConstant($constantName, $line, $phpVersion, $shouldError, $removedIn, $addedIn) | ||
{ | ||
$error = "The constant \"$constantName\" was removed in PHP $removedIn and re-added in PHP $addedIn."; | ||
|
||
$file = $this->sniffFile(__FILE__, $phpVersion); | ||
if ($shouldError) { | ||
$this->assertError($file, $line, $error); | ||
} else { | ||
$this->assertNoViolation($file, $line); | ||
} | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testReaddedConstant() | ||
* | ||
* @return array | ||
*/ | ||
public static function dataReaddedConstants() | ||
{ | ||
// 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 [ | ||
['T_BAD_CHARACTER', 5, '5.6', false, '7.0', '7.4'], // Last version before removal | ||
['T_BAD_CHARACTER', 5, '7.0', true, '7.0', '7.4'], // Removed | ||
['T_BAD_CHARACTER', 5, '7.1', true, '7.0', '7.4'], // Removed | ||
['T_BAD_CHARACTER', 5, '7.2', true, '7.0', '7.4'], // Removed | ||
['T_BAD_CHARACTER', 5, '7.3', true, '7.0', '7.4'], // Removed | ||
['T_BAD_CHARACTER', 5, '7.4', false, '7.0', '7.4'], // Added again | ||
|
||
['T_BAD_CHARACTER', 5, '-5.6', false, '7.0', '7.4'], // Before | ||
['T_BAD_CHARACTER', 5, '-7.2', true, '7.0', '7.4'], // Inside | ||
['T_BAD_CHARACTER', 5, '-8.2', true, '7.0', '7.4'], // After | ||
|
||
['T_BAD_CHARACTER', 5, '5.4-', true, '7.0', '7.4'], // Before | ||
['T_BAD_CHARACTER', 5, '7.2-', true, '7.0', '7.4'], // Inside | ||
['T_BAD_CHARACTER', 5, '7.4-', false, '7.0', '7.4'], // After | ||
|
||
['T_BAD_CHARACTER', 5, '5.0-5.6', false, '7.0', '7.4'], // Before | ||
['T_BAD_CHARACTER', 5, '7.4-8.3', false, '7.0', '7.4'], // After | ||
['T_BAD_CHARACTER', 5, '5.0-8.3', true, '7.0', '7.4'], // Inside and both sides | ||
['T_BAD_CHARACTER', 5, '5.0-7.2', true, '7.0', '7.4'], // Inside and before | ||
['T_BAD_CHARACTER', 5, '7.0-7.3', true, '7.0', '7.4'], // Inside only | ||
['T_BAD_CHARACTER', 5, '7.2-8.1', true, '7.0', '7.4'], // Inside and after | ||
|
||
// Other cases | ||
['T_BAD_CHARACTER', 6, '7.0', false, '7.0', '7.4'], | ||
['T_BAD_CHARACTER', 7, '7.0', false, '7.0', '7.4'], | ||
['T_BAD_CHARACTER', 8, '7.0', false, '7.0', '7.4'], | ||
['T_BAD_CHARACTER', 9, '7.0', false, '7.0', '7.4'], | ||
['T_BAD_CHARACTER', 10, '7.0', false, '7.0', '7.4'], | ||
['T_BAD_CHARACTER', 11, '7.0', false, '7.0', '7.4'], | ||
['T_BAD_CHARACTER', 12, '7.0', true, '7.0', '7.4'], | ||
]; | ||
} | ||
|
||
/** | ||
* Verify no notices are thrown at all. | ||
* | ||
* @dataProvider dataNoViolations | ||
* | ||
* @param string $phpVersion PHP version to test with. | ||
* | ||
* @return void | ||
*/ | ||
public function testNoViolationsInFileOnValidVersion($phpVersion) | ||
{ | ||
$file = $this->sniffFile(__FILE__, $phpVersion); | ||
$this->assertNoViolation($file); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testNoViolationsInFileOnValidVersion() | ||
* | ||
* @return array | ||
*/ | ||
public static function dataNoViolations() | ||
{ | ||
return [ | ||
['5.0'], // Low version below the first deprecation. | ||
['99.0'], // High version, after all constants have been re-added. | ||
]; | ||
} | ||
} |