Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed resolving self::, static:: and parent:: as property or constant values #472

Merged
merged 2 commits into from May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/NodeCompiler/CompileNodeToValue.php
Expand Up @@ -98,14 +98,16 @@ private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, Compile
$className = $node->class->toString();

if ($nodeName === 'class') {
kukulich marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down Expand Up @@ -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;
}
}
36 changes: 36 additions & 0 deletions test/unit/NodeCompiler/CompileNodeToValueTest.php
Expand Up @@ -441,4 +441,40 @@ class Bar {
$classInfo = $reflector->reflect('Bar');
self::assertSame('baz', $classInfo->getProperty('property')->getDefaultValue());
}

public function testSelfStaticOrParentAsPropertyDefaultValue() : void
{
$phpCode = <<<'PHP'
<?php

class Baz {
const PARENT_CONSTANT = 'parentConstant';
}

class Foo extends Baz {
const SELF_CONSTANT = 'selfConstant';
const STATIC_CONSTANT = 'staticConstant';
const PARENT_CONSTANT = 'selfConstant';

public $selfClass = self::class;
public $staticClass = static::class;
public $parentClass = parent::class;

public $selfConstant = self::SELF_CONSTANT;
public $staticConstant = self::STATIC_CONSTANT;
public $parentConstant = parent::PARENT_CONSTANT;
}
PHP;

$reflector = new ClassReflector(new StringSourceLocator($phpCode, $this->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());
}
}