1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix method calls and property accesses after extension_loaded

This commit is contained in:
Daniil Gentili 2023-10-19 11:16:20 +02:00
parent d6186ec32a
commit 576e4d2bc4
7 changed files with 46 additions and 29 deletions

View File

@ -203,7 +203,8 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer
?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 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer
&& !($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(

View File

@ -671,16 +671,14 @@ class AssignmentAnalyzer
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) {

View File

@ -184,6 +184,7 @@ class AtomicMethodCallAnalyzer extends CallAnalyzer
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true, false, true, true, $lhs_type_part->from_docblock),
$context->check_classes,
);
}

View File

@ -122,21 +122,6 @@ class MethodCallAnalyzer extends CallAnalyzer
$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())

View File

@ -103,7 +103,7 @@ class StaticCallAnalyzer extends CallAnalyzer
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 @@ class StaticCallAnalyzer extends CallAnalyzer
: null,
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(false, false, false, true),
$context->check_classes,
);
}

View File

@ -154,7 +154,6 @@ class StaticPropertyFetchAnalyzer
}
if (!$fq_class_name
|| !$context->check_classes
|| !$context->check_variables
|| ExpressionAnalyzer::isMock($fq_class_name)
) {
@ -249,7 +248,7 @@ class StaticPropertyFetchAnalyzer
: null,
)
) {
if ($context->inside_isset) {
if ($context->inside_isset || !$context->check_classes) {
return true;
}

View File

@ -467,6 +467,35 @@ class UnusedCodeTest extends TestCase
new A();
}',
],
'useMethodPropertiesAfterExtensionLoaded' => [
'code' => '<?php
final class a {
public static self $a;
public static function get(): a {
return new a;
}
}
final class b {
public function test(): a {
return new a;
}
}
function process(b $handler): a {
if (\extension_loaded("fdsfdsfd")) {
return $handler->test();
}
if (\extension_loaded("fdsfdsfd")) {
return a::$a;
}
if (\extension_loaded("fdsfdsfd")) {
return a::get();
}
return $handler->test();
}',
],
'usedParamInIf' => [
'code' => '<?php
class O {}