From 81c9daf2c2a26c5bebc70edb76c5f621f6f6ebd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Tue, 14 May 2019 10:22:55 +0200 Subject: [PATCH 1/2] Fixed resolving self::, static:: and parent:: as property or constant values --- src/NodeCompiler/CompileNodeToValue.php | 20 ++++++++++- .../NodeCompiler/CompileNodeToValueTest.php | 36 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/NodeCompiler/CompileNodeToValue.php b/src/NodeCompiler/CompileNodeToValue.php index 65f7ffc79..9a59c007d 100644 --- a/src/NodeCompiler/CompileNodeToValue.php +++ b/src/NodeCompiler/CompileNodeToValue.php @@ -98,7 +98,7 @@ 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 */ @@ -106,6 +106,8 @@ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, Compile if ($className === 'self' || $className === 'static') { $classInfo = $this->getConstantDeclaringClass($nodeName, $context->getSelf()); + } elseif ($className === 'parent') { + $classInfo = $context->getSelf()->getParentClass(); } if ($classInfo === null) { @@ -141,6 +143,22 @@ private function compileClassConstant(CompilerContext $context) : string return $context->hasSelf() ? $context->getSelf()->getName() : ''; } + private function resolveClassNameForClassNameConstant(string $className, CompilerContext $context) : string + { + if ($className === 'self' || $className === 'static') { + return $context->getSelf()->getName(); + } + + if ($className === 'parent') { + /** @var ReflectionClass $parentClass */ + $parentClass = $context->getSelf()->getParentClass(); + + return $parentClass->getName(); + } + + return $className; + } + private function getConstantDeclaringClass(string $constantName, ReflectionClass $class) : ?ReflectionClass { if ($class->hasConstant($constantName)) { 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()); + } } From eadddf5a355a1701123efdae2017a3072d0b1b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Tue, 14 May 2019 10:33:36 +0200 Subject: [PATCH 2/2] Removed non-executable code --- src/NodeCompiler/CompileNodeToValue.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/NodeCompiler/CompileNodeToValue.php b/src/NodeCompiler/CompileNodeToValue.php index 9a59c007d..de9e23abd 100644 --- a/src/NodeCompiler/CompileNodeToValue.php +++ b/src/NodeCompiler/CompileNodeToValue.php @@ -105,7 +105,7 @@ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, Compile $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(); } @@ -158,15 +158,4 @@ private function resolveClassNameForClassNameConstant(string $className, Compile return $className; } - - private function getConstantDeclaringClass(string $constantName, ReflectionClass $class) : ?ReflectionClass - { - if ($class->hasConstant($constantName)) { - return $class; - } - - $parentClass = $class->getParentClass(); - - return $parentClass ? $this->getConstantDeclaringClass($constantName, $parentClass) : null; - } }