From 0ba346c5a0addf22082c360242c23f5755ad60f4 Mon Sep 17 00:00:00 2001 From: Ivan Sidorov Date: Wed, 31 Jan 2024 16:59:28 +0000 Subject: [PATCH] Delete code of replacing variable method_id The main task for deleting: ``` $method_id = new MethodIdentifier( $fq_class_name, '__callstatic', ); ``` List of resolved problems: ``` 1) Psalm\Tests\MagicMethodAnnotationTest::testNoSealAllMethodsWithStatic Failed asserting that exception of type "Psalm\Exception\CodeException" is thrown. 2) Psalm\Tests\MagicMethodAnnotationTest::testSealAllMethodsWithoutFooWithStatic Failed asserting that exception of type "Psalm\Exception\CodeException" is thrown. 3) Psalm\Tests\MagicMethodAnnotationTest::testInvalidCode with data set "inheritSealedMethodsWithStatic" Failed asserting that exception of type "Psalm\Exception\CodeException" is thrown. ``` Appended problem by that fix: ``` 1) MagicMethodAnnotationTest::testSealAllMethodsSetToFalseWithStatic Psalm\Exception\CodeException: UndefinedMagicMethod - somefile.php:8:15 - Magic method B::foo does not exist ``` --- .../Internal/Analyzer/MethodAnalyzer.php | 5 ++- .../Call/Method/MethodVisibilityAnalyzer.php | 6 ++- .../StaticMethod/AtomicStaticCallAnalyzer.php | 38 ++----------------- 3 files changed, 10 insertions(+), 39 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/MethodAnalyzer.php b/src/Psalm/Internal/Analyzer/MethodAnalyzer.php index 05773590b..94e81f119 100644 --- a/src/Psalm/Internal/Analyzer/MethodAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/MethodAnalyzer.php @@ -111,8 +111,9 @@ final class MethodAnalyzer extends FunctionLikeAnalyzer } $original_method_id = $method_id; + $with_pseudo = true; - $method_id = $codebase_methods->getDeclaringMethodId($method_id); + $method_id = $codebase_methods->getDeclaringMethodId($method_id, $with_pseudo); if (!$method_id) { if (InternalCallMapHandler::inCallMap((string) $original_method_id)) { @@ -122,7 +123,7 @@ final class MethodAnalyzer extends FunctionLikeAnalyzer throw new LogicException('Declaring method for ' . $original_method_id . ' should not be null'); } - $storage = $codebase_methods->getStorage($method_id); + $storage = $codebase_methods->getStorage($method_id, $with_pseudo); if (!$storage->is_static) { if ($self_call) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodVisibilityAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodVisibilityAnalyzer.php index 0a81ee8ec..e96edfd58 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodVisibilityAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/MethodVisibilityAnalyzer.php @@ -40,6 +40,8 @@ final class MethodVisibilityAnalyzer $fq_classlike_name = $method_id->fq_class_name; $method_name = $method_id->method_name; + $with_pseudo = true; + if ($codebase_methods->visibility_provider->has($fq_classlike_name)) { $method_visible = $codebase_methods->visibility_provider->isMethodVisible( $source, @@ -65,7 +67,7 @@ final class MethodVisibilityAnalyzer } } - $declaring_method_id = $codebase_methods->getDeclaringMethodId($method_id); + $declaring_method_id = $codebase_methods->getDeclaringMethodId($method_id, $with_pseudo); if (!$declaring_method_id) { if ($method_name === '__construct' @@ -109,7 +111,7 @@ final class MethodVisibilityAnalyzer return null; } - $storage = $codebase->methods->getStorage($declaring_method_id); + $storage = $codebase->methods->getStorage($declaring_method_id, $with_pseudo); $visibility = $storage->visibility; if ($appearing_method_name diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php index 2b76c7d66..c6e368090 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticMethod/AtomicStaticCallAnalyzer.php @@ -32,12 +32,8 @@ use Psalm\Issue\MixedMethodCall; use Psalm\Issue\UndefinedClass; use Psalm\Issue\UndefinedMethod; use Psalm\IssueBuffer; -use Psalm\Node\Expr\VirtualArray; -use Psalm\Node\Expr\VirtualArrayItem; use Psalm\Node\Expr\VirtualMethodCall; use Psalm\Node\Expr\VirtualVariable; -use Psalm\Node\Scalar\VirtualString; -use Psalm\Node\VirtualArg; use Psalm\Storage\ClassLikeStorage; use Psalm\Storage\MethodStorage; use Psalm\Type; @@ -57,7 +53,6 @@ use Psalm\Type\Atomic\TTemplateParam; use Psalm\Type\Union; use function array_filter; -use function array_map; use function array_values; use function assert; use function count; @@ -569,6 +564,8 @@ final class AtomicStaticCallAnalyzer $callstatic_method_exists = $codebase->methods->methodExists($callstatic_id); + $with_pseudo = $callstatic_method_exists; + if (!$naive_method_exists || !MethodAnalyzer::isMethodVisible( $method_id, @@ -680,36 +677,6 @@ final class AtomicStaticCallAnalyzer return false; } } - - $array_values = array_map( - static fn(PhpParser\Node\Arg $arg): PhpParser\Node\Expr\ArrayItem => new VirtualArrayItem( - $arg->value, - null, - false, - $arg->getAttributes(), - ), - $args, - ); - - $args = [ - new VirtualArg( - new VirtualString((string) $method_id, $stmt_name->getAttributes()), - false, - false, - $stmt_name->getAttributes(), - ), - new VirtualArg( - new VirtualArray($array_values, $stmt->getAttributes()), - false, - false, - $stmt->getAttributes(), - ), - ]; - - $method_id = new MethodIdentifier( - $fq_class_name, - '__callstatic', - ); } elseif ($found_method_and_class_storage && ($config->use_phpdoc_method_without_magic_or_parent || $class_storage->parent_class) ) { @@ -797,6 +764,7 @@ final class AtomicStaticCallAnalyzer new CodeLocation($statements_analyzer, $stmt), $statements_analyzer->getSuppressedIssues(), $context->calling_method_id, + $with_pseudo, ); if (!$does_method_exist) {