From 576e4d2bc46a674d8566b7e6f6e3d72302a827c2 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 19 Oct 2023 11:16:20 +0200 Subject: [PATCH 1/2] Fix method calls and property accesses after extension_loaded --- .../Internal/Analyzer/ClassLikeAnalyzer.php | 6 +++- .../Expression/AssignmentAnalyzer.php | 18 +++++------- .../Call/Method/AtomicMethodCallAnalyzer.php | 1 + .../Expression/Call/MethodCallAnalyzer.php | 15 ---------- .../Expression/Call/StaticCallAnalyzer.php | 3 +- .../Fetch/StaticPropertyFetchAnalyzer.php | 3 +- tests/UnusedCodeTest.php | 29 +++++++++++++++++++ 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php index 2c32967f805..3ee68c6582f 100644 --- a/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php @@ -203,7 +203,8 @@ public static function checkFullyQualifiedClassLikeName( ?string $calling_fq_class_name, ?string $calling_method_id, array $suppressed_issues, - ?ClassLikeNameOptions $options = null + ?ClassLikeNameOptions $options = null, + bool $check_classes = true ): ?bool { if ($options === null) { $options = new ClassLikeNameOptions(); @@ -276,6 +277,9 @@ public static function checkFullyQualifiedClassLikeName( && !($interface_exists && $options->allow_interface) && !($enum_exists && $options->allow_enum) ) { + if (!$check_classes) { + return null; + } if (!$options->allow_trait || !$codebase->classlikes->traitExists($fq_class_name, $code_location)) { if ($options->from_docblock) { if (IssueBuffer::accepts( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index 76c7073c12e..c59a554d9ab 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -671,16 +671,14 @@ private static function analyzeAssignment( return false; } - if ($context->check_classes) { - if (StaticPropertyAssignmentAnalyzer::analyze( - $statements_analyzer, - $assign_var, - $assign_value, - $assign_value_type, - $context, - ) === false) { - return false; - } + if (StaticPropertyAssignmentAnalyzer::analyze( + $statements_analyzer, + $assign_var, + $assign_value, + $assign_value_type, + $context, + ) === false) { + return false; } if ($var_id) { 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 64567213200..fca21662783 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/Method/AtomicMethodCallAnalyzer.php @@ -184,6 +184,7 @@ public static function analyze( $context->calling_method_id, $statements_analyzer->getSuppressedIssues(), new ClassLikeNameOptions(true, false, true, true, $lhs_type_part->from_docblock), + $context->check_classes, ); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php index 437f65d7510..72c343d1073 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php @@ -122,21 +122,6 @@ public static function analyze( $statements_analyzer->node_data->setType($stmt, Type::getMixed()); } - if (!$context->check_classes) { - if (ArgumentsAnalyzer::analyze( - $statements_analyzer, - $stmt->getArgs(), - null, - null, - true, - $context, - ) === false) { - return false; - } - - return true; - } - if ($class_type && $stmt->name instanceof PhpParser\Node\Identifier && ($class_type->isNull() || $class_type->isVoid()) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php index fa7cc498184..6a0a95ff2da 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php @@ -103,7 +103,7 @@ public static function analyze( if ($context->isPhantomClass($fq_class_name)) { return true; } - } elseif ($context->check_classes) { + } else { $aliases = $statements_analyzer->getAliases(); if ($context->calling_method_id @@ -153,6 +153,7 @@ public static function analyze( : null, $statements_analyzer->getSuppressedIssues(), new ClassLikeNameOptions(false, false, false, true), + $context->check_classes, ); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php index 46a4cf0f414..35bc7061427 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/StaticPropertyFetchAnalyzer.php @@ -154,7 +154,6 @@ public static function analyze( } if (!$fq_class_name - || !$context->check_classes || !$context->check_variables || ExpressionAnalyzer::isMock($fq_class_name) ) { @@ -249,7 +248,7 @@ public static function analyze( : null, ) ) { - if ($context->inside_isset) { + if ($context->inside_isset || !$context->check_classes) { return true; } diff --git a/tests/UnusedCodeTest.php b/tests/UnusedCodeTest.php index 82fe731886a..ef570f8a6f0 100644 --- a/tests/UnusedCodeTest.php +++ b/tests/UnusedCodeTest.php @@ -467,6 +467,35 @@ public function __construct() {} new A(); }', ], + 'useMethodPropertiesAfterExtensionLoaded' => [ + 'code' => 'test(); + } + if (\extension_loaded("fdsfdsfd")) { + return a::$a; + } + if (\extension_loaded("fdsfdsfd")) { + return a::get(); + } + return $handler->test(); + }', + ], 'usedParamInIf' => [ 'code' => ' Date: Thu, 19 Oct 2023 11:23:24 +0200 Subject: [PATCH 2/2] Fix --- .../Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php index 6a0a95ff2da..8302dcfca44 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/StaticCallAnalyzer.php @@ -54,8 +54,6 @@ public static function analyze( $config = $codebase->config; if ($stmt->class instanceof PhpParser\Node\Name) { - $fq_class_name = null; - if (count($stmt->class->getParts()) === 1 && in_array(strtolower($stmt->class->getFirst()), ['self', 'static', 'parent'], true) ) {