Skip to content

Commit

Permalink
Fix method calls and property accesses after extension_loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 19, 2023
1 parent d6186ec commit 576e4d2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,6 +153,7 @@ public static function analyze(
: null,
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(false, false, false, true),
$context->check_classes,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public static function analyze(
}

if (!$fq_class_name
|| !$context->check_classes
|| !$context->check_variables
|| ExpressionAnalyzer::isMock($fq_class_name)
) {
Expand Down Expand Up @@ -249,7 +248,7 @@ public static function analyze(
: null,
)
) {
if ($context->inside_isset) {
if ($context->inside_isset || !$context->check_classes) {
return true;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,35 @@ public function __construct() {}
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 {}
Expand Down

0 comments on commit 576e4d2

Please sign in to comment.