Skip to content

Commit a550120

Browse files
committed
CS: always import all used classes [2]
.... instead of importing the namespace and using partially qualified names. Previously, this was not consistently applied, which led to near-miss bugs/code which only worked by accident, not by design, like in the `Config`, `Fixer` and `Runner` classes, where certain code only worked by the grace of the namespace of the class itself being the top-level `PHP_CodeSniffer` namespace. Note: the JS/CSS tokenizers and a sniff which has been removed in 4.0 have been excluded from this commit.
1 parent 941b60d commit a550120

File tree

9 files changed

+46
-43
lines changed

9 files changed

+46
-43
lines changed

src/Config.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHP_CodeSniffer\Exceptions\DeepExitException;
1818
use PHP_CodeSniffer\Exceptions\RuntimeException;
1919
use PHP_CodeSniffer\Util\Common;
20+
use PHP_CodeSniffer\Util\Standards;
2021

2122
/**
2223
* Stores the configuration used to run PHPCS and PHPCBF.
@@ -267,7 +268,7 @@ public function __set($name, $value)
267268
$cleaned = [];
268269

269270
// Check if the standard name is valid, or if the case is invalid.
270-
$installedStandards = Util\Standards::getInstalledStandards();
271+
$installedStandards = Standards::getInstalledStandards();
271272
foreach ($value as $standard) {
272273
foreach ($installedStandards as $validStandard) {
273274
if (strtolower($standard) === strtolower($validStandard)) {
@@ -422,7 +423,7 @@ public function __construct(array $cliArgs=[], $dieOnUnknownArg=true)
422423

423424
// Check for content on STDIN.
424425
if ($this->stdin === true
425-
|| (Util\Common::isStdinATTY() === false
426+
|| (Common::isStdinATTY() === false
426427
&& feof($handle) === false)
427428
) {
428429
$readStreams = [$handle];
@@ -649,7 +650,7 @@ public function processShortArgument($arg, $pos)
649650
throw new DeepExitException($output, 0);
650651
case 'i' :
651652
ob_start();
652-
Util\Standards::printInstalledStandards();
653+
Standards::printInstalledStandards();
653654
$output = ob_get_contents();
654655
ob_end_clean();
655656
throw new DeepExitException($output, 0);
@@ -922,7 +923,7 @@ public function processLongArgument($arg, $pos)
922923
$this->cache = true;
923924
$this->overriddenDefaults['cache'] = true;
924925

925-
$this->cacheFile = Util\Common::realpath(substr($arg, 6));
926+
$this->cacheFile = Common::realpath(substr($arg, 6));
926927

927928
// It may not exist and return false instead.
928929
if ($this->cacheFile === false) {
@@ -941,9 +942,9 @@ public function processLongArgument($arg, $pos)
941942
} else {
942943
if ($dir[0] === '/') {
943944
// An absolute path.
944-
$dir = Util\Common::realpath($dir);
945+
$dir = Common::realpath($dir);
945946
} else {
946-
$dir = Util\Common::realpath(getcwd().'/'.$dir);
947+
$dir = Common::realpath(getcwd().'/'.$dir);
947948
}
948949

949950
if ($dir !== false) {
@@ -964,7 +965,7 @@ public function processLongArgument($arg, $pos)
964965
$files = explode(',', substr($arg, 10));
965966
$bootstrap = [];
966967
foreach ($files as $file) {
967-
$path = Util\Common::realpath($file);
968+
$path = Common::realpath($file);
968969
if ($path === false) {
969970
$error = 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL;
970971
$error .= $this->printShortUsage(true);
@@ -978,7 +979,7 @@ public function processLongArgument($arg, $pos)
978979
$this->overriddenDefaults['bootstrap'] = true;
979980
} else if (substr($arg, 0, 10) === 'file-list=') {
980981
$fileList = substr($arg, 10);
981-
$path = Util\Common::realpath($fileList);
982+
$path = Common::realpath($fileList);
982983
if ($path === false) {
983984
$error = 'ERROR: The specified file list "'.$fileList.'" does not exist'.PHP_EOL.PHP_EOL;
984985
$error .= $this->printShortUsage(true);
@@ -1001,7 +1002,7 @@ public function processLongArgument($arg, $pos)
10011002
break;
10021003
}
10031004

1004-
$this->stdinPath = Util\Common::realpath(substr($arg, 11));
1005+
$this->stdinPath = Common::realpath(substr($arg, 11));
10051006

10061007
// It may not exist and return false instead, so use whatever they gave us.
10071008
if ($this->stdinPath === false) {
@@ -1014,13 +1015,13 @@ public function processLongArgument($arg, $pos)
10141015
break;
10151016
}
10161017

1017-
$this->reportFile = Util\Common::realpath(substr($arg, 12));
1018+
$this->reportFile = Common::realpath(substr($arg, 12));
10181019

10191020
// It may not exist and return false instead.
10201021
if ($this->reportFile === false) {
10211022
$this->reportFile = substr($arg, 12);
10221023

1023-
$dir = Util\Common::realpath(dirname($this->reportFile));
1024+
$dir = Common::realpath(dirname($this->reportFile));
10241025
if (is_dir($dir) === false) {
10251026
$error = 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
10261027
$error .= $this->printShortUsage(true);
@@ -1056,7 +1057,7 @@ public function processLongArgument($arg, $pos)
10561057
break;
10571058
}
10581059

1059-
$this->basepath = Util\Common::realpath(substr($arg, 9));
1060+
$this->basepath = Common::realpath(substr($arg, 9));
10601061

10611062
// It may not exist and return false instead.
10621063
if ($this->basepath === false) {
@@ -1083,7 +1084,7 @@ public function processLongArgument($arg, $pos)
10831084
if ($output === false) {
10841085
$output = null;
10851086
} else {
1086-
$dir = Util\Common::realpath(dirname($output));
1087+
$dir = Common::realpath(dirname($output));
10871088
if (is_dir($dir) === false) {
10881089
$error = 'ERROR: The specified '.$report.' report file path "'.$output.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
10891090
$error .= $this->printShortUsage(true);
@@ -1299,7 +1300,7 @@ public function processFilePath($path)
12991300
return;
13001301
}
13011302

1302-
$file = Util\Common::realpath($path);
1303+
$file = Common::realpath($path);
13031304
if (file_exists($file) === false) {
13041305
if ($this->dieOnUnknownArg === false) {
13051306
return;
@@ -1633,7 +1634,7 @@ public function setConfigData($key, $value, $temp=false)
16331634
// If the installed paths are being set, make sure all known
16341635
// standards paths are added to the autoloader.
16351636
if ($key === 'installed_paths') {
1636-
$installedStandards = Util\Standards::getInstalledStandardDetails();
1637+
$installedStandards = Standards::getInstalledStandardDetails();
16371638
foreach ($installedStandards as $name => $details) {
16381639
Autoload::addSearchPath($details['path'], $details['namespace']);
16391640
}

src/Files/FileList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use PHP_CodeSniffer\Config;
1919
use PHP_CodeSniffer\Exceptions\DeepExitException;
2020
use PHP_CodeSniffer\Ruleset;
21-
use PHP_CodeSniffer\Util;
21+
use PHP_CodeSniffer\Util\Common;
2222
use RecursiveArrayIterator;
2323
use RecursiveDirectoryIterator;
2424
use RecursiveIteratorIterator;
@@ -78,7 +78,7 @@ public function __construct(Config $config, Ruleset $ruleset)
7878

7979
$paths = $config->files;
8080
foreach ($paths as $path) {
81-
$isPharFile = Util\Common::isPharFile($path);
81+
$isPharFile = Common::isPharFile($path);
8282
if (is_dir($path) === true || $isPharFile === true) {
8383
if ($isPharFile === true) {
8484
$path = 'phar://'.$path;

src/Filters/ExactMatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace PHP_CodeSniffer\Filters;
1313

14-
use PHP_CodeSniffer\Util;
14+
use PHP_CodeSniffer\Util\Common;
1515

1616
abstract class ExactMatch extends Filter
1717
{
@@ -64,7 +64,7 @@ public function accept()
6464
}
6565
}
6666

67-
$filePath = Util\Common::realpath($this->current());
67+
$filePath = Common::realpath($this->current());
6868

6969
// If a file is both disallowed and allowed, the disallowed files list takes precedence.
7070
if (isset($this->disallowedFiles[$filePath]) === true) {

src/Filters/Filter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use FilesystemIterator;
1313
use PHP_CodeSniffer\Config;
1414
use PHP_CodeSniffer\Ruleset;
15-
use PHP_CodeSniffer\Util;
15+
use PHP_CodeSniffer\Util\Common;
1616
use RecursiveDirectoryIterator;
1717
use RecursiveFilterIterator;
1818
use ReturnTypeWillChange;
@@ -89,7 +89,7 @@ public function __construct($iterator, $basedir, Config $config, Ruleset $rulese
8989
$this->config = $config;
9090
$this->ruleset = $ruleset;
9191

92-
if (is_dir($basedir) === true || Util\Common::isPharFile($basedir) === true) {
92+
if (is_dir($basedir) === true || Common::isPharFile($basedir) === true) {
9393
$this->isBasedirDir = true;
9494
}
9595

@@ -108,7 +108,7 @@ public function __construct($iterator, $basedir, Config $config, Ruleset $rulese
108108
public function accept()
109109
{
110110
$filePath = $this->current();
111-
$realPath = Util\Common::realpath($filePath);
111+
$realPath = Common::realpath($filePath);
112112

113113
if ($realPath !== false) {
114114
// It's a real path somewhere, so record it

src/Filters/GitModified.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace PHP_CodeSniffer\Filters;
1111

12-
use PHP_CodeSniffer\Util;
12+
use PHP_CodeSniffer\Util\Common;
1313

1414
class GitModified extends ExactMatch
1515
{
@@ -65,7 +65,7 @@ protected function getAllowedFiles()
6565
}
6666

6767
foreach ($output as $path) {
68-
$path = Util\Common::realpath($path);
68+
$path = Common::realpath($path);
6969

7070
if ($path === false) {
7171
continue;

src/Filters/GitStaged.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace PHP_CodeSniffer\Filters;
1313

14-
use PHP_CodeSniffer\Util;
14+
use PHP_CodeSniffer\Util\Common;
1515

1616
class GitStaged extends ExactMatch
1717
{
@@ -67,7 +67,7 @@ protected function getAllowedFiles()
6767
}
6868

6969
foreach ($output as $path) {
70-
$path = Util\Common::realpath($path);
70+
$path = Common::realpath($path);
7171
if ($path === false) {
7272
// Skip deleted files.
7373
continue;

src/Fixer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function beginChangeset()
359359
if ($bt[1]['class'] === __CLASS__) {
360360
$sniff = 'Fixer';
361361
} else {
362-
$sniff = Util\Common::getSniffCode($bt[1]['class']);
362+
$sniff = Common::getSniffCode($bt[1]['class']);
363363
}
364364

365365
$line = $bt[0]['line'];
@@ -438,7 +438,7 @@ public function rollbackChangeset()
438438
$line = $bt[0]['line'];
439439
}
440440

441-
$sniff = Util\Common::getSniffCode($sniff);
441+
$sniff = Common::getSniffCode($sniff);
442442

443443
$numChanges = count($this->changeset);
444444

@@ -491,7 +491,7 @@ public function replaceToken($stackPtr, $content)
491491
$line = $bt[0]['line'];
492492
}
493493

494-
$sniff = Util\Common::getSniffCode($sniff);
494+
$sniff = Common::getSniffCode($sniff);
495495

496496
$tokens = $this->currentFile->getTokens();
497497
$type = $tokens[$stackPtr]['type'];
@@ -596,7 +596,7 @@ public function revertToken($stackPtr)
596596
$line = $bt[0]['line'];
597597
}
598598

599-
$sniff = Util\Common::getSniffCode($sniff);
599+
$sniff = Common::getSniffCode($sniff);
600600

601601
$tokens = $this->currentFile->getTokens();
602602
$type = $tokens[$stackPtr]['type'];

src/Reports/Code.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Exception;
1313
use PHP_CodeSniffer\Files\File;
14-
use PHP_CodeSniffer\Util;
14+
use PHP_CodeSniffer\Util\Common;
1515

1616
class Code implements Report
1717
{
@@ -46,9 +46,9 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
4646
if (empty($tokens) === true) {
4747
if (PHP_CODESNIFFER_VERBOSITY === 1) {
4848
$startTime = microtime(true);
49-
Util\Common::forcePrintStatusMessage('CODE report is parsing '.basename($file).' ', 0, true);
49+
Common::forcePrintStatusMessage('CODE report is parsing '.basename($file).' ', 0, true);
5050
} else if (PHP_CODESNIFFER_VERBOSITY > 1) {
51-
Util\Common::forcePrintStatusMessage("CODE report is forcing parse of $file", 0);
51+
Common::forcePrintStatusMessage("CODE report is forcing parse of $file", 0);
5252
}
5353

5454
try {
@@ -62,10 +62,10 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
6262
$timeTaken = ((microtime(true) - $startTime) * 1000);
6363
if ($timeTaken < 1000) {
6464
$timeTaken = round($timeTaken);
65-
Util\Common::forcePrintStatusMessage("DONE in {$timeTaken}ms", 0);
65+
Common::forcePrintStatusMessage("DONE in {$timeTaken}ms", 0);
6666
} else {
6767
$timeTaken = round(($timeTaken / 1000), 2);
68-
Util\Common::forcePrintStatusMessage("DONE in $timeTaken secs", 0);
68+
Common::forcePrintStatusMessage("DONE in $timeTaken secs", 0);
6969
}
7070
}
7171

@@ -234,7 +234,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
234234
$tokenContent = $token['content'];
235235
}
236236

237-
$tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]);
237+
$tokenContent = Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]);
238238
$tokenContent = str_replace("\000", ' ', $tokenContent);
239239

240240
$underline = false;

src/Runner.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use PHP_CodeSniffer\Util\Cache;
2222
use PHP_CodeSniffer\Util\Common;
2323
use PHP_CodeSniffer\Util\Standards;
24+
use PHP_CodeSniffer\Util\Timing;
25+
use PHP_CodeSniffer\Util\Tokens;
2426

2527
class Runner
2628
{
@@ -57,7 +59,7 @@ public function runPHPCS()
5759
$this->registerOutOfMemoryShutdownMessage('phpcs');
5860

5961
try {
60-
Util\Timing::startTiming();
62+
Timing::startTiming();
6163
Runner::checkRequirements();
6264

6365
if (defined('PHP_CODESNIFFER_CBF') === false) {
@@ -119,7 +121,7 @@ public function runPHPCS()
119121
// Print all the reports for this run.
120122
$this->reporter->printReports();
121123

122-
Util\Timing::printRunTime();
124+
Timing::printRunTime();
123125
} catch (DeepExitException $e) {
124126
echo $e->getMessage();
125127
return $e->getCode();
@@ -153,7 +155,7 @@ public function runPHPCBF()
153155
}
154156

155157
try {
156-
Util\Timing::startTiming();
158+
Timing::startTiming();
157159
Runner::checkRequirements();
158160

159161
// Creating the Config object populates it with all required settings
@@ -204,7 +206,7 @@ public function runPHPCBF()
204206
$this->reporter->printReports();
205207

206208
echo PHP_EOL;
207-
Util\Timing::printRunTime();
209+
Timing::printRunTime();
208210
} catch (DeepExitException $e) {
209211
echo $e->getMessage();
210212
return $e->getCode();
@@ -297,12 +299,12 @@ public function init()
297299

298300
// Check that the standards are valid.
299301
foreach ($this->config->standards as $standard) {
300-
if (Util\Standards::isInstalledStandard($standard) === false) {
302+
if (Standards::isInstalledStandard($standard) === false) {
301303
// They didn't select a valid coding standard, so help them
302304
// out by letting them know which standards are installed.
303305
$error = 'ERROR: the "'.$standard.'" coding standard is not installed. ';
304306
ob_start();
305-
Util\Standards::printInstalledStandards();
307+
Standards::printInstalledStandards();
306308
$error .= ob_get_contents();
307309
ob_end_clean();
308310
throw new DeepExitException($error, 3);
@@ -317,7 +319,7 @@ public function init()
317319

318320
// Create this class so it is autoloaded and sets up a bunch
319321
// of PHP_CodeSniffer-specific token type constants.
320-
$tokens = new Util\Tokens();
322+
$tokens = new Tokens();
321323

322324
// Allow autoloading of custom files inside installed standards.
323325
$installedStandards = Standards::getInstalledStandardDetails();

0 commit comments

Comments
 (0)