Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/Files/FileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.';
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions tests/ConfigDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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') {
Expand Down
5 changes: 3 additions & 2 deletions tests/Core/AbstractMethodTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion tests/Core/Config/AbstractRealConfigTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHP_CodeSniffer\Tests\Core\Config;

use PHP_CodeSniffer\Config;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion tests/Core/Config/ExtensionsArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace PHP_CodeSniffer\Tests\Core\Config;

use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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}"]);
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/Config/GeneratorArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace PHP_CodeSniffer\Tests\Core\Config;

use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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}"]);
Expand Down
14 changes: 7 additions & 7 deletions tests/Core/Config/SniffsExcludeArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace PHP_CodeSniffer\Tests\Core\Config;

use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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;
Expand All @@ -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"]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Filters/GitModifiedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Filters/GitStagedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 4 additions & 10 deletions tests/Core/Ruleset/AbstractRulesetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions tests/Core/Tokenizers/AbstractTokenizerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 5 additions & 6 deletions tests/Core/Util/Common/GetSniffCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PHP_CodeSniffer\Tests\Core\Util\Common;

use InvalidArgumentException;
use PHP_CodeSniffer\Util\Common;
use PHPUnit\Framework\TestCase;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/Util/Help/HelpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(), [], []);
Expand Down
Loading
Loading