Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully qualify all global function calls. #718

Merged
merged 2 commits into from
Jan 18, 2024
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
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseSpacing"/>
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses"/>
</ruleset>
2 changes: 2 additions & 0 deletions src/DeprecatedScope/DeprecationHelperScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver;
use function class_exists;
use function count;

final class DeprecationHelperScope implements DeprecatedScopeResolver
{
Expand Down
1 change: 1 addition & 0 deletions src/DeprecatedScope/GroupLegacyScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPStan\Analyser\Scope;
use PHPStan\Rules\Deprecations\DeprecatedScopeResolver;
use function strpos;

final class GroupLegacyScope implements DeprecatedScopeResolver
{
Expand Down
40 changes: 32 additions & 8 deletions src/Drupal/DrupalAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
namespace mglaman\PHPStanDrupal\Drupal;

use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
use Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter;
use DrupalFinder\DrupalFinder;
use Drush\Drush;
use PHPStan\DependencyInjection\Container;
use PHPUnit\Framework\Test;
use ReflectionClass;
use RuntimeException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
use Throwable;
use function array_map;
use function array_merge;
use function array_walk;
use function class_exists;
use function dirname;
use function file_exists;
use function in_array;
use function interface_exists;
use function is_array;
use function is_dir;
use function is_string;
use function realpath;
use function str_replace;
use function strpos;
use function strtr;
use function trigger_error;
use function ucwords;
use function usort;

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

$this->drupalRoot = $drupalRoot;
Expand Down Expand Up @@ -142,11 +166,11 @@ public function register(Container $container): void
}
}

if (class_exists(\Drush\Drush::class)) {
$reflect = new \ReflectionClass(\Drush\Drush::class);
if (class_exists(Drush::class)) {
$reflect = new ReflectionClass(Drush::class);
if ($reflect->getFileName() !== false) {
$levels = 2;
if (\Drush\Drush::getMajorVersion() < 9) {
if (Drush::getMajorVersion() < 9) {
$levels = 3;
}
$drushDir = dirname($reflect->getFileName(), $levels);
Expand Down Expand Up @@ -200,9 +224,9 @@ class: Drupal\jsonapi\Routing\JsonApiParamEnhancer
$service_map = $container->getByType(ServiceMap::class);
$service_map->setDrupalServices($this->serviceMap);

if (interface_exists(\PHPUnit\Framework\Test::class)
if (interface_exists(Test::class)
&& class_exists('Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter')) {
\Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\ClassWriter::mutateTestBase($this->autoloader);
ClassWriter::mutateTestBase($this->autoloader);
}

$extension_map = $container->getByType(ExtensionMap::class);
Expand Down Expand Up @@ -307,7 +331,7 @@ protected function loadExtension(Extension $extension): void
{
try {
$extension->load();
} catch (\Throwable $e) {
} catch (Throwable $e) {
// Something prevented the extension file from loading.
// This can happen when drupal_get_path or drupal_get_filename are used outside of the scope of a function.
}
Expand All @@ -321,7 +345,7 @@ protected function loadAndCatchErrors(string $path): void
$path = str_replace(dirname($this->drupalRoot) . '/', '', $path);
// This can happen when drupal_get_path or drupal_get_filename are used outside the scope of a function.
@trigger_error("$path invoked the Drupal container outside of the scope of a function or class method. It was not loaded.", E_USER_WARNING);
} catch (\Throwable $e) {
} catch (Throwable $e) {
$path = str_replace(dirname($this->drupalRoot) . '/', '', $path);
// Something prevented the extension file from loading.
@trigger_error("$path failed loading due to {$e->getMessage()}", E_USER_WARNING);
Expand Down
2 changes: 2 additions & 0 deletions src/Drupal/DrupalServiceDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function str_replace;

class DrupalServiceDefinition
{
Expand Down
23 changes: 15 additions & 8 deletions src/Drupal/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

namespace mglaman\PHPStanDrupal\Drupal;

use RuntimeException;
use Symfony\Component\Yaml\Yaml;
use function explode;
use function file_get_contents;
use function is_array;
use function sprintf;
use function strpos;
use function trim;

/**
* Defines an extension (file) object.
Expand Down Expand Up @@ -190,7 +197,7 @@ public function load(): bool
*/
public function getDependencies(): array
{
if (\is_array($this->dependencies)) {
if (is_array($this->dependencies)) {
return $this->dependencies;
}

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

// @see \Drupal\Core\Extension\Dependency::createFromString().
foreach ($dependencies as $dependency) {
if (\strpos($dependency, ':') !== false) {
[, $dependency] = \explode(':', $dependency);
if (strpos($dependency, ':') !== false) {
[, $dependency] = explode(':', $dependency);
}

$parts = \explode('(', $dependency, 2);
$this->dependencies[] = \trim($parts[0]);
$parts = explode('(', $dependency, 2);
$this->dependencies[] = trim($parts[0]);
}

return $this->dependencies;
}

private function parseInfo(): array
{
if (\is_array($this->info)) {
if (is_array($this->info)) {
return $this->info;
}

$infoContent = \file_get_contents(\sprintf('%s/%s', $this->root, $this->getPathname()));
$infoContent = file_get_contents(sprintf('%s/%s', $this->root, $this->getPathname()));
if (false === $infoContent) {
throw new \RuntimeException(\sprintf('Cannot read "%s', $this->getPathname()));
throw new RuntimeException(sprintf('Cannot read "%s', $this->getPathname()));
}

return $this->info = Yaml::parse($infoContent);
Expand Down
31 changes: 22 additions & 9 deletions src/Drupal/ExtensionDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

namespace mglaman\PHPStanDrupal\Drupal;

use FilesystemIterator;
use mglaman\PHPStanDrupal\Drupal\Extension;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use function array_filter;
use function array_flip;
use function array_multisort;
use function dirname;
use function file_exists;
use function is_dir;
use function preg_match;
use function strpos;

class ExtensionDiscovery
{

Expand Down Expand Up @@ -207,7 +220,7 @@ protected function filterByProfileDirectories(array $all_files)
return $all_files;
}

return array_filter($all_files, function (\mglaman\PHPStanDrupal\Drupal\Extension $file) : bool {
return array_filter($all_files, function (Extension $file) : bool {
if (strpos($file->subpath, 'profiles') !== 0) {
// This extension doesn't belong to a profile, ignore it.
return true;
Expand Down Expand Up @@ -332,11 +345,11 @@ protected function scanDirectory($dir): array
// symlinks (to allow extensions to be linked from elsewhere), and return
// the RecursiveDirectoryIterator instance to have access to getSubPath(),
// since SplFileInfo does not support relative paths.
$flags = \FilesystemIterator::UNIX_PATHS;
$flags |= \FilesystemIterator::SKIP_DOTS;
$flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
$flags |= \FilesystemIterator::CURRENT_AS_SELF;
$directory_iterator = new \RecursiveDirectoryIterator($absolute_dir, $flags);
$flags = FilesystemIterator::UNIX_PATHS;
$flags |= FilesystemIterator::SKIP_DOTS;
$flags |= FilesystemIterator::FOLLOW_SYMLINKS;
$flags |= FilesystemIterator::CURRENT_AS_SELF;
$directory_iterator = new RecursiveDirectoryIterator($absolute_dir, $flags);

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

// The actual recursive filesystem scan is only invoked by instantiating the
// RecursiveIteratorIterator.
$iterator = new \RecursiveIteratorIterator(
$iterator = new RecursiveIteratorIterator(
$filter,
\RecursiveIteratorIterator::LEAVES_ONLY,
RecursiveIteratorIterator::LEAVES_ONLY,
// Suppress filesystem errors in case a directory cannot be accessed.
\RecursiveIteratorIterator::CATCH_GET_CHILD
RecursiveIteratorIterator::CATCH_GET_CHILD
);

foreach ($iterator as $key => $fileinfo) {
Expand Down
4 changes: 4 additions & 0 deletions src/Drupal/ExtensionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace mglaman\PHPStanDrupal\Drupal;

use function array_combine;
use function array_map;
use function is_array;

final class ExtensionMap
{
/** @var array<string, Extension> */
Expand Down
12 changes: 9 additions & 3 deletions src/Drupal/RecursiveExtensionFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace mglaman\PHPStanDrupal\Drupal;

use RecursiveFilterIterator;
use RecursiveIterator;
use function array_merge;
use function in_array;
use function substr;

/**
* Filters a RecursiveDirectoryIterator to discover extensions.
*
* Locally bundled version of \Drupal\Core\Extension\Discovery\RecursiveExtensionFilterIterator.
*
* @method bool isDir()
*/
class RecursiveExtensionFilterIterator extends \RecursiveFilterIterator
class RecursiveExtensionFilterIterator extends RecursiveFilterIterator
{

/**
Expand Down Expand Up @@ -65,7 +71,7 @@ class RecursiveExtensionFilterIterator extends \RecursiveFilterIterator
* (optional) Add to the blacklist of directories that should be filtered
* out during the iteration.
*/
public function __construct(\RecursiveIterator $iterator, array $blacklist = [])
public function __construct(RecursiveIterator $iterator, array $blacklist = [])
{
parent::__construct($iterator);
$this->blacklist = array_merge($this->blacklist, $blacklist);
Expand All @@ -74,7 +80,7 @@ public function __construct(\RecursiveIterator $iterator, array $blacklist = [])
/**
* {@inheritdoc}
*/
public function getChildren(): \RecursiveFilterIterator
public function getChildren(): RecursiveFilterIterator
{
$filter = parent::getChildren();
if ($filter instanceof self) {
Expand Down
2 changes: 2 additions & 0 deletions src/Drupal/ServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace mglaman\PHPStanDrupal\Drupal;

use function class_exists;

class ServiceMap
{
/** @var DrupalServiceDefinition[] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Type\ObjectType;
use function array_key_exists;

/**
* Allows some common methods on fields.
Expand Down
9 changes: 5 additions & 4 deletions src/Reflection/EntityFieldReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -116,13 +117,13 @@ public function getDocComment(): ?string
return null;
}

public function isDeprecated(): \PHPStan\TrinaryLogic
public function isDeprecated(): TrinaryLogic
{
return \PHPStan\TrinaryLogic::createNo();
return TrinaryLogic::createNo();
}

public function isInternal(): \PHPStan\TrinaryLogic
public function isInternal(): TrinaryLogic
{
return \PHPStan\TrinaryLogic::createNo();
return TrinaryLogic::createNo();
}
}
4 changes: 3 additions & 1 deletion src/Reflection/EntityFieldsViaMagicReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace mglaman\PHPStanDrupal\Reflection;

use LogicException;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ObjectType;
use function array_key_exists;

/**
* Allows field access via magic methods
Expand Down Expand Up @@ -58,7 +60,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
return new FieldItemListPropertyReflection($classReflection, $propertyName);
}

throw new \LogicException($classReflection->getName() . "::$propertyName should be handled earlier.");
throw new LogicException($classReflection->getName() . "::$propertyName should be handled earlier.");
}

public static function classObjectIsSuperOfInterface(string $name, ObjectType $interfaceObject) : TrinaryLogic
Expand Down
10 changes: 6 additions & 4 deletions src/Reflection/FieldItemListPropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use function in_array;

/**
* Allows field access via magic methods
Expand Down Expand Up @@ -111,18 +113,18 @@ public function getDocComment(): ?string
return null;
}

public function isDeprecated(): \PHPStan\TrinaryLogic
public function isDeprecated(): TrinaryLogic
{
return \PHPStan\TrinaryLogic::createNo();
return TrinaryLogic::createNo();
}

public function getDeprecatedDescription(): ?string
{
return null;
}

public function isInternal(): \PHPStan\TrinaryLogic
public function isInternal(): TrinaryLogic
{
return \PHPStan\TrinaryLogic::createNo();
return TrinaryLogic::createNo();
}
}
Loading
Loading