diff --git a/src/NodeCompiler/CompileNodeToValue.php b/src/NodeCompiler/CompileNodeToValue.php index 65f7ffc79..de9e23abd 100644 --- a/src/NodeCompiler/CompileNodeToValue.php +++ b/src/NodeCompiler/CompileNodeToValue.php @@ -98,14 +98,16 @@ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, Compile $className = $node->class->toString(); if ($nodeName === 'class') { - return $className; + return $this->resolveClassNameForClassNameConstant($className, $context); } /** @var ReflectionClass|null $classInfo */ $classInfo = null; if ($className === 'self' || $className === 'static') { - $classInfo = $this->getConstantDeclaringClass($nodeName, $context->getSelf()); + $classInfo = $context->getSelf()->hasConstant($nodeName) ? $context->getSelf() : null; + } elseif ($className === 'parent') { + $classInfo = $context->getSelf()->getParentClass(); } if ($classInfo === null) { @@ -141,14 +143,19 @@ private function compileClassConstant(CompilerContext $context) : string return $context->hasSelf() ? $context->getSelf()->getName() : ''; } - private function getConstantDeclaringClass(string $constantName, ReflectionClass $class) : ?ReflectionClass + private function resolveClassNameForClassNameConstant(string $className, CompilerContext $context) : string { - if ($class->hasConstant($constantName)) { - return $class; + if ($className === 'self' || $className === 'static') { + return $context->getSelf()->getName(); } - $parentClass = $class->getParentClass(); + if ($className === 'parent') { + /** @var ReflectionClass $parentClass */ + $parentClass = $context->getSelf()->getParentClass(); + + return $parentClass->getName(); + } - return $parentClass ? $this->getConstantDeclaringClass($constantName, $parentClass) : null; + return $className; } } diff --git a/test/unit/NodeCompiler/CompileNodeToValueTest.php b/test/unit/NodeCompiler/CompileNodeToValueTest.php index 86925dd22..264352029 100644 --- a/test/unit/NodeCompiler/CompileNodeToValueTest.php +++ b/test/unit/NodeCompiler/CompileNodeToValueTest.php @@ -441,4 +441,40 @@ class Bar { $classInfo = $reflector->reflect('Bar'); self::assertSame('baz', $classInfo->getProperty('property')->getDefaultValue()); } + + public function testSelfStaticOrParentAsPropertyDefaultValue() : void + { + $phpCode = <<<'PHP' + astLocator)); + $classInfo = $reflector->reflect('Foo'); + + self::assertSame('Foo', $classInfo->getProperty('selfClass')->getDefaultValue()); + self::assertSame('Foo', $classInfo->getProperty('staticClass')->getDefaultValue()); + self::assertSame('Baz', $classInfo->getProperty('parentClass')->getDefaultValue()); + + self::assertSame('selfConstant', $classInfo->getProperty('selfConstant')->getDefaultValue()); + self::assertSame('staticConstant', $classInfo->getProperty('staticConstant')->getDefaultValue()); + self::assertSame('parentConstant', $classInfo->getProperty('parentConstant')->getDefaultValue()); + } }