From 28f4feaf60023b32e9fb85e3626f6b15c7a3597d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 9 Sep 2025 14:27:34 +0200 Subject: [PATCH] 4.0 | Modernize: use `::class` resolution ... which is a PHP 5.5+ syntax and can now be used what with support for PHP < 7.2 having been dropped. Note: I have not applied this change to test expectations in data providers. --- src/Files/FileList.php | 3 ++- src/Filters/Filter.php | 2 +- src/Fixer.php | 8 ++++---- src/Ruleset.php | 5 +++-- .../PEAR/Sniffs/Commenting/FileCommentSniff.php | 2 +- tests/ConfigDouble.php | 4 ++-- tests/Core/AbstractMethodTestCase.php | 5 +++-- tests/Core/Config/AbstractRealConfigTestCase.php | 3 ++- tests/Core/Config/ExtensionsArgTest.php | 3 ++- tests/Core/Config/GeneratorArgTest.php | 6 +++--- tests/Core/Config/SniffsExcludeArgsTest.php | 14 +++++++------- tests/Core/Filters/GitModifiedTest.php | 4 ++-- tests/Core/Filters/GitStagedTest.php | 4 ++-- tests/Core/Ruleset/AbstractRulesetTestCase.php | 14 ++++---------- .../Core/Tokenizers/AbstractTokenizerTestCase.php | 5 +++-- .../Tokenizer/ReplaceTabsInTokenTestCase.php | 2 +- tests/Core/Util/Common/GetSniffCodeTest.php | 11 +++++------ tests/Core/Util/Help/HelpTest.php | 6 +++--- .../Util/MessageCollector/MessageCollectorTest.php | 12 +++++------- tests/Standards/AbstractSniffTestCase.php | 2 +- 20 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/Files/FileList.php b/src/Files/FileList.php index dc3dce3e2a..3ccc8a04f6 100644 --- a/src/Files/FileList.php +++ b/src/Files/FileList.php @@ -17,6 +17,7 @@ use PHP_CodeSniffer\Autoload; use PHP_CodeSniffer\Config; use PHP_CodeSniffer\Exceptions\DeepExitException; +use PHP_CodeSniffer\Filters\Filter; use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Util\Common; use PHP_CodeSniffer\Util\ExitCode; @@ -156,7 +157,7 @@ private function getFilterClass() $filterType = $this->config->filter; if ($filterType === null) { - $filterClass = '\PHP_CodeSniffer\Filters\Filter'; + $filterClass = Filter::class; } else { if (strpos($filterType, '.') !== false) { // This is a path to a custom filter class. diff --git a/src/Filters/Filter.php b/src/Filters/Filter.php index 81a9af5b7a..a136869110 100644 --- a/src/Filters/Filter.php +++ b/src/Filters/Filter.php @@ -151,7 +151,7 @@ public function accept() #[ReturnTypeWillChange] public function getChildren() { - $filterClass = get_called_class(); + $filterClass = static::class; $children = new $filterClass( new RecursiveDirectoryIterator($this->current(), (RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS)), $this->basedir, diff --git a/src/Fixer.php b/src/Fixer.php index 4a47c8cf67..63b5044c09 100644 --- a/src/Fixer.php +++ b/src/Fixer.php @@ -404,7 +404,7 @@ public function beginChangeset() if (PHP_CODESNIFFER_VERBOSITY > 1) { $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - if ($bt[1]['class'] === __CLASS__) { + if ($bt[1]['class'] === self::class) { $sniff = 'Fixer'; } else { $sniff = $this->getSniffCodeForDebug($bt[1]['class']); @@ -478,7 +478,7 @@ public function rollbackChangeset() if (empty($this->changeset) === false) { if (PHP_CODESNIFFER_VERBOSITY > 1) { $bt = debug_backtrace(); - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { + if ($bt[1]['class'] === self::class) { $sniff = $bt[2]['class']; $line = $bt[1]['line']; } else { @@ -531,7 +531,7 @@ public function replaceToken($stackPtr, $content) if (PHP_CODESNIFFER_VERBOSITY > 1) { $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { + if ($bt[1]['class'] === self::class) { $sniff = $bt[2]['class']; $line = $bt[1]['line']; } else { @@ -636,7 +636,7 @@ public function revertToken($stackPtr) if (PHP_CODESNIFFER_VERBOSITY > 1) { $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - if ($bt[1]['class'] === 'PHP_CodeSniffer\Fixer') { + if ($bt[1]['class'] === self::class) { $sniff = $bt[2]['class']; $line = $bt[1]['line']; } else { diff --git a/src/Ruleset.php b/src/Ruleset.php index 60392c922d..7225d6c8b5 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -14,6 +14,7 @@ use InvalidArgumentException; use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Sniffs\DeprecatedSniff; +use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Common; use PHP_CodeSniffer\Util\MessageCollector; use PHP_CodeSniffer\Util\Standards; @@ -1470,7 +1471,7 @@ public function registerSniffs($files, $restrictions, $exclusions) continue; } - if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\Sniff') === false) { + if ($reflection->implementsInterface(Sniff::class) === false) { $message = 'All sniffs must implement the PHP_CodeSniffer\\Sniffs\\Sniff interface.'.PHP_EOL; $message .= "Interface not implemented for sniff $className.".PHP_EOL; $message .= 'Contact the sniff author to fix the sniff.'; @@ -1486,7 +1487,7 @@ public function registerSniffs($files, $restrictions, $exclusions) && empty($value) === false && in_array('PHP', $value, true) === false ) { - if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\DeprecatedSniff') === true) { + if ($reflection->implementsInterface(DeprecatedSniff::class) === true) { // Silently ignore the sniff if the sniff is marked as deprecated. continue; } diff --git a/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php b/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php index 29a720cd95..0e0da3e7f9 100644 --- a/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php +++ b/src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php @@ -232,7 +232,7 @@ protected function processTags($phpcsFile, $stackPtr, $commentStart) { $tokens = $phpcsFile->getTokens(); - if (get_class($this) === 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff') { + if (static::class === 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FileCommentSniff') { $docBlock = 'file'; } else { $docBlock = 'class'; diff --git a/tests/ConfigDouble.php b/tests/ConfigDouble.php index 4ea3b8871a..975df462a7 100644 --- a/tests/ConfigDouble.php +++ b/tests/ConfigDouble.php @@ -182,7 +182,7 @@ private function preventAutoDiscoveryScreenWidth() */ private function getStaticConfigProperty($name) { - $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); + $property = new ReflectionProperty(Config::class, $name); (PHP_VERSION_ID < 80100) && $property->setAccessible(true); if ($name === 'overriddenDefaults') { @@ -204,7 +204,7 @@ private function getStaticConfigProperty($name) */ private function setStaticConfigProperty($name, $value) { - $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); + $property = new ReflectionProperty(Config::class, $name); (PHP_VERSION_ID < 80100) && $property->setAccessible(true); if ($name === 'overriddenDefaults') { diff --git a/tests/Core/AbstractMethodTestCase.php b/tests/Core/AbstractMethodTestCase.php index 9b4b8dbff6..e4a1d05258 100644 --- a/tests/Core/AbstractMethodTestCase.php +++ b/tests/Core/AbstractMethodTestCase.php @@ -10,6 +10,7 @@ namespace PHP_CodeSniffer\Tests\Core; use Exception; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Files\LocalFile; use PHP_CodeSniffer\Ruleset; @@ -54,7 +55,7 @@ public static function setUpBeforeClass(): void $ruleset = new Ruleset($config); // Default to a file with the same name as the test class. Extension is property based. - $relativeCN = str_replace(__NAMESPACE__, '', get_called_class()); + $relativeCN = str_replace(__NAMESPACE__, '', static::class); $relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN); $pathToTestFile = realpath(__DIR__).$relativePath.'.inc'; @@ -235,7 +236,7 @@ public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $ */ public function expectRunTimeException($message) { - $this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException'); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage($message); }//end expectRunTimeException() diff --git a/tests/Core/Config/AbstractRealConfigTestCase.php b/tests/Core/Config/AbstractRealConfigTestCase.php index 791c2db3a5..f316f4aa85 100644 --- a/tests/Core/Config/AbstractRealConfigTestCase.php +++ b/tests/Core/Config/AbstractRealConfigTestCase.php @@ -10,6 +10,7 @@ namespace PHP_CodeSniffer\Tests\Core\Config; +use PHP_CodeSniffer\Config; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -73,7 +74,7 @@ public static function tearDownAfterClass(): void */ protected static function setStaticConfigProperty($name, $value) { - $property = new ReflectionProperty('PHP_CodeSniffer\Config', $name); + $property = new ReflectionProperty(Config::class, $name); (PHP_VERSION_ID < 80100) && $property->setAccessible(true); $property->setValue(null, $value); (PHP_VERSION_ID < 80100) && $property->setAccessible(false); diff --git a/tests/Core/Config/ExtensionsArgTest.php b/tests/Core/Config/ExtensionsArgTest.php index eaf408fe75..ca951c087b 100644 --- a/tests/Core/Config/ExtensionsArgTest.php +++ b/tests/Core/Config/ExtensionsArgTest.php @@ -8,6 +8,7 @@ namespace PHP_CodeSniffer\Tests\Core\Config; +use PHP_CodeSniffer\Exceptions\DeepExitException; use PHP_CodeSniffer\Tests\ConfigDouble; use PHPUnit\Framework\TestCase; @@ -134,7 +135,7 @@ public function testInvalidExtensions($passedValue) $message .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL; $message .= 'Received: '.$passedValue.PHP_EOL.PHP_EOL; - $this->expectException('PHP_CodeSniffer\Exceptions\DeepExitException'); + $this->expectException(DeepExitException::class); $this->expectExceptionMessage($message); new ConfigDouble(["--extensions={$passedValue}"]); diff --git a/tests/Core/Config/GeneratorArgTest.php b/tests/Core/Config/GeneratorArgTest.php index 42778872a4..eeda822120 100644 --- a/tests/Core/Config/GeneratorArgTest.php +++ b/tests/Core/Config/GeneratorArgTest.php @@ -8,6 +8,7 @@ namespace PHP_CodeSniffer\Tests\Core\Config; +use PHP_CodeSniffer\Exceptions\DeepExitException; use PHP_CodeSniffer\Tests\ConfigDouble; use PHPUnit\Framework\TestCase; @@ -123,10 +124,9 @@ public function testOnlySetOnce() */ public function testInvalidGenerator($generatorName) { - $exception = 'PHP_CodeSniffer\Exceptions\DeepExitException'; - $message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.'; + $message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.'; - $this->expectException($exception); + $this->expectException(DeepExitException::class); $this->expectExceptionMessage($message); new ConfigDouble(["--generator={$generatorName}"]); diff --git a/tests/Core/Config/SniffsExcludeArgsTest.php b/tests/Core/Config/SniffsExcludeArgsTest.php index 6543265785..a8f78b1189 100644 --- a/tests/Core/Config/SniffsExcludeArgsTest.php +++ b/tests/Core/Config/SniffsExcludeArgsTest.php @@ -8,6 +8,7 @@ namespace PHP_CodeSniffer\Tests\Core\Config; +use PHP_CodeSniffer\Exceptions\DeepExitException; use PHP_CodeSniffer\Tests\ConfigDouble; use PHPUnit\Framework\TestCase; @@ -39,12 +40,11 @@ public function testInvalid($argument, $value, $errors, $suggestion) $cmd = 'phpcbf'; } - $exception = 'PHP_CodeSniffer\Exceptions\DeepExitException'; - $message = 'ERROR: The --'.$argument.' option only supports sniff codes.'.PHP_EOL; - $message .= 'Sniff codes are in the form "Standard.Category.Sniff".'.PHP_EOL; - $message .= PHP_EOL; - $message .= 'The following problems were detected:'.PHP_EOL; - $message .= '* '.implode(PHP_EOL.'* ', $errors).PHP_EOL; + $message = 'ERROR: The --'.$argument.' option only supports sniff codes.'.PHP_EOL; + $message .= 'Sniff codes are in the form "Standard.Category.Sniff".'.PHP_EOL; + $message .= PHP_EOL; + $message .= 'The following problems were detected:'.PHP_EOL; + $message .= '* '.implode(PHP_EOL.'* ', $errors).PHP_EOL; if ($suggestion !== null) { $message .= PHP_EOL; @@ -55,7 +55,7 @@ public function testInvalid($argument, $value, $errors, $suggestion) $message .= "Run \"{$cmd} --help\" for usage information".PHP_EOL; $message .= PHP_EOL; - $this->expectException($exception); + $this->expectException(DeepExitException::class); $this->expectExceptionMessage($message); new ConfigDouble(["--$argument=$value"]); diff --git a/tests/Core/Filters/GitModifiedTest.php b/tests/Core/Filters/GitModifiedTest.php index afb11d5945..365a2634d2 100644 --- a/tests/Core/Filters/GitModifiedTest.php +++ b/tests/Core/Filters/GitModifiedTest.php @@ -40,7 +40,7 @@ public function testFileNamePassesAsBasePathWillTranslateToDirname() self::$config, self::$ruleset, ]; - $mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitModified', $constructorArgs, ['exec']); + $mockObj = $this->getMockedClass(GitModified::class, $constructorArgs, ['exec']); $mockObj->expects($this->once()) ->method('exec') @@ -71,7 +71,7 @@ public function testAcceptOnlyGitModified($inputPaths, $outputGitModified, $expe self::$config, self::$ruleset, ]; - $mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitModified', $constructorArgs, ['exec']); + $mockObj = $this->getMockedClass(GitModified::class, $constructorArgs, ['exec']); $mockObj->expects($this->once()) ->method('exec') diff --git a/tests/Core/Filters/GitStagedTest.php b/tests/Core/Filters/GitStagedTest.php index 0bf2b74676..3d7d4f8c14 100644 --- a/tests/Core/Filters/GitStagedTest.php +++ b/tests/Core/Filters/GitStagedTest.php @@ -40,7 +40,7 @@ public function testFileNamePassesAsBasePathWillTranslateToDirname() self::$config, self::$ruleset, ]; - $mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitStaged', $constructorArgs, ['exec']); + $mockObj = $this->getMockedClass(GitStaged::class, $constructorArgs, ['exec']); $mockObj->expects($this->once()) ->method('exec') @@ -71,7 +71,7 @@ public function testAcceptOnlyGitStaged($inputPaths, $outputGitStaged, $expected self::$config, self::$ruleset, ]; - $mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitStaged', $constructorArgs, ['exec']); + $mockObj = $this->getMockedClass(GitStaged::class, $constructorArgs, ['exec']); $mockObj->expects($this->once()) ->method('exec') diff --git a/tests/Core/Ruleset/AbstractRulesetTestCase.php b/tests/Core/Ruleset/AbstractRulesetTestCase.php index 97930b094e..247d49784e 100644 --- a/tests/Core/Ruleset/AbstractRulesetTestCase.php +++ b/tests/Core/Ruleset/AbstractRulesetTestCase.php @@ -9,18 +9,12 @@ namespace PHP_CodeSniffer\Tests\Core\Ruleset; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHPUnit\Framework\TestCase; abstract class AbstractRulesetTestCase extends TestCase { - /** - * The fully qualified name of the PHPCS runtime exception class. - * - * @var string - */ - private const RUNTIME_EXCEPTION = 'PHP_CodeSniffer\Exceptions\RuntimeException'; - /** * Asserts that an object has a specified property in a PHPUnit cross-version compatible manner. @@ -75,7 +69,7 @@ protected function assertXObjectNotHasProperty($propertyName, $object, $message= */ protected function expectRuntimeExceptionMessage($message) { - $this->expectException(self::RUNTIME_EXCEPTION); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage($message); }//end expectRuntimeExceptionMessage() @@ -92,11 +86,11 @@ protected function expectRuntimeExceptionMessage($message) protected function expectRuntimeExceptionRegex($regex) { if (method_exists($this, 'expectExceptionMessageMatches') === true) { - $this->expectException(self::RUNTIME_EXCEPTION); + $this->expectException(RuntimeException::class); $this->expectExceptionMessageMatches($regex); } else { // PHPUnit < 8.4.0. - $this->expectException(self::RUNTIME_EXCEPTION); + $this->expectException(RuntimeException::class); $this->expectExceptionMessageRegExp($regex); } diff --git a/tests/Core/Tokenizers/AbstractTokenizerTestCase.php b/tests/Core/Tokenizers/AbstractTokenizerTestCase.php index cd8ce349f8..c0bd7496f9 100644 --- a/tests/Core/Tokenizers/AbstractTokenizerTestCase.php +++ b/tests/Core/Tokenizers/AbstractTokenizerTestCase.php @@ -16,6 +16,7 @@ use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Tests\ConfigDouble; use PHP_CodeSniffer\Tests\Core\AbstractMethodTestCase; +use PHP_CodeSniffer\Tokenizers\PHP; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -59,7 +60,7 @@ protected function setUp(): void $ruleset = new Ruleset($config); // Default to a file with the same name as the test class. Extension is property based. - $relativeCN = str_replace(__NAMESPACE__, '', get_called_class()); + $relativeCN = str_replace(__NAMESPACE__, '', static::class); $relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN); $pathToTestFile = realpath(__DIR__).$relativePath.'.inc'; @@ -120,7 +121,7 @@ protected function getTargetToken($commentString, $tokenType, $tokenContent=null */ public static function clearResolvedTokensCache() { - $property = new ReflectionProperty('PHP_CodeSniffer\Tokenizers\PHP', 'resolveTokenCache'); + $property = new ReflectionProperty(PHP::class, 'resolveTokenCache'); (PHP_VERSION_ID < 80100) && $property->setAccessible(true); $property->setValue(null, []); (PHP_VERSION_ID < 80100) && $property->setAccessible(false); diff --git a/tests/Core/Tokenizers/Tokenizer/ReplaceTabsInTokenTestCase.php b/tests/Core/Tokenizers/Tokenizer/ReplaceTabsInTokenTestCase.php index 883c0779ee..041da55f05 100644 --- a/tests/Core/Tokenizers/Tokenizer/ReplaceTabsInTokenTestCase.php +++ b/tests/Core/Tokenizers/Tokenizer/ReplaceTabsInTokenTestCase.php @@ -37,7 +37,7 @@ abstract class ReplaceTabsInTokenTestCase extends AbstractTokenizerTestCase */ public static function setUpBeforeClass(): void { - $relativeCN = str_replace(__NAMESPACE__.'\\', '', get_called_class()); + $relativeCN = str_replace(__NAMESPACE__.'\\', '', static::class); self::$caseFileName = __DIR__.DIRECTORY_SEPARATOR.$relativeCN.'.inc'; $baseFileName = realpath(__DIR__.'/ReplaceTabsInTokenTest.inc'); diff --git a/tests/Core/Util/Common/GetSniffCodeTest.php b/tests/Core/Util/Common/GetSniffCodeTest.php index a2e92a923f..36002b05b9 100644 --- a/tests/Core/Util/Common/GetSniffCodeTest.php +++ b/tests/Core/Util/Common/GetSniffCodeTest.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Tests\Core\Util\Common; +use InvalidArgumentException; use PHP_CodeSniffer\Util\Common; use PHPUnit\Framework\TestCase; @@ -32,10 +33,9 @@ final class GetSniffCodeTest extends TestCase */ public function testGetSniffCodeThrowsExceptionOnInvalidInput($input) { - $exception = 'InvalidArgumentException'; - $message = 'The $sniffClass parameter must be a non-empty string'; + $message = 'The $sniffClass parameter must be a non-empty string'; - $this->expectException($exception); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage($message); Common::getSniffCode($input); @@ -72,10 +72,9 @@ public static function dataGetSniffCodeThrowsExceptionOnInvalidInput() */ public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass($input) { - $exception = 'InvalidArgumentException'; - $message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:'; + $message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:'; - $this->expectException($exception); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage($message); Common::getSniffCode($input); diff --git a/tests/Core/Util/Help/HelpTest.php b/tests/Core/Util/Help/HelpTest.php index de55ebfa47..e2d2cc59b0 100644 --- a/tests/Core/Util/Help/HelpTest.php +++ b/tests/Core/Util/Help/HelpTest.php @@ -9,6 +9,7 @@ namespace PHP_CodeSniffer\Tests\Core\Util\Help; +use InvalidArgumentException; use PHP_CodeSniffer\Tests\ConfigDouble; use PHP_CodeSniffer\Util\Help; use PHPUnit\Framework\TestCase; @@ -139,10 +140,9 @@ public function testQaValidCategoryOptionDefinitions() */ public function testConstructorInvalidArgumentException() { - $exception = 'InvalidArgumentException'; - $message = 'The $shortOptions parameter must be a string'; + $message = 'The $shortOptions parameter must be a string'; - $this->expectException($exception); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage($message); new Help(new ConfigDouble(), [], []); diff --git a/tests/Core/Util/MessageCollector/MessageCollectorTest.php b/tests/Core/Util/MessageCollector/MessageCollectorTest.php index 5ae16d5642..3e23260e47 100644 --- a/tests/Core/Util/MessageCollector/MessageCollectorTest.php +++ b/tests/Core/Util/MessageCollector/MessageCollectorTest.php @@ -8,6 +8,8 @@ namespace PHP_CodeSniffer\Tests\Core\Util\MessageCollector; +use InvalidArgumentException; +use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Tests\Core\AbstractWriterTestCase; use PHP_CodeSniffer\Util\MessageCollector; @@ -31,10 +33,9 @@ final class MessageCollectorTest extends AbstractWriterTestCase */ public function testAddingNonStringMessageResultsInException($message) { - $exception = 'InvalidArgumentException'; $exceptionMsg = 'The $message should be of type string. Received: '; - $this->expectException($exception); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage($exceptionMsg); $msgCollector = new MessageCollector(); @@ -73,10 +74,9 @@ public static function dataAddingNonStringMessageResultsInException() */ public function testAddingMessageWithUnsupportedMessageTypeResultsInException($type) { - $exception = 'InvalidArgumentException'; $exceptionMsg = 'The message $type should be one of the predefined MessageCollector constants. Received: '; - $this->expectException($exception); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage($exceptionMsg); $msgCollector = new MessageCollector(); @@ -300,9 +300,7 @@ public static function dataDisplayingNonBlockingMessages() */ public function testDisplayingBlockingErrors($messages, $expected) { - $exception = 'PHP_CodeSniffer\Exceptions\RuntimeException'; - - $this->expectException($exception); + $this->expectException(RuntimeException::class); $this->expectExceptionMessage($expected); $msgCollector = new MessageCollector(); diff --git a/tests/Standards/AbstractSniffTestCase.php b/tests/Standards/AbstractSniffTestCase.php index beb9376574..872b09c114 100644 --- a/tests/Standards/AbstractSniffTestCase.php +++ b/tests/Standards/AbstractSniffTestCase.php @@ -150,7 +150,7 @@ final public function testSniff() $this->markTestSkipped(); } - $sniffCode = Common::getSniffCode(get_class($this)); + $sniffCode = Common::getSniffCode(static::class); $sniffCodeParts = explode('.', $sniffCode); $standardName = $sniffCodeParts[0];