From 58acaefe706f346fb28a9dfb7f863135ee33a776 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sat, 5 Jan 2019 15:42:56 -0500 Subject: [PATCH] Add support for get_parent_class() with no args --- .../Internal/Analyzer/FunctionAnalyzer.php | 18 ++++++++++++++ tests/ClassStringTest.php | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php index af01f3117..38007c9e6 100644 --- a/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionAnalyzer.php @@ -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) { diff --git a/tests/ClassStringTest.php b/tests/ClassStringTest.php index 4ae88a578..01b0a4382 100644 --- a/tests/ClassStringTest.php +++ b/tests/ClassStringTest.php @@ -322,6 +322,17 @@ class ClassStringTest extends TestCase return $a::class; }', ], + 'returnGetCalledClassClassStringParameterized' => [ + ' $s + */ + function foo() : string { + return get_called_class(); + } + }', + ], 'returnGetClassClassStringParameterized' => [ ' [ + ' $s + */ + function foo() : string { + return get_parent_class(); + } + }', + ], 'createClassOfTypeFromString' => [ '