From 65fffd21160ecd286a7117e92848f78086d8768a Mon Sep 17 00:00:00 2001 From: orklah Date: Mon, 27 Dec 2021 20:18:33 +0100 Subject: [PATCH] replace fully qualified names by imports --- examples/TemplateChecker.php | 6 ++-- examples/TemplateScanner.php | 3 +- examples/plugins/ClassUnqualifier.php | 6 ++-- examples/plugins/FunctionCasingChecker.php | 13 +++++--- .../plugins/PreventFloatAssignmentChecker.php | 6 ++-- examples/plugins/StringChecker.php | 11 ++++--- psalm | 5 ++- psalm-language-server | 5 ++- psalm-plugin | 5 ++- psalm-refactor | 5 ++- psalter | 5 ++- scoper.inc.php | 4 ++- src/Psalm/Internal/Cli/LanguageServer.php | 2 ++ src/Psalm/Internal/Cli/Psalm.php | 3 +- src/Psalm/Internal/Cli/Psalter.php | 2 ++ src/Psalm/Internal/Cli/Refactor.php | 2 ++ .../Generator/ClassLikeStubGenerator.php | 25 +++++++++------ .../Stubs/Generator/StubsGenerator.php | 32 ++++++++++++------- 18 files changed, 95 insertions(+), 45 deletions(-) diff --git a/examples/TemplateChecker.php b/examples/TemplateChecker.php index 8e9e0792d..f048d9b1a 100644 --- a/examples/TemplateChecker.php +++ b/examples/TemplateChecker.php @@ -1,6 +1,7 @@ analyze($pseudo_method_stmts, $context); diff --git a/examples/TemplateScanner.php b/examples/TemplateScanner.php index 86afa8766..f713dfcc8 100644 --- a/examples/TemplateScanner.php +++ b/examples/TemplateScanner.php @@ -1,6 +1,7 @@ cased_name !== (string)$expr->name) { - if (\Psalm\IssueBuffer::accepts( + if (IssueBuffer::accepts( new IncorrectFunctionCasing( 'Function is incorrectly cased, expecting ' . $function_storage->cased_name, new CodeLocation($statements_source, $expr->name) @@ -59,7 +62,7 @@ class FunctionCasingChecker implements AfterFunctionCallAnalysisInterface, After // fall through } } - } catch (\Exception $e) { + } catch (Exception $e) { // can throw if storage is missing } } @@ -93,7 +96,7 @@ class FunctionCasingChecker implements AfterFunctionCallAnalysisInterface, After $function_name_parts = explode('\\', $function_storage->cased_name); if (end($function_name_parts) !== end($expr->name->parts)) { - if (\Psalm\IssueBuffer::accepts( + if (IssueBuffer::accepts( new IncorrectFunctionCasing( 'Function is incorrectly cased, expecting ' . $function_storage->cased_name, new CodeLocation($statements_source, $expr->name) @@ -103,12 +106,12 @@ class FunctionCasingChecker implements AfterFunctionCallAnalysisInterface, After // fall through } } - } catch (\Exception $e) { + } catch (Exception $e) { // can throw if storage is missing } } } -class IncorrectFunctionCasing extends \Psalm\Issue\PluginIssue +class IncorrectFunctionCasing extends PluginIssue { } diff --git a/examples/plugins/PreventFloatAssignmentChecker.php b/examples/plugins/PreventFloatAssignmentChecker.php index e19d1742f..cbd7d2324 100644 --- a/examples/plugins/PreventFloatAssignmentChecker.php +++ b/examples/plugins/PreventFloatAssignmentChecker.php @@ -5,6 +5,8 @@ use PhpParser; use Psalm\Checker; use Psalm\CodeLocation; use Psalm\FileManipulation; +use Psalm\Issue\PluginIssue; +use Psalm\IssueBuffer; use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface; use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent; @@ -25,7 +27,7 @@ class PreventFloatAssignmentChecker implements AfterExpressionAnalysisInterface && ($expr_type = $statements_source->getNodeTypeProvider()->getType($expr->expr)) && $expr_type->hasFloat() ) { - if (\Psalm\IssueBuffer::accepts( + if (IssueBuffer::accepts( new NoFloatAssignment( 'Don’t assign to floats', new CodeLocation($statements_source, $expr) @@ -40,5 +42,5 @@ class PreventFloatAssignmentChecker implements AfterExpressionAnalysisInterface } } -class NoFloatAssignment extends \Psalm\Issue\PluginIssue { +class NoFloatAssignment extends PluginIssue { } diff --git a/examples/plugins/StringChecker.php b/examples/plugins/StringChecker.php index ae7b09aad..91f0b79c5 100644 --- a/examples/plugins/StringChecker.php +++ b/examples/plugins/StringChecker.php @@ -6,6 +6,9 @@ use Psalm\Checker; use Psalm\Checker\StatementsChecker; use Psalm\CodeLocation; use Psalm\FileManipulation; +use Psalm\Issue\InvalidClass; +use Psalm\Issue\UndefinedMethod; +use Psalm\IssueBuffer; use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface; use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent; @@ -29,8 +32,8 @@ class StringChecker implements AfterExpressionAnalysisInterface ) { $absolute_class = preg_split('/[:]/', $expr->value)[0]; - if (\Psalm\IssueBuffer::accepts( - new \Psalm\Issue\InvalidClass( + if (IssueBuffer::accepts( + new InvalidClass( 'Use ::class constants when representing class names', new CodeLocation($statements_source, $expr), $absolute_class @@ -54,8 +57,8 @@ class StringChecker implements AfterExpressionAnalysisInterface $appearing_method_id = $codebase->getAppearingMethodId($method_id); if (!$appearing_method_id) { - if (\Psalm\IssueBuffer::accepts( - new \Psalm\Issue\UndefinedMethod( + if (IssueBuffer::accepts( + new UndefinedMethod( 'Method ' . $method_id . ' does not exist', new CodeLocation($statements_source, $expr), $method_id diff --git a/psalm b/psalm index 84684e608..a633cb872 100755 --- a/psalm +++ b/psalm @@ -1,4 +1,7 @@ #!/usr/bin/env php [ function ($filePath, $prefix, $contents) { @@ -79,7 +81,7 @@ return [ }, ], 'whitelist' => [ - \Composer\Autoload\ClassLoader::class, + ClassLoader::class, 'Psalm\*', ], 'files-whitelist' => [ diff --git a/src/Psalm/Internal/Cli/LanguageServer.php b/src/Psalm/Internal/Cli/LanguageServer.php index 5d44a9a06..9f8bd7f40 100644 --- a/src/Psalm/Internal/Cli/LanguageServer.php +++ b/src/Psalm/Internal/Cli/LanguageServer.php @@ -2,6 +2,7 @@ namespace Psalm\Internal\Cli; +use Composer\Autoload\ClassLoader; use Psalm\Config; use Psalm\Internal\Analyzer\ProjectAnalyzer; use Psalm\Internal\CliUtils; @@ -219,6 +220,7 @@ HELP; $include_collector = new IncludeCollector(); $first_autoloader = $include_collector->runAndCollect( + // we ignore the FQN because of a hack in scoper.inc that needs full path // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader { return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir); diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 8c3e4f027..36043f4cf 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -211,8 +211,9 @@ final class Psalm $include_collector = new IncludeCollector(); $first_autoloader = $include_collector->runAndCollect( + // we ignore the FQN because of a hack in scoper.inc that needs full path // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName - function () use ($current_dir, $options, $vendor_dir): ?ClassLoader { + function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader { return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir); } ); diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index b04190e66..fad34b891 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -2,6 +2,7 @@ namespace Psalm\Internal\Cli; +use Composer\Autoload\ClassLoader; use Composer\XdebugHandler\XdebugHandler; use Psalm\Config; use Psalm\Exception\UnsupportedIssueToFixException; @@ -202,6 +203,7 @@ HELP; $include_collector = new IncludeCollector(); $first_autoloader = $include_collector->runAndCollect( + // we ignore the FQN because of a hack in scoper.inc that needs full path // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader { return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir); diff --git a/src/Psalm/Internal/Cli/Refactor.php b/src/Psalm/Internal/Cli/Refactor.php index 8f0bd1f42..5f990ebc3 100644 --- a/src/Psalm/Internal/Cli/Refactor.php +++ b/src/Psalm/Internal/Cli/Refactor.php @@ -2,6 +2,7 @@ namespace Psalm\Internal\Cli; +use Composer\Autoload\ClassLoader; use Composer\XdebugHandler\XdebugHandler; use Psalm\Internal\Analyzer\ProjectAnalyzer; use Psalm\Internal\CliUtils; @@ -177,6 +178,7 @@ HELP; $include_collector = new IncludeCollector(); $first_autoloader = $include_collector->runAndCollect( + // we ignore the FQN because of a hack in scoper.inc that needs full path // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader { return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir); diff --git a/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php b/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php index 20a7f4bb8..6bc81302b 100644 --- a/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php +++ b/src/Psalm/Internal/Stubs/Generator/ClassLikeStubGenerator.php @@ -3,6 +3,8 @@ namespace Psalm\Internal\Stubs\Generator; use PhpParser; +use Psalm\Codebase; +use Psalm\Internal\Codebase\ConstantTypeResolver; use Psalm\Node\Name\VirtualFullyQualified; use Psalm\Node\Stmt\VirtualClass; use Psalm\Node\Stmt\VirtualClassConst; @@ -18,7 +20,10 @@ use Psalm\Internal\Scanner\ParsedDocblock; use Psalm\Type; use Psalm\Type\Union; +use ReflectionProperty; +use UnexpectedValueException; use function array_slice; +use function rtrim; class ClassLikeStubGenerator { @@ -26,7 +31,7 @@ class ClassLikeStubGenerator * @return PhpParser\Node\Stmt\Class_|PhpParser\Node\Stmt\Interface_|PhpParser\Node\Stmt\Trait_ */ public static function getClassLikeNode( - \Psalm\Codebase $codebase, + Codebase $codebase, ClassLikeStorage $storage, string $classlike_name ) : PhpParser\Node\Stmt\ClassLike { @@ -61,7 +66,7 @@ class ClassLikeStubGenerator 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] @@ -112,14 +117,14 @@ class ClassLikeStubGenerator /** * @return list */ - private static function getConstantNodes(\Psalm\Codebase $codebase, ClassLikeStorage $storage): array + private static function getConstantNodes(Codebase $codebase, ClassLikeStorage $storage): array { $constant_nodes = []; foreach ($storage->constants as $constant_name => $constant_storage) { if ($constant_storage->unresolved_node) { $type = new Union([ - \Psalm\Internal\Codebase\ConstantTypeResolver::resolve( + ConstantTypeResolver::resolve( $codebase->classlikes, $constant_storage->unresolved_node ) @@ -127,7 +132,7 @@ class ClassLikeStubGenerator } elseif ($constant_storage->type) { $type = $constant_storage->type; } else { - throw new \UnexpectedValueException('bad'); + throw new UnexpectedValueException('bad'); } $constant_nodes[] = new VirtualClassConst( @@ -197,7 +202,7 @@ class ClassLikeStubGenerator 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] @@ -220,14 +225,14 @@ class ClassLikeStubGenerator foreach ($storage->methods as $method_storage) { if (!$method_storage->cased_name) { - throw new \UnexpectedValueException('very bad'); + throw new UnexpectedValueException('very bad'); } switch ($method_storage->visibility) { - case \ReflectionProperty::IS_PRIVATE: + case ReflectionProperty::IS_PRIVATE: $flag = PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE; break; - case \ReflectionProperty::IS_PROTECTED: + case ReflectionProperty::IS_PROTECTED: $flag = PhpParser\Node\Stmt\Class_::MODIFIER_PROTECTED; break; default: @@ -296,7 +301,7 @@ class ClassLikeStubGenerator 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] diff --git a/src/Psalm/Internal/Stubs/Generator/StubsGenerator.php b/src/Psalm/Internal/Stubs/Generator/StubsGenerator.php index d17b843d1..23b1ca16e 100644 --- a/src/Psalm/Internal/Stubs/Generator/StubsGenerator.php +++ b/src/Psalm/Internal/Stubs/Generator/StubsGenerator.php @@ -2,6 +2,10 @@ namespace Psalm\Internal\Stubs\Generator; +use Psalm\Codebase; +use Psalm\Internal\Provider\ClassLikeStorageProvider; +use Psalm\Internal\Provider\FileStorageProvider; +use Psalm\Storage\FunctionLikeStorage; use Psalm\Type\Atomic\TAnonymousClassInstance; use Psalm\Type\Atomic\TArray; use Psalm\Type\Atomic\TArrayKey; @@ -98,21 +102,25 @@ use Psalm\Node\VirtualParam; use Psalm\Type; use Psalm\Type\Union; +use UnexpectedValueException; use function dirname; +use function is_int; +use function rtrim; +use function strpos; class StubsGenerator { public static function getAll( - \Psalm\Codebase $codebase, - \Psalm\Internal\Provider\ClassLikeStorageProvider $class_provider, - \Psalm\Internal\Provider\FileStorageProvider $file_provider + Codebase $codebase, + ClassLikeStorageProvider $class_provider, + FileStorageProvider $file_provider ): string { $namespaced_nodes = []; $psalm_base = dirname(__DIR__, 5); foreach ($class_provider->getAll() as $storage) { - if (\strpos($storage->name, 'Psalm\\') === 0) { + if (strpos($storage->name, 'Psalm\\') === 0) { continue; } @@ -146,13 +154,13 @@ class StubsGenerator foreach ($codebase->functions->getAllStubbedFunctions() as $function_storage) { if ($function_storage->location - && \strpos($function_storage->location->file_path, $psalm_base) === 0 + && strpos($function_storage->location->file_path, $psalm_base) === 0 ) { continue; } if (!$function_storage->cased_name) { - throw new \UnexpectedValueException('very bad'); + throw new UnexpectedValueException('very bad'); } $fq_name = $function_storage->cased_name; @@ -192,7 +200,7 @@ class StubsGenerator } foreach ($file_provider->getAll() as $file_storage) { - if (\strpos($file_storage->file_path, $psalm_base) === 0) { + if (strpos($file_storage->file_path, $psalm_base) === 0) { continue; } @@ -265,7 +273,7 @@ class StubsGenerator } private static function getFunctionNode( - \Psalm\Storage\FunctionLikeStorage $function_storage, + FunctionLikeStorage $function_storage, string $function_name, string $namespace_name ) : PhpParser\Node\Stmt\Function_ { @@ -327,7 +335,7 @@ class StubsGenerator 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] @@ -338,7 +346,7 @@ class StubsGenerator /** * @return list */ - public static function getFunctionParamNodes(\Psalm\Storage\FunctionLikeStorage $method_storage): array + public static function getFunctionParamNodes(FunctionLikeStorage $method_storage): array { $param_nodes = []; @@ -379,7 +387,7 @@ class StubsGenerator $identifier_string = $atomic_type->toPhpString(null, [], null, 8, 0); if ($identifier_string === null) { - throw new \UnexpectedValueException( + throw new UnexpectedValueException( $atomic_type->getId() . ' could not be converted to an identifier' ); } @@ -447,7 +455,7 @@ class StubsGenerator foreach ($atomic_type->properties as $property_name => $property_type) { if ($atomic_type->is_list) { $key_type = null; - } elseif (\is_int($property_name)) { + } elseif (is_int($property_name)) { $key_type = new VirtualLNumber($property_name); } else { $key_type = new VirtualString($property_name);