Skip to content

Commit 88a3b90

Browse files
[PropertyInfo] fix resolving parent|self type hints
1 parent 10a2d39 commit 88a3b90

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private function extractFromMutator($class, $property)
176176
if (!$reflectionType = $reflectionParameter->getType()) {
177177
return;
178178
}
179-
$type = $this->extractFromReflectionType($reflectionType);
179+
$type = $this->extractFromReflectionType($reflectionType, $reflectionMethod);
180180

181181
// HHVM reports variadics with "array" but not builtin type hints
182182
if (!$reflectionType->isBuiltin() && Type::BUILTIN_TYPE_ARRAY === $type->getBuiltinType()) {
@@ -188,7 +188,7 @@ private function extractFromMutator($class, $property)
188188
} elseif (Type::BUILTIN_TYPE_CALLABLE === $info[1]) {
189189
$type = new Type(Type::BUILTIN_TYPE_CALLABLE, $reflectionParameter->allowsNull());
190190
} else {
191-
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $reflectionParameter->allowsNull(), $info[1]);
191+
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $reflectionParameter->allowsNull(), $this->resolveTypeName($info[1], $reflectionMethod));
192192
}
193193
} else {
194194
return;
@@ -217,7 +217,7 @@ private function extractFromAccessor($class, $property)
217217
}
218218

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

223223
if (\in_array($prefix, array('is', 'can'))) {
@@ -228,11 +228,9 @@ private function extractFromAccessor($class, $property)
228228
/**
229229
* Extracts data from the PHP 7 reflection type.
230230
*
231-
* @param \ReflectionType $reflectionType
232-
*
233231
* @return Type
234232
*/
235-
private function extractFromReflectionType(\ReflectionType $reflectionType)
233+
private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionMethod $reflectionMethod)
236234
{
237235
$phpTypeOrClass = $reflectionType instanceof \ReflectionNamedType ? $reflectionType->getName() : $reflectionType->__toString();
238236
$nullable = $reflectionType->allowsNull();
@@ -244,12 +242,24 @@ private function extractFromReflectionType(\ReflectionType $reflectionType)
244242
} elseif ($reflectionType->isBuiltin()) {
245243
$type = new Type($phpTypeOrClass, $nullable);
246244
} else {
247-
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $phpTypeOrClass);
245+
$type = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $this->resolveTypeName($phpTypeOrClass, $reflectionMethod));
248246
}
249247

250248
return $type;
251249
}
252250

251+
private function resolveTypeName($name, \ReflectionMethod $reflectionMethod)
252+
{
253+
if ('self' === $lcName = strtolower($name)) {
254+
return $reflectionMethod->getDeclaringClass()->name;
255+
}
256+
if ('parent' === $lcName && $parent = $reflectionMethod->getDeclaringClass()->getParentClass()) {
257+
return $parent->name;
258+
}
259+
260+
return $name;
261+
}
262+
253263
/**
254264
* Does the class have the given public property?
255265
*

src/Symfony/Component/PropertyInfo/Tests/Extractors/PhpDocExtractorTest.php renamed to src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\PropertyInfo\Tests\PhpDocExtractors;
12+
namespace Symfony\Component\PropertyInfo\Tests\PhpDocExtractor;
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;

src/Symfony/Component/PropertyInfo/Tests/Extractors/ReflectionExtractorTest.php renamed to src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function testGetProperties()
5252
'DOB',
5353
'Id',
5454
'123',
55+
'self',
56+
'realParent',
5557
'c',
5658
'd',
5759
'e',
@@ -135,6 +137,8 @@ public function typesProvider()
135137
array('donotexist', null),
136138
array('staticGetter', null),
137139
array('staticSetter', null),
140+
array('self', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy'))),
141+
array('realParent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy'))),
138142
);
139143
}
140144

@@ -153,6 +157,8 @@ public function php7TypesProvider()
153157
array('foo', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true))),
154158
array('bar', array(new Type(Type::BUILTIN_TYPE_INT))),
155159
array('baz', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
160+
array('buz', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy'))),
161+
array('biz', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'stdClass'))),
156162
array('donotexist', null),
157163
);
158164
}

src/Symfony/Component/PropertyInfo/Tests/Extractors/SerializerExtractorTest.php renamed to src/Symfony/Component/PropertyInfo/Tests/Extractor/SerializerExtractorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\PropertyInfo\Tests\Extractors;
12+
namespace Symfony\Component\PropertyInfo\Tests\Extractor;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use PHPUnit\Framework\TestCase;

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php

+14
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,18 @@ public function getId()
127127
public function get123()
128128
{
129129
}
130+
131+
/**
132+
* @param self $self
133+
*/
134+
public function setSelf(self $self)
135+
{
136+
}
137+
138+
/**
139+
* @param parent $realParent
140+
*/
141+
public function setRealParent(parent $realParent)
142+
{
143+
}
130144
}

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php7Dummy.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @author Kévin Dunglas <dunglas@gmail.com>
1616
*/
17-
class Php7Dummy
17+
class Php7Dummy extends \stdClass
1818
{
1919
public function getFoo(): array
2020
{
@@ -27,4 +27,12 @@ public function setBar(int $bar)
2727
public function addBaz(string $baz)
2828
{
2929
}
30+
31+
public function getBuz(): self
32+
{
33+
}
34+
35+
public function getBiz(): parent
36+
{
37+
}
3038
}

0 commit comments

Comments
 (0)