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

Fix #5264 - use accurate static type when calling parent method in trait

This commit is contained in:
Matt Brown 2021-02-23 20:48:22 -05:00
parent def7326ae1
commit cafbdb6831
3 changed files with 33 additions and 1 deletions

View File

@ -918,7 +918,9 @@ class MethodComparator
$implementer_method_storage_return_type = \Psalm\Internal\Type\TypeExpander::expandUnion(
$codebase,
$implementer_return_type,
$implementer_classlike_storage->name,
$implementer_classlike_storage->is_trait
? $implementer_called_class_name
: $implementer_classlike_storage->name,
$implementer_called_class_name,
$implementer_classlike_storage->parent_class
);

View File

@ -270,6 +270,12 @@ class ExistingAtomicStaticCallAnalyzer
: Type::getObject(),
$lhs_type_part->defining_class
);
} elseif ($stmt->class instanceof PhpParser\Node\Name
&& count($stmt->class->parts) === 1
&& in_array(strtolower($stmt->class->parts[0]), ['self', 'static', 'parent'], true)
&& $lhs_type_part instanceof Type\Atomic\TNamedObject
) {
$static_type = $context->self;
} else {
$static_type = $fq_class_name;
}

View File

@ -913,6 +913,30 @@ class MethodSignatureTest extends TestCase
}
}'
],
'callParentMethodFromTrait' => [
'<?php
class MyParentClass
{
/** @return static */
public function myMethod()
{
return $this;
}
}
trait MyTrait
{
final public function myMethod() : self
{
return parent::myMethod();
}
}
class MyChildClass extends MyParentClass
{
use MyTrait;
}'
],
];
}