Skip to content

Commit

Permalink
Merge pull request #7084 from orklah/7081
Browse files Browse the repository at this point in the history
Don't crash when checking purity of __callStatic in a trait
  • Loading branch information
orklah authored Dec 6, 2021
2 parents 4a35a85 + 257d237 commit 77e0f09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public static function analyze(

/**
* @psalm-suppress UnusedReturnValue not used but seems important
* @psalm-suppress ComplexMethod to be refactored
*/
private static function handleNamedCall(
StatementsAnalyzer $statements_analyzer,
Expand Down Expand Up @@ -477,7 +478,13 @@ private static function handleNamedCall(
)) {
$callstatic_appearing_id = $codebase->methods->getAppearingMethodId($callstatic_id);
assert($callstatic_appearing_id !== null);
$callstatic_storage = $codebase->methods->getStorage($callstatic_appearing_id);
$callstatic_pure = false;
$callstatic_mutation_free = false;
if ($codebase->methods->hasStorage($callstatic_appearing_id)) {
$callstatic_storage = $codebase->methods->getStorage($callstatic_appearing_id);
$callstatic_pure = $callstatic_storage->pure;
$callstatic_mutation_free = $callstatic_storage->mutation_free;
}
if ($codebase->methods->return_type_provider->has($fq_class_name)) {
$return_type_candidate = $codebase->methods->return_type_provider->getReturnType(
$statements_analyzer,
Expand Down Expand Up @@ -525,15 +532,15 @@ private static function handleNamedCall(
}

if (!$context->inside_throw) {
if ($context->pure && !$callstatic_storage->pure) {
if ($context->pure && !$callstatic_pure) {
IssueBuffer::maybeAdd(
new ImpureMethodCall(
'Cannot call an impure method from a pure context',
new CodeLocation($statements_analyzer, $stmt_name)
),
$statements_analyzer->getSuppressedIssues()
);
} elseif ($context->mutation_free&& !$callstatic_storage->mutation_free) {
} elseif ($context->mutation_free && !$callstatic_mutation_free) {
IssueBuffer::maybeAdd(
new ImpureMethodCall(
'Cannot call a possibly-mutating method from a mutation-free context',
Expand All @@ -544,9 +551,9 @@ private static function handleNamedCall(
} elseif ($statements_analyzer->getSource()
instanceof FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
&& !$callstatic_storage->pure
&& !$callstatic_pure
) {
if (!$callstatic_storage->mutation_free) {
if (!$callstatic_mutation_free) {
$statements_analyzer->getSource()->inferred_has_mutation = true;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/PureAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,23 @@ function gimmeFoo(): MyEnum
return MyEnum::FOO();
}',
],
'dontCrashWhileCheckingPurityOnCallStaticInATrait' => [
'<?php
/**
* @method static static tt()
*/
trait Date {
public static function __callStatic(string $_method, array $_parameters){
}
}
class Date2{
use Date;
}
Date2::tt();
',
],
];
}

Expand Down

0 comments on commit 77e0f09

Please sign in to comment.