Skip to content

[PropertyInfo] fix resolving parent|self type hints #27287

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

Merged
merged 1 commit into from
May 16, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private function extractFromMutator($class, $property)
if (!$reflectionType = $reflectionParameter->getType()) {
return;
}
$type = $this->extractFromReflectionType($reflectionType);
$type = $this->extractFromReflectionType($reflectionType, $reflectionMethod);

// HHVM reports variadics with "array" but not builtin type hints
if (!$reflectionType->isBuiltin() && Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
Expand All @@ -188,7 +188,7 @@ private function extractFromMutator($class, $property)
} elseif (Type::BUILTIN_TYPE_CALLABLE === $info[1]) {
$type = new Type(Type::BUILTIN_TYPE_CALLABLE, $reflectionParameter->allowsNull());
} else {
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $reflectionParameter->allowsNull(), $info[1]);
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $reflectionParameter->allowsNull(), $this->resolveTypeName($info[1], $reflectionMethod));
}
} else {
return;
Expand Down Expand Up @@ -217,7 +217,7 @@ private function extractFromAccessor($class, $property)
}

if ($this->supportsParameterType && $reflectionType = $reflectionMethod->getReturnType()) {
return array($this->extractFromReflectionType($reflectionType));
return array($this->extractFromReflectionType($reflectionType, $reflectionMethod));
}

if (\in_array($prefix, array('is', 'can'))) {
Expand All @@ -228,11 +228,9 @@ private function extractFromAccessor($class, $property)
/**
* Extracts data from the PHP 7 reflection type.
*
* @param \ReflectionType $reflectionType
*
* @return Type
*/
private function extractFromReflectionType(\ReflectionType $reflectionType)
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionMethod $reflectionMethod)
{
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : $reflectionType->__toString();
$nullable = $reflectionType->allowsNull();
Expand All @@ -244,12 +242,24 @@ private function extractFromReflectionType(\ReflectionType $reflectionType)
} elseif ($reflectionType->isBuiltin()) {
$type = new Type($phpTypeOrClass, $nullable);
} else {
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $phpTypeOrClass);
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $reflectionMethod));
}

return $type;
}

private function resolveTypeName($name, \ReflectionMethod $reflectionMethod)
{
if ('self' === $lcName = strtolower($name)) {
return $reflectionMethod->getDeclaringClass()->name;
}
if ('parent' === $lcName && $parent = $reflectionMethod->getDeclaringClass()->getParentClass()) {
return $parent->name;
}

return $name;
}

/**
* Does the class have the given public property?
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyInfo\Tests\PhpDocExtractors;
namespace Symfony\Component\PropertyInfo\Tests\PhpDocExtractor;

use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function testGetProperties()
'DOB',
'Id',
'123',
'self',
'realParent',
'c',
'd',
'e',
Expand Down Expand Up @@ -135,6 +137,8 @@ public function typesProvider()
array('donotexist', null),
array('staticGetter', null),
array('staticSetter', null),
array('self', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy'))),
array('realParent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy'))),
);
}

Expand All @@ -153,6 +157,8 @@ public function php7TypesProvider()
array('foo', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true))),
array('bar', array(new Type(Type::BUILTIN_TYPE_INT))),
array('baz', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
array('buz', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy'))),
array('biz', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'stdClass'))),
array('donotexist', null),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyInfo\Tests\Extractors;
namespace Symfony\Component\PropertyInfo\Tests\Extractor;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,18 @@ public function getId()
public function get123()
{
}

/**
* @param self $self
*/
public function setSelf(self $self)
{
}

/**
* @param parent $realParent
*/
public function setRealParent(parent $realParent)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Php7Dummy
class Php7Dummy extends \stdClass
{
public function getFoo(): array
{
Expand All @@ -27,4 +27,12 @@ public function setBar(int $bar)
public function addBaz(string $baz)
{
}

public function getBuz(): self
{
}

public function getBiz(): parent
{
}
}