1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Checking psalm-if-this-is before applying psalm-this-out

This commit is contained in:
adrew 2021-12-30 21:48:25 +03:00
parent 7b186732c8
commit 4a903c71a1
3 changed files with 43 additions and 25 deletions

View File

@ -22,6 +22,7 @@ use Psalm\Internal\Type\Comparator\TypeComparisonResult;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Issue\IfThisIsMismatch;
use Psalm\Issue\InvalidPropertyAssignmentValue;
use Psalm\Issue\MixedPropertyTypeCoercion;
use Psalm\Issue\PossiblyInvalidPropertyAssignmentValue;
@ -270,6 +271,25 @@ class ExistingAtomicMethodCallAnalyzer extends CallAnalyzer
}
if ($method_storage) {
$class_type = new Union([$lhs_type_part]);
if ($method_storage->if_this_is_type
&& !UnionTypeComparator::isContainedBy(
$codebase,
$class_type,
$method_storage->if_this_is_type
)
) {
IssueBuffer::maybeAdd(
new IfThisIsMismatch(
'Class type must be ' . $method_storage->if_this_is_type->getId()
. ' current type ' . $class_type->getId(),
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
);
}
if ($method_storage->self_out_type && $lhs_var_id) {
$self_out_candidate = clone $method_storage->self_out_type;

View File

@ -423,30 +423,6 @@ class MethodCallAnalyzer extends CallAnalyzer
$context->vars_in_scope[$lhs_var_id] = $class_type;
}
if ($lhs_var_id) {
$method_id = MethodIdentifier::wrap($result->existent_method_ids[0]);
// TODO: When should a method have a storage?
if ($codebase->methods->hasStorage($method_id)) {
$storage = $codebase->methods->getStorage($method_id);
if ($storage->if_this_is_type
&& !UnionTypeComparator::isContainedBy(
$codebase,
$class_type,
$storage->if_this_is_type
)
) {
IssueBuffer::maybeAdd(
new IfThisIsMismatch(
'Class is not ' . (string) $storage->if_this_is_type
. ' as required by psalm-if-this-is',
new CodeLocation($source, $stmt->name)
),
$statements_analyzer->getSuppressedIssues()
);
}
}
}
return true;
}

View File

@ -111,7 +111,29 @@ class IfThisIsTest extends TestCase
$f = new F();
$f->test();
'
]
],
'ifThisIsAndThisOutAtTheSameTime' => [
'<?php
/**
* @template T of string
*/
final class App
{
/**
* @psalm-if-this-is App<"idle">
* @psalm-this-out App<"started">
*/
public function start(): void
{
throw new RuntimeException("???");
}
}
/** @var App<"idle"> */
$app = new App();
$app->start();
'
],
];
}