diff --git a/dictionaries/scripts/update_signaturemap_from_other_tool.php b/dictionaries/scripts/update_signaturemap_from_other_tool.php index e2547a58757..21fb61166d5 100644 --- a/dictionaries/scripts/update_signaturemap_from_other_tool.php +++ b/dictionaries/scripts/update_signaturemap_from_other_tool.php @@ -34,7 +34,7 @@ foreach ($new_local as $name => $data) { if (!is_array($data)) { - throw new \UnexpectedValueException('bad data for ' . $name); + throw new UnexpectedValueException('bad data for ' . $name); } $return_type = array_shift($data); echo '\'' . str_replace("'", "\'", $name) . '\' => [\'' . str_replace("'", "\'", $return_type) . '\''; diff --git a/examples/TemplateChecker.php b/examples/TemplateChecker.php index 8e9e0792df8..f048d9b1a49 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 86afa8766a4..f713dfcc8ac 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,15 +62,11 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve // fall through } } - } catch (\Exception $e) { + } catch (Exception $e) { // can throw if storage is missing } } - /** - * @param non-empty-string $function_id - * @param FileManipulation[] $file_replacements - */ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $event): void { $expr = $event->getExpr(); @@ -93,7 +92,7 @@ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $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 +102,12 @@ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent // 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 e19d1742fe4..cbd7d23243e 100644 --- a/examples/plugins/PreventFloatAssignmentChecker.php +++ b/examples/plugins/PreventFloatAssignmentChecker.php @@ -5,6 +5,8 @@ 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 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve && ($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 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve } } -class NoFloatAssignment extends \Psalm\Issue\PluginIssue { +class NoFloatAssignment extends PluginIssue { } diff --git a/examples/plugins/StringChecker.php b/examples/plugins/StringChecker.php index ae7b09aadec..91f0b79c55b 100644 --- a/examples/plugins/StringChecker.php +++ b/examples/plugins/StringChecker.php @@ -6,6 +6,9 @@ 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 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve ) { $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 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve $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/examples/plugins/composer-based/echo-checker/EchoChecker.php b/examples/plugins/composer-based/echo-checker/EchoChecker.php index 2bb8908d281..c75eac76a56 100644 --- a/examples/plugins/composer-based/echo-checker/EchoChecker.php +++ b/examples/plugins/composer-based/echo-checker/EchoChecker.php @@ -17,8 +17,6 @@ class EchoChecker implements AfterStatementAnalysisInterface /** * Called after a statement has been checked * - * @param FileManipulation[] $file_replacements - * * @return null|false */ public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool { diff --git a/psalm b/psalm index 84684e60840..a633cb872ac 100755 --- a/psalm +++ b/psalm @@ -1,4 +1,7 @@ #!/usr/bin/env php [ function ($filePath, $prefix, $contents) { @@ -79,7 +81,7 @@ function ($filePath, $prefix, $contents) { }, ], 'whitelist' => [ - \Composer\Autoload\ClassLoader::class, + ClassLoader::class, 'Psalm\*', ], 'files-whitelist' => [ diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php index 4c6edd38264..84b777c7e20 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentAnalyzer.php @@ -658,7 +658,7 @@ private static function checkFunctionLikeTypeMatches( } /** - * @param TKeyedArray|TArray|TList|TClassStringMap $unpacked_atomic_array + * @param TKeyedArray|TArray|TList|TClassStringMap|null $unpacked_atomic_array * @return null|false * @psalm-suppress ComplexMethod */ diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php index cb1b5fbbf01..33d6d173ac6 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php @@ -60,7 +60,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer { /** - * @param TNamedObject|TTemplateParam $static_type + * @param TNamedObject|TTemplateParam|null $static_type * * @psalm-suppress ComplexMethod it's really complex, but unavoidably so */ diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php index 2dc0c0592d5..bfb78e45750 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/ExistingAtomicMethodCallAnalyzer.php @@ -48,7 +48,7 @@ class ExistingAtomicMethodCallAnalyzer extends CallAnalyzer { /** - * @param TNamedObject|TTemplateParam $static_type + * @param TNamedObject|TTemplateParam|null $static_type * @param list $args */ public static function analyze( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php index 8f09b222b08..b94dc918d63 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodCallReturnTypeFetcher.php @@ -37,7 +37,7 @@ class MethodCallReturnTypeFetcher { /** - * @param TNamedObject|TTemplateParam $static_type + * @param TNamedObject|TTemplateParam|null $static_type * @param list $args */ public static function fetch( diff --git a/src/Psalm/Internal/Cli/LanguageServer.php b/src/Psalm/Internal/Cli/LanguageServer.php index 5d44a9a06b5..e8689ee968c 100644 --- a/src/Psalm/Internal/Cli/LanguageServer.php +++ b/src/Psalm/Internal/Cli/LanguageServer.php @@ -219,6 +219,7 @@ function (string $arg) use ($valid_long_options): void { $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 f87b17bef17..36043f4cf89 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -211,6 +211,7 @@ public static function run(array $argv): void $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/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index b04190e66a6..a29491475cf 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -202,6 +202,7 @@ public static function run(array $argv): void $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 8f0bd1f4223..1c87f4e84a2 100644 --- a/src/Psalm/Internal/Cli/Refactor.php +++ b/src/Psalm/Internal/Cli/Refactor.php @@ -177,6 +177,7 @@ function (string $arg) use ($valid_long_options): void { $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 20a7f4bb8a4..6bc81302b96 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\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 @@ public static function getClassLikeNode( 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] @@ -112,14 +117,14 @@ public static function getClassLikeNode( /** * @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 @@ private static function getConstantNodes(\Psalm\Codebase $codebase, ClassLikeSto } 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 @@ private static function getPropertyNodes(ClassLikeStorage $storage): array 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] @@ -220,14 +225,14 @@ private static function getMethodNodes(ClassLikeStorage $storage): array { 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 @@ private static function getMethodNodes(ClassLikeStorage $storage): array { '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 d17b843d1c3..23b1ca16ef0 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\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 @@ public static function getAll( 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 @@ public static function getAll( } 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 @@ public static function getAll( } 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 @@ private static function getFunctionNode( 'comments' => $docblock->tags ? [ new PhpParser\Comment\Doc( - \rtrim($docblock->render(' ')) + rtrim($docblock->render(' ')) ) ] : [] @@ -338,7 +346,7 @@ private static function getFunctionNode( /** * @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 @@ public static function getParserTypeFromPsalmType(Union $type): ?PhpParser\NodeA $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 @@ public static function getExpressionFromType(Union $type) : PhpParser\Node\Expr 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); diff --git a/src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php b/src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php index b60483522c1..bee00cdb7b1 100644 --- a/src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php +++ b/src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php @@ -486,7 +486,7 @@ private static function getTypeParts( $atomic_types = []; foreach ($union_type->getAtomicTypes() as $atomic_type) { if (!$atomic_type instanceof TTypeAlias) { - array_push($atomic_types, $atomic_type); + $atomic_types[] = $atomic_type; continue; } $expanded = TypeExpander::expandAtomic( @@ -499,7 +499,7 @@ private static function getTypeParts( true ); if ($expanded instanceof Atomic) { - array_push($atomic_types, $expanded); + $atomic_types[] = $expanded; continue; } diff --git a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php index e29f9d68fae..1dc6033b60e 100644 --- a/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php +++ b/src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php @@ -657,7 +657,7 @@ private static function handleTemplateParamStandin( if ($keyed_template instanceof TKeyedArray) { $key_type = $keyed_template->getGenericKeyType(); } elseif ($keyed_template instanceof TList) { - $key_type = \Psalm\Type::getInt(); + $key_type = Type::getInt(); } else { $key_type = $keyed_template->type_params[0]; } diff --git a/src/Psalm/Issue/IfThisIsMismatch.php b/src/Psalm/Issue/IfThisIsMismatch.php index 9437a5b643e..c9bcba5dc45 100644 --- a/src/Psalm/Issue/IfThisIsMismatch.php +++ b/src/Psalm/Issue/IfThisIsMismatch.php @@ -4,6 +4,6 @@ class IfThisIsMismatch extends CodeIssue { - const ERROR_LEVEL = 4; - const SHORTCODE = 300; + public const ERROR_LEVEL = 4; + public const SHORTCODE = 300; }