Skip to content

Commit 8b83793

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 8974f26 commit 8b83793

File tree

17 files changed

+180
-172
lines changed

17 files changed

+180
-172
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];
@@ -651,7 +652,7 @@ public function processShortArgument($arg, $pos)
651652
throw new DeepExitException($output, 0);
652653
case 'i' :
653654
ob_start();
654-
Util\Standards::printInstalledStandards();
655+
Standards::printInstalledStandards();
655656
$output = ob_get_contents();
656657
ob_end_clean();
657658
throw new DeepExitException($output, 0);
@@ -924,7 +925,7 @@ public function processLongArgument($arg, $pos)
924925
$this->cache = true;
925926
self::$overriddenDefaults['cache'] = true;
926927

927-
$this->cacheFile = Util\Common::realpath(substr($arg, 6));
928+
$this->cacheFile = Common::realpath(substr($arg, 6));
928929

929930
// It may not exist and return false instead.
930931
if ($this->cacheFile === false) {
@@ -943,9 +944,9 @@ public function processLongArgument($arg, $pos)
943944
} else {
944945
if ($dir[0] === '/') {
945946
// An absolute path.
946-
$dir = Util\Common::realpath($dir);
947+
$dir = Common::realpath($dir);
947948
} else {
948-
$dir = Util\Common::realpath(getcwd().'/'.$dir);
949+
$dir = Common::realpath(getcwd().'/'.$dir);
949950
}
950951

951952
if ($dir !== false) {
@@ -966,7 +967,7 @@ public function processLongArgument($arg, $pos)
966967
$files = explode(',', substr($arg, 10));
967968
$bootstrap = [];
968969
foreach ($files as $file) {
969-
$path = Util\Common::realpath($file);
970+
$path = Common::realpath($file);
970971
if ($path === false) {
971972
$error = 'ERROR: The specified bootstrap file "'.$file.'" does not exist'.PHP_EOL.PHP_EOL;
972973
$error .= $this->printShortUsage(true);
@@ -980,7 +981,7 @@ public function processLongArgument($arg, $pos)
980981
self::$overriddenDefaults['bootstrap'] = true;
981982
} else if (substr($arg, 0, 10) === 'file-list=') {
982983
$fileList = substr($arg, 10);
983-
$path = Util\Common::realpath($fileList);
984+
$path = Common::realpath($fileList);
984985
if ($path === false) {
985986
$error = 'ERROR: The specified file list "'.$fileList.'" does not exist'.PHP_EOL.PHP_EOL;
986987
$error .= $this->printShortUsage(true);
@@ -1003,7 +1004,7 @@ public function processLongArgument($arg, $pos)
10031004
break;
10041005
}
10051006

1006-
$this->stdinPath = Util\Common::realpath(substr($arg, 11));
1007+
$this->stdinPath = Common::realpath(substr($arg, 11));
10071008

10081009
// It may not exist and return false instead, so use whatever they gave us.
10091010
if ($this->stdinPath === false) {
@@ -1016,13 +1017,13 @@ public function processLongArgument($arg, $pos)
10161017
break;
10171018
}
10181019

1019-
$this->reportFile = Util\Common::realpath(substr($arg, 12));
1020+
$this->reportFile = Common::realpath(substr($arg, 12));
10201021

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

1025-
$dir = Util\Common::realpath(dirname($this->reportFile));
1026+
$dir = Common::realpath(dirname($this->reportFile));
10261027
if (is_dir($dir) === false) {
10271028
$error = 'ERROR: The specified report file path "'.$this->reportFile.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
10281029
$error .= $this->printShortUsage(true);
@@ -1058,7 +1059,7 @@ public function processLongArgument($arg, $pos)
10581059
break;
10591060
}
10601061

1061-
$this->basepath = Util\Common::realpath(substr($arg, 9));
1062+
$this->basepath = Common::realpath(substr($arg, 9));
10621063

10631064
// It may not exist and return false instead.
10641065
if ($this->basepath === false) {
@@ -1085,7 +1086,7 @@ public function processLongArgument($arg, $pos)
10851086
if ($output === false) {
10861087
$output = null;
10871088
} else {
1088-
$dir = Util\Common::realpath(dirname($output));
1089+
$dir = Common::realpath(dirname($output));
10891090
if (is_dir($dir) === false) {
10901091
$error = 'ERROR: The specified '.$report.' report file path "'.$output.'" points to a non-existent directory'.PHP_EOL.PHP_EOL;
10911092
$error .= $this->printShortUsage(true);
@@ -1319,7 +1320,7 @@ public function processFilePath($path)
13191320
return;
13201321
}
13211322

1322-
$file = Util\Common::realpath($path);
1323+
$file = Common::realpath($path);
13231324
if (file_exists($file) === false) {
13241325
if ($this->dieOnUnknownArg === false) {
13251326
return;
@@ -1655,7 +1656,7 @@ public static function setConfigData($key, $value, $temp=false)
16551656
// If the installed paths are being set, make sure all known
16561657
// standards paths are added to the autoloader.
16571658
if ($key === 'installed_paths') {
1658-
$installedStandards = Util\Standards::getInstalledStandardDetails();
1659+
$installedStandards = Standards::getInstalledStandardDetails();
16591660
foreach ($installedStandards as $name => $details) {
16601661
Autoload::addSearchPath($details['path'], $details['namespace']);
16611662
}

src/Files/File.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use PHP_CodeSniffer\Exceptions\TokenizerException;
1515
use PHP_CodeSniffer\Fixer;
1616
use PHP_CodeSniffer\Ruleset;
17-
use PHP_CodeSniffer\Util;
17+
use PHP_CodeSniffer\Util\Common;
18+
use PHP_CodeSniffer\Util\Tokens;
1819

1920
class File
2021
{
@@ -276,7 +277,7 @@ public function setContent($content)
276277
$this->tokens = [];
277278

278279
try {
279-
$this->eolChar = Util\Common::detectLineEndings($content);
280+
$this->eolChar = Common::detectLineEndings($content);
280281
} catch (RuntimeException $e) {
281282
$this->addWarningOnLine($e->getMessage(), 1, 'Internal.DetectLineEndings');
282283
return;
@@ -430,7 +431,7 @@ public function process()
430431

431432
if (PHP_CODESNIFFER_VERBOSITY > 2) {
432433
$type = $token['type'];
433-
$content = Util\Common::prepareForOutput($token['content']);
434+
$content = Common::prepareForOutput($token['content']);
434435
echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL;
435436
}
436437

@@ -872,7 +873,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
872873
$parts = explode('.', $code);
873874
if ($parts[0] === 'Internal') {
874875
// An internal message.
875-
$listenerCode = Util\Common::getSniffCode($this->activeListener);
876+
$listenerCode = Common::getSniffCode($this->activeListener);
876877
$sniffCode = $code;
877878
$checkCodes = [$sniffCode];
878879
} else {
@@ -881,7 +882,7 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
881882
$sniffCode = $code;
882883
$listenerCode = substr($sniffCode, 0, strrpos($sniffCode, '.'));
883884
} else {
884-
$listenerCode = Util\Common::getSniffCode($this->activeListener);
885+
$listenerCode = Common::getSniffCode($this->activeListener);
885886
$sniffCode = $listenerCode.'.'.$code;
886887
$parts = explode('.', $sniffCode);
887888
}
@@ -1615,7 +1616,7 @@ public function getMethodParameters($stackPtr)
16151616
$paramCount++;
16161617
break;
16171618
case T_EQUAL:
1618-
$defaultStart = $this->findNext(Util\Tokens::$emptyTokens, ($i + 1), null, true);
1619+
$defaultStart = $this->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
16191620
$equalToken = $i;
16201621
break;
16211622
}//end switch
@@ -1885,7 +1886,7 @@ public function getMemberProperties($stackPtr)
18851886
T_READONLY => T_READONLY,
18861887
];
18871888

1888-
$valid += Util\Tokens::$emptyTokens;
1889+
$valid += Tokens::$emptyTokens;
18891890

18901891
$scope = 'public';
18911892
$scopeSpecified = false;
@@ -2073,7 +2074,7 @@ public function isReference($stackPtr)
20732074
}
20742075

20752076
$tokenBefore = $this->findPrevious(
2076-
Util\Tokens::$emptyTokens,
2077+
Tokens::$emptyTokens,
20772078
($stackPtr - 1),
20782079
null,
20792080
true
@@ -2097,14 +2098,14 @@ public function isReference($stackPtr)
20972098
return true;
20982099
}
20992100

2100-
if (isset(Util\Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) {
2101+
if (isset(Tokens::$assignmentTokens[$this->tokens[$tokenBefore]['code']]) === true) {
21012102
// This is directly after an assignment. It's a reference. Even if
21022103
// it is part of an operation, the other tests will handle it.
21032104
return true;
21042105
}
21052106

21062107
$tokenAfter = $this->findNext(
2107-
Util\Tokens::$emptyTokens,
2108+
Tokens::$emptyTokens,
21082109
($stackPtr + 1),
21092110
null,
21102111
true
@@ -2155,7 +2156,7 @@ public function isReference($stackPtr)
21552156
if ($this->tokens[$tokenAfter]['code'] === T_VARIABLE) {
21562157
return true;
21572158
} else {
2158-
$skip = Util\Tokens::$emptyTokens;
2159+
$skip = Tokens::$emptyTokens;
21592160
$skip[] = T_NS_SEPARATOR;
21602161
$skip[] = T_SELF;
21612162
$skip[] = T_PARENT;
@@ -2382,7 +2383,7 @@ public function findNext(
23822383
*/
23832384
public function findStartOfStatement($start, $ignore=null)
23842385
{
2385-
$startTokens = Util\Tokens::$blockOpeners;
2386+
$startTokens = Tokens::$blockOpeners;
23862387
$startTokens[T_OPEN_SHORT_ARRAY] = true;
23872388
$startTokens[T_OPEN_TAG] = true;
23882389
$startTokens[T_OPEN_TAG_WITH_ECHO] = true;
@@ -2436,7 +2437,7 @@ public function findStartOfStatement($start, $ignore=null)
24362437

24372438
if ($prevMatch <= $this->tokens[$matchExpression]['scope_opener']) {
24382439
// We're before the arrow in the first case.
2439-
$next = $this->findNext(Util\Tokens::$emptyTokens, ($this->tokens[$matchExpression]['scope_opener'] + 1), null, true);
2440+
$next = $this->findNext(Tokens::$emptyTokens, ($this->tokens[$matchExpression]['scope_opener'] + 1), null, true);
24402441
if ($next === false) {
24412442
return $start;
24422443
}
@@ -2449,12 +2450,12 @@ public function findStartOfStatement($start, $ignore=null)
24492450
$prevMatchArrow = $this->findPrevious(T_MATCH_ARROW, ($prevMatch - 1), $this->tokens[$matchExpression]['scope_opener']);
24502451
if ($prevMatchArrow === false) {
24512452
// We're before the arrow in the first case.
2452-
$next = $this->findNext(Util\Tokens::$emptyTokens, ($this->tokens[$matchExpression]['scope_opener'] + 1), null, true);
2453+
$next = $this->findNext(Tokens::$emptyTokens, ($this->tokens[$matchExpression]['scope_opener'] + 1), null, true);
24532454
return $next;
24542455
}
24552456

24562457
$end = $this->findEndOfStatement($prevMatchArrow);
2457-
$next = $this->findNext(Util\Tokens::$emptyTokens, ($end + 1), null, true);
2458+
$next = $this->findNext(Tokens::$emptyTokens, ($end + 1), null, true);
24582459
return $next;
24592460
}
24602461
}//end if
@@ -2515,7 +2516,7 @@ public function findStartOfStatement($start, $ignore=null)
25152516
}
25162517
}//end if
25172518

2518-
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
2519+
if (isset(Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
25192520
$lastNotEmpty = $i;
25202521
}
25212522
}//end for
@@ -2610,7 +2611,7 @@ public function findEndOfStatement($start, $ignore=null)
26102611
continue;
26112612
}
26122613

2613-
if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
2614+
if ($i === $start && isset(Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
26142615
return $this->tokens[$i]['scope_closer'];
26152616
}
26162617

@@ -2630,7 +2631,7 @@ public function findEndOfStatement($start, $ignore=null)
26302631
}
26312632
}//end if
26322633

2633-
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
2634+
if (isset(Tokens::$emptyTokens[$this->tokens[$i]['code']]) === false) {
26342635
$lastNotEmpty = $i;
26352636
}
26362637
}//end for

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: 2 additions & 2 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;
@@ -97,7 +97,7 @@ public function __construct($iterator, $basedir, Config $config, Ruleset $rulese
9797
public function accept()
9898
{
9999
$filePath = $this->current();
100-
$realPath = Util\Common::realpath($filePath);
100+
$realPath = Common::realpath($filePath);
101101

102102
if ($realPath !== false) {
103103
// 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;

0 commit comments

Comments
 (0)