Skip to content

Commit bedaab5

Browse files
authored
Merge pull request #1220 from PHPCSStandards/phpcs-4.0/feature/modernize-use-doublecolon-class
4.0 | Modernize: use `::class` resolution
2 parents b8d295e + 28f4fea commit bedaab5

20 files changed

+56
-59
lines changed

src/Files/FileList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHP_CodeSniffer\Autoload;
1818
use PHP_CodeSniffer\Config;
1919
use PHP_CodeSniffer\Exceptions\DeepExitException;
20+
use PHP_CodeSniffer\Filters\Filter;
2021
use PHP_CodeSniffer\Ruleset;
2122
use PHP_CodeSniffer\Util\Common;
2223
use PHP_CodeSniffer\Util\ExitCode;
@@ -156,7 +157,7 @@ private function getFilterClass()
156157
$filterType = $this->config->filter;
157158

158159
if ($filterType === null) {
159-
$filterClass = '\PHP_CodeSniffer\Filters\Filter';
160+
$filterClass = Filter::class;
160161
} else {
161162
if (strpos($filterType, '.') !== false) {
162163
// This is a path to a custom filter class.

src/Filters/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function accept()
151151
#[ReturnTypeWillChange]
152152
public function getChildren()
153153
{
154-
$filterClass = get_called_class();
154+
$filterClass = static::class;
155155
$children = new $filterClass(
156156
new RecursiveDirectoryIterator($this->current(), (RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS)),
157157
$this->basedir,

src/Fixer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public function beginChangeset()
404404

405405
if (PHP_CODESNIFFER_VERBOSITY > 1) {
406406
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
407-
if ($bt[1]['class'] === __CLASS__) {
407+
if ($bt[1]['class'] === self::class) {
408408
$sniff = 'Fixer';
409409
} else {
410410
$sniff = $this->getSniffCodeForDebug($bt[1]['class']);
@@ -478,7 +478,7 @@ public function rollbackChangeset()
478478
if (empty($this->changeset) === false) {
479479
if (PHP_CODESNIFFER_VERBOSITY > 1) {
480480
$bt = debug_backtrace();
481-
if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') {
481+
if ($bt[1]['class'] === self::class) {
482482
$sniff = $bt[2]['class'];
483483
$line = $bt[1]['line'];
484484
} else {
@@ -531,7 +531,7 @@ public function replaceToken($stackPtr, $content)
531531

532532
if (PHP_CODESNIFFER_VERBOSITY > 1) {
533533
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
534-
if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') {
534+
if ($bt[1]['class'] === self::class) {
535535
$sniff = $bt[2]['class'];
536536
$line = $bt[1]['line'];
537537
} else {
@@ -636,7 +636,7 @@ public function revertToken($stackPtr)
636636

637637
if (PHP_CODESNIFFER_VERBOSITY > 1) {
638638
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
639-
if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') {
639+
if ($bt[1]['class'] === self::class) {
640640
$sniff = $bt[2]['class'];
641641
$line = $bt[1]['line'];
642642
} else {

src/Ruleset.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use InvalidArgumentException;
1515
use PHP_CodeSniffer\Exceptions\RuntimeException;
1616
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
17+
use PHP_CodeSniffer\Sniffs\Sniff;
1718
use PHP_CodeSniffer\Util\Common;
1819
use PHP_CodeSniffer\Util\MessageCollector;
1920
use PHP_CodeSniffer\Util\Standards;
@@ -1470,7 +1471,7 @@ public function registerSniffs($files, $restrictions, $exclusions)
14701471
continue;
14711472
}
14721473

1473-
if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\Sniff') === false) {
1474+
if ($reflection->implementsInterface(Sniff::class) === false) {
14741475
$message = 'All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL;
14751476
$message .= "Interface not implemented for sniff $className.".PHP_EOL;
14761477
$message .= 'Contact the sniff author to fix the sniff.';
@@ -1486,7 +1487,7 @@ public function registerSniffs($files, $restrictions, $exclusions)
14861487
&& empty($value) === false
14871488
&& in_array('PHP', $value, true) === false
14881489
) {
1489-
if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\DeprecatedSniff') === true) {
1490+
if ($reflection->implementsInterface(DeprecatedSniff::class) === true) {
14901491
// Silently ignore the sniff if the sniff is marked as deprecated.
14911492
continue;
14921493
}

src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ protected function processTags($phpcsFile, $stackPtr, $commentStart)
232232
{
233233
$tokens = $phpcsFile->getTokens();
234234

235-
if (get_class($this) === 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff') {
235+
if (static::class === 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff') {
236236
$docBlock = 'file';
237237
} else {
238238
$docBlock = 'class';

tests/ConfigDouble.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function preventAutoDiscoveryScreenWidth()
182182
*/
183183
private function getStaticConfigProperty($name)
184184
{
185-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
185+
$property = new ReflectionProperty(Config::class, $name);
186186
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
187187

188188
if ($name === 'overriddenDefaults') {
@@ -204,7 +204,7 @@ private function getStaticConfigProperty($name)
204204
*/
205205
private function setStaticConfigProperty($name, $value)
206206
{
207-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
207+
$property = new ReflectionProperty(Config::class, $name);
208208
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
209209

210210
if ($name === 'overriddenDefaults') {

tests/Core/AbstractMethodTestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Tests\Core;
1111

1212
use Exception;
13+
use PHP_CodeSniffer\Exceptions\RuntimeException;
1314
use PHP_CodeSniffer\Files\File;
1415
use PHP_CodeSniffer\Files\LocalFile;
1516
use PHP_CodeSniffer\Ruleset;
@@ -54,7 +55,7 @@ public static function setUpBeforeClass(): void
5455
$ruleset = new Ruleset($config);
5556

5657
// Default to a file with the same name as the test class. Extension is property based.
57-
$relativeCN = str_replace(__NAMESPACE__, '', get_called_class());
58+
$relativeCN = str_replace(__NAMESPACE__, '', static::class);
5859
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN);
5960
$pathToTestFile = realpath(__DIR__).$relativePath.'.inc';
6061

@@ -235,7 +236,7 @@ public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $
235236
*/
236237
public function expectRunTimeException($message)
237238
{
238-
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
239+
$this->expectException(RuntimeException::class);
239240
$this->expectExceptionMessage($message);
240241

241242
}//end expectRunTimeException()

tests/Core/Config/AbstractRealConfigTestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace PHP_CodeSniffer\Tests\Core\Config;
1212

13+
use PHP_CodeSniffer\Config;
1314
use PHPUnit\Framework\TestCase;
1415
use ReflectionProperty;
1516

@@ -73,7 +74,7 @@ public static function tearDownAfterClass(): void
7374
*/
7475
protected static function setStaticConfigProperty($name, $value)
7576
{
76-
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
77+
$property = new ReflectionProperty(Config::class, $name);
7778
(PHP_VERSION_ID < 80100) && $property->setAccessible(true);
7879
$property->setValue(null, $value);
7980
(PHP_VERSION_ID < 80100) && $property->setAccessible(false);

tests/Core/Config/ExtensionsArgTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace PHP_CodeSniffer\Tests\Core\Config;
1010

11+
use PHP_CodeSniffer\Exceptions\DeepExitException;
1112
use PHP_CodeSniffer\Tests\ConfigDouble;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -134,7 +135,7 @@ public function testInvalidExtensions($passedValue)
134135
$message .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
135136
$message .= 'Received: '.$passedValue.PHP_EOL.PHP_EOL;
136137

137-
$this->expectException('PHP_CodeSniffer\Exceptions\DeepExitException');
138+
$this->expectException(DeepExitException::class);
138139
$this->expectExceptionMessage($message);
139140

140141
new ConfigDouble(["--extensions={$passedValue}"]);

tests/Core/Config/GeneratorArgTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace PHP_CodeSniffer\Tests\Core\Config;
1010

11+
use PHP_CodeSniffer\Exceptions\DeepExitException;
1112
use PHP_CodeSniffer\Tests\ConfigDouble;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -123,10 +124,9 @@ public function testOnlySetOnce()
123124
*/
124125
public function testInvalidGenerator($generatorName)
125126
{
126-
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
127-
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.';
127+
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.';
128128

129-
$this->expectException($exception);
129+
$this->expectException(DeepExitException::class);
130130
$this->expectExceptionMessage($message);
131131

132132
new ConfigDouble(["--generator={$generatorName}"]);

0 commit comments

Comments
 (0)