1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #7084 from orklah/7081

Don't crash when checking purity of __callStatic in a trait
This commit is contained in:
orklah 2021-12-06 23:17:20 +01:00 committed by GitHub
commit 77e0f09b43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -242,6 +242,7 @@ class AtomicStaticCallAnalyzer
/**
* @psalm-suppress UnusedReturnValue not used but seems important
* @psalm-suppress ComplexMethod to be refactored
*/
private static function handleNamedCall(
StatementsAnalyzer $statements_analyzer,
@ -477,7 +478,13 @@ class AtomicStaticCallAnalyzer
)) {
$callstatic_appearing_id = $codebase->methods->getAppearingMethodId($callstatic_id);
assert($callstatic_appearing_id !== null);
$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,
@ -525,7 +532,7 @@ class AtomicStaticCallAnalyzer
}
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',
@ -533,7 +540,7 @@ class AtomicStaticCallAnalyzer
),
$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',
@ -544,9 +551,9 @@ class AtomicStaticCallAnalyzer
} 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;
}

View File

@ -445,6 +445,23 @@ class PureAnnotationTest extends TestCase
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();
',
],
];
}