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

Add support for get_parent_class() with no args

This commit is contained in:
Matthew Brown 2019-01-05 15:42:56 -05:00
parent d85c9d8aa2
commit 58acaefe70
2 changed files with 42 additions and 0 deletions

View File

@ -82,6 +82,24 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
case 'microtime':
return Type::getString();
case 'get_called_class':
return new Type\Union([new Type\Atomic\TClassString($context->self ?: 'object')]);
case 'get_parent_class':
$codebase = $statements_analyzer->getCodebase();
if ($context->self && $codebase->classExists($context->self)) {
$classlike_storage = $codebase->classlike_storage_provider->get($context->self);
if ($classlike_storage->parent_classes) {
return new Type\Union([
new Type\Atomic\TClassString(
array_values($classlike_storage->parent_classes)[0]
)
]);
}
}
}
} else {
switch ($call_map_key) {

View File

@ -322,6 +322,17 @@ class ClassStringTest extends TestCase
return $a::class;
}',
],
'returnGetCalledClassClassStringParameterized' => [
'<?php
class A {
/**
* @return class-string<A> $s
*/
function foo() : string {
return get_called_class();
}
}',
],
'returnGetClassClassStringParameterized' => [
'<?php
class A {}
@ -333,6 +344,19 @@ class ClassStringTest extends TestCase
return get_class($a);
}',
],
'returnGetParentClassClassStringParameterized' => [
'<?php
class A {}
class B extends A {
/**
* @return class-string<A> $s
*/
function foo() : string {
return get_parent_class();
}
}',
],
'createClassOfTypeFromString' => [
'<?php
class A {}