Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check __callStatic purity instead of the pseudoMethod purity #6953

Merged
merged 2 commits into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,6 +34,7 @@

use function array_filter;
use function array_map;
use function assert;
use function count;
use function in_array;
use function strtolower;
Expand Down Expand Up @@ -469,6 +470,9 @@ private static function handleNamedCall(
true,
$context->insideUse()
)) {
$callstatic_appearing_id = $codebase->methods->getAppearingMethodId($callstatic_id);
assert($callstatic_appearing_id !== null);
$callstatic_storage = $codebase->methods->getStorage($callstatic_appearing_id);
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 @@ -516,7 +520,7 @@ private static function handleNamedCall(
}

if (!$context->inside_throw) {
if ($context->pure && !$pseudo_method_storage->pure) {
if ($context->pure && !$callstatic_storage->pure) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call an impure method from a pure context',
Expand All @@ -526,7 +530,7 @@ private static function handleNamedCall(
)) {
// fall through
}
} elseif ($context->mutation_free && !$pseudo_method_storage->mutation_free) {
} elseif ($context->mutation_free&& !$callstatic_storage->mutation_free) {
if (IssueBuffer::accepts(
new ImpureMethodCall(
'Cannot call a possibly-mutating method from a mutation-free context',
Expand All @@ -539,9 +543,9 @@ private static function handleNamedCall(
} elseif ($statements_analyzer->getSource()
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
&& !$pseudo_method_storage->pure
&& !$callstatic_storage->pure
) {
if (!$pseudo_method_storage->mutation_free) {
if (!$callstatic_storage->mutation_free) {
$statements_analyzer->getSource()->inferred_has_mutation = true;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/PureAnnotationTest.php
Expand Up @@ -413,6 +413,35 @@ private function isValidPort(int $portNumber): bool {
}
}'
],
'pureThroughCallStatic' => [
'<?php

/**
* @method static self FOO()
* @method static static BAR()
* @method static static BAZ()
*
* @psalm-immutable
*/
class MyEnum
{
const FOO = "foo";
const BAR = "bar";
const BAZ = "baz";

/** @psalm-pure */
public static function __callStatic(string $name, array $params): static
{
throw new BadMethodCallException("not implemented");
}
}

/** @psalm-pure */
function gimmeFoo(): MyEnum
{
return MyEnum::FOO();
}',
],
];
}

Expand Down