Skip to content

Commit

Permalink
Don't crash when checking purity of __callStatic in a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Dec 6, 2021
1 parent 4a35a85 commit 75312d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,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 +531,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 +550,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 75312d9

Please sign in to comment.