Skip to content

Commit c393f60

Browse files
authored
Fully qualify all global function calls. (#718)
* Fully qualify all global function calls. * Add the slevomat/coding-standards ReferenceUsedNames rule and fix violations.
1 parent 7014f5b commit c393f60

File tree

43 files changed

+212
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+212
-68
lines changed

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
2222
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
2323
<rule ref="SlevomatCodingStandard.Namespaces.UseSpacing"/>
24+
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
2425
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
2526
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses"/>
2627
</ruleset>

src/DeprecatedScope/DeprecationHelperScope.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\MethodReflection;
1010
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver;
11+
use function class_exists;
12+
use function count;
1113

1214
final class DeprecationHelperScope implements DeprecatedScopeResolver
1315
{

src/DeprecatedScope/GroupLegacyScope.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPStan\Analyser\Scope;
88
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver;
9+
use function strpos;
910

1011
final class GroupLegacyScope implements DeprecatedScopeResolver
1112
{

src/Drupal/DrupalAutoloader.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,34 @@
33
namespace mglaman\PHPStanDrupal\Drupal;
44

55
use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
6+
use Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter;
67
use DrupalFinder\DrupalFinder;
8+
use Drush\Drush;
79
use PHPStan\DependencyInjection\Container;
10+
use PHPUnit\Framework\Test;
11+
use ReflectionClass;
12+
use RuntimeException;
813
use Symfony\Component\Finder\Finder;
914
use Symfony\Component\Yaml\Yaml;
15+
use Throwable;
16+
use function array_map;
17+
use function array_merge;
18+
use function array_walk;
19+
use function class_exists;
20+
use function dirname;
21+
use function file_exists;
22+
use function in_array;
23+
use function interface_exists;
24+
use function is_array;
25+
use function is_dir;
26+
use function is_string;
27+
use function realpath;
28+
use function str_replace;
29+
use function strpos;
30+
use function strtr;
31+
use function trigger_error;
32+
use function ucwords;
33+
use function usort;
1034

1135
class DrupalAutoloader
1236
{
@@ -68,7 +92,7 @@ public function register(Container $container): void
6892
$drupalRoot = $finder->getDrupalRoot();
6993
$drupalVendorRoot = $finder->getVendorDir();
7094
if (! (bool) $drupalRoot || ! (bool) $drupalVendorRoot) {
71-
throw new \RuntimeException("Unable to detect Drupal at {$drupalParams['drupal_root']}");
95+
throw new RuntimeException("Unable to detect Drupal at {$drupalParams['drupal_root']}");
7296
}
7397

7498
$this->drupalRoot = $drupalRoot;
@@ -142,11 +166,11 @@ public function register(Container $container): void
142166
}
143167
}
144168

145-
if (class_exists(\Drush\Drush::class)) {
146-
$reflect = new \ReflectionClass(\Drush\Drush::class);
169+
if (class_exists(Drush::class)) {
170+
$reflect = new ReflectionClass(Drush::class);
147171
if ($reflect->getFileName() !== false) {
148172
$levels = 2;
149-
if (\Drush\Drush::getMajorVersion() < 9) {
173+
if (Drush::getMajorVersion() < 9) {
150174
$levels = 3;
151175
}
152176
$drushDir = dirname($reflect->getFileName(), $levels);
@@ -200,9 +224,9 @@ class: Drupal\jsonapi\Routing\JsonApiParamEnhancer
200224
$service_map = $container->getByType(ServiceMap::class);
201225
$service_map->setDrupalServices($this->serviceMap);
202226

203-
if (interface_exists(\PHPUnit\Framework\Test::class)
227+
if (interface_exists(Test::class)
204228
&& class_exists('Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter')) {
205-
\Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter::mutateTestBase($this->autoloader);
229+
ClassWriter::mutateTestBase($this->autoloader);
206230
}
207231

208232
$extension_map = $container->getByType(ExtensionMap::class);
@@ -307,7 +331,7 @@ protected function loadExtension(Extension $extension): void
307331
{
308332
try {
309333
$extension->load();
310-
} catch (\Throwable $e) {
334+
} catch (Throwable $e) {
311335
// Something prevented the extension file from loading.
312336
// This can happen when drupal_get_path or drupal_get_filename are used outside of the scope of a function.
313337
}
@@ -321,7 +345,7 @@ protected function loadAndCatchErrors(string $path): void
321345
$path = str_replace(dirname($this->drupalRoot) . '/', '', $path);
322346
// This can happen when drupal_get_path or drupal_get_filename are used outside the scope of a function.
323347
@trigger_error("$path invoked the Drupal container outside of the scope of a function or class method. It was not loaded.", E_USER_WARNING);
324-
} catch (\Throwable $e) {
348+
} catch (Throwable $e) {
325349
$path = str_replace(dirname($this->drupalRoot) . '/', '', $path);
326350
// Something prevented the extension file from loading.
327351
@trigger_error("$path failed loading due to {$e->getMessage()}", E_USER_WARNING);

src/Drupal/DrupalServiceDefinition.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use PHPStan\Type\StringType;
77
use PHPStan\Type\Type;
88
use PHPStan\Type\TypeCombinator;
9+
use function count;
10+
use function str_replace;
911

1012
class DrupalServiceDefinition
1113
{

src/Drupal/Extension.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use RuntimeException;
56
use Symfony\Component\Yaml\Yaml;
7+
use function explode;
8+
use function file_get_contents;
9+
use function is_array;
10+
use function sprintf;
11+
use function strpos;
12+
use function trim;
613

714
/**
815
* Defines an extension (file) object.
@@ -190,7 +197,7 @@ public function load(): bool
190197
*/
191198
public function getDependencies(): array
192199
{
193-
if (\is_array($this->dependencies)) {
200+
if (is_array($this->dependencies)) {
194201
return $this->dependencies;
195202
}
196203

@@ -205,26 +212,26 @@ public function getDependencies(): array
205212

206213
// @see \Drupal\Core\Extension\Dependency::createFromString().
207214
foreach ($dependencies as $dependency) {
208-
if (\strpos($dependency, ':') !== false) {
209-
[, $dependency] = \explode(':', $dependency);
215+
if (strpos($dependency, ':') !== false) {
216+
[, $dependency] = explode(':', $dependency);
210217
}
211218

212-
$parts = \explode('(', $dependency, 2);
213-
$this->dependencies[] = \trim($parts[0]);
219+
$parts = explode('(', $dependency, 2);
220+
$this->dependencies[] = trim($parts[0]);
214221
}
215222

216223
return $this->dependencies;
217224
}
218225

219226
private function parseInfo(): array
220227
{
221-
if (\is_array($this->info)) {
228+
if (is_array($this->info)) {
222229
return $this->info;
223230
}
224231

225-
$infoContent = \file_get_contents(\sprintf('%s/%s', $this->root, $this->getPathname()));
232+
$infoContent = file_get_contents(sprintf('%s/%s', $this->root, $this->getPathname()));
226233
if (false === $infoContent) {
227-
throw new \RuntimeException(\sprintf('Cannot read "%s', $this->getPathname()));
234+
throw new RuntimeException(sprintf('Cannot read "%s', $this->getPathname()));
228235
}
229236

230237
return $this->info = Yaml::parse($infoContent);

src/Drupal/ExtensionDiscovery.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use FilesystemIterator;
6+
use mglaman\PHPStanDrupal\Drupal\Extension;
7+
use RecursiveDirectoryIterator;
8+
use RecursiveIteratorIterator;
9+
use function array_filter;
10+
use function array_flip;
11+
use function array_multisort;
12+
use function dirname;
13+
use function file_exists;
14+
use function is_dir;
15+
use function preg_match;
16+
use function strpos;
17+
518
class ExtensionDiscovery
619
{
720

@@ -207,7 +220,7 @@ protected function filterByProfileDirectories(array $all_files)
207220
return $all_files;
208221
}
209222

210-
return array_filter($all_files, function (\mglaman\PHPStanDrupal\Drupal\Extension $file) : bool {
223+
return array_filter($all_files, function (Extension $file) : bool {
211224
if (strpos($file->subpath, 'profiles') !== 0) {
212225
// This extension doesn't belong to a profile, ignore it.
213226
return true;
@@ -332,11 +345,11 @@ protected function scanDirectory($dir): array
332345
// symlinks (to allow extensions to be linked from elsewhere), and return
333346
// the RecursiveDirectoryIterator instance to have access to getSubPath(),
334347
// since SplFileInfo does not support relative paths.
335-
$flags = \FilesystemIterator::UNIX_PATHS;
336-
$flags |= \FilesystemIterator::SKIP_DOTS;
337-
$flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
338-
$flags |= \FilesystemIterator::CURRENT_AS_SELF;
339-
$directory_iterator = new \RecursiveDirectoryIterator($absolute_dir, $flags);
348+
$flags = FilesystemIterator::UNIX_PATHS;
349+
$flags |= FilesystemIterator::SKIP_DOTS;
350+
$flags |= FilesystemIterator::FOLLOW_SYMLINKS;
351+
$flags |= FilesystemIterator::CURRENT_AS_SELF;
352+
$directory_iterator = new RecursiveDirectoryIterator($absolute_dir, $flags);
340353

341354
// Allow directories specified in settings.php to be ignored. You can use
342355
// this to not check for files in common special-purpose directories. For
@@ -352,11 +365,11 @@ protected function scanDirectory($dir): array
352365

353366
// The actual recursive filesystem scan is only invoked by instantiating the
354367
// RecursiveIteratorIterator.
355-
$iterator = new \RecursiveIteratorIterator(
368+
$iterator = new RecursiveIteratorIterator(
356369
$filter,
357-
\RecursiveIteratorIterator::LEAVES_ONLY,
370+
RecursiveIteratorIterator::LEAVES_ONLY,
358371
// Suppress filesystem errors in case a directory cannot be accessed.
359-
\RecursiveIteratorIterator::CATCH_GET_CHILD
372+
RecursiveIteratorIterator::CATCH_GET_CHILD
360373
);
361374

362375
foreach ($iterator as $key => $fileinfo) {

src/Drupal/ExtensionMap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use function array_combine;
6+
use function array_map;
7+
use function is_array;
8+
59
final class ExtensionMap
610
{
711
/** @var array<string, Extension> */

src/Drupal/RecursiveExtensionFilterIterator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use RecursiveFilterIterator;
6+
use RecursiveIterator;
7+
use function array_merge;
8+
use function in_array;
9+
use function substr;
10+
511
/**
612
* Filters a RecursiveDirectoryIterator to discover extensions.
713
*
814
* Locally bundled version of \Drupal\Core\Extension\Discovery\RecursiveExtensionFilterIterator.
915
*
1016
* @method bool isDir()
1117
*/
12-
class RecursiveExtensionFilterIterator extends \RecursiveFilterIterator
18+
class RecursiveExtensionFilterIterator extends RecursiveFilterIterator
1319
{
1420

1521
/**
@@ -65,7 +71,7 @@ class RecursiveExtensionFilterIterator extends \RecursiveFilterIterator
6571
* (optional) Add to the blacklist of directories that should be filtered
6672
* out during the iteration.
6773
*/
68-
public function __construct(\RecursiveIterator $iterator, array $blacklist = [])
74+
public function __construct(RecursiveIterator $iterator, array $blacklist = [])
6975
{
7076
parent::__construct($iterator);
7177
$this->blacklist = array_merge($this->blacklist, $blacklist);
@@ -74,7 +80,7 @@ public function __construct(\RecursiveIterator $iterator, array $blacklist = [])
7480
/**
7581
* {@inheritdoc}
7682
*/
77-
public function getChildren(): \RecursiveFilterIterator
83+
public function getChildren(): RecursiveFilterIterator
7884
{
7985
$filter = parent::getChildren();
8086
if ($filter instanceof self) {

src/Drupal/ServiceMap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace mglaman\PHPStanDrupal\Drupal;
44

5+
use function class_exists;
6+
57
class ServiceMap
68
{
79
/** @var DrupalServiceDefinition[] */

0 commit comments

Comments
 (0)