Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a313180

Browse filesBrowse files
xabbuhnicolas-grekas
authored andcommitted
fix resolving parent/self/static type annotations
1 parent 08c789c commit a313180
Copy full SHA for a313180

File tree

Expand file treeCollapse file tree

8 files changed

+119
-10
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+119
-10
lines changed

‎src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,36 @@ public function getTypes($class, $property, array $context = []): ?array
142142
break;
143143
}
144144

145+
$parentClass = null;
145146
$types = [];
146147
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
147148
foreach ($docBlock->getTagsByName($tag) as $tag) {
148149
if ($tag && !$tag instanceof InvalidTag && null !== $tag->getType()) {
149-
$types = array_merge($types, $this->phpDocTypeHelper->getTypes($tag->getType()));
150+
foreach ($this->phpDocTypeHelper->getTypes($tag->getType()) as $type) {
151+
if (Type::BUILTIN_TYPE_OBJECT !== $type->getBuiltinType()) {
152+
$types[] = $type;
153+
continue;
154+
}
155+
156+
switch ($type->getClassName()) {
157+
case 'self':
158+
case 'static':
159+
$resolvedClass = $class;
160+
break;
161+
162+
case 'parent':
163+
if (false !== $resolvedClass = $parentClass ?? $parentClass = get_parent_class($class)) {
164+
break;
165+
}
166+
// no break
167+
168+
default:
169+
$types[] = $type;
170+
continue 2;
171+
}
172+
173+
$types[] = new Type(Type::BUILTIN_TYPE_OBJECT, $type->isNullable(), $resolvedClass, $type->isCollection(), $type->getCollectionKeyType(), $type->getCollectionValueType());
174+
}
150175
}
151176
}
152177

‎src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Framework\TestCase;
1818
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1919
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
20+
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
2122
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
2223
use Symfony\Component\PropertyInfo\Type;
@@ -120,6 +121,7 @@ public function typesProvider()
120121
['staticGetter', null, null, null],
121122
['staticSetter', null, null, null],
122123
['emptyVar', null, $this->isPhpDocumentorV5() ? 'This should not be removed.' : null, null],
124+
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], null, null],
123125
];
124126
}
125127

@@ -293,6 +295,38 @@ public function propertiesDefinedByTraitsProvider(): array
293295
];
294296
}
295297

298+
/**
299+
* @dataProvider propertiesStaticTypeProvider
300+
*/
301+
public function testPropertiesStaticType(string $class, string $property, Type $type)
302+
{
303+
$this->assertEquals([$type], $this->extractor->getTypes($class, $property));
304+
}
305+
306+
public function propertiesStaticTypeProvider(): array
307+
{
308+
return [
309+
[ParentDummy::class, 'propertyTypeStatic', new Type(Type::BUILTIN_TYPE_OBJECT, false, ParentDummy::class)],
310+
[Dummy::class, 'propertyTypeStatic', new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)],
311+
];
312+
}
313+
314+
/**
315+
* @dataProvider propertiesParentTypeProvider
316+
*/
317+
public function testPropertiesParentType(string $class, string $property, ?array $types)
318+
{
319+
$this->assertEquals($types, $this->extractor->getTypes($class, $property));
320+
}
321+
322+
public function propertiesParentTypeProvider(): array
323+
{
324+
return [
325+
[ParentDummy::class, 'parentAnnotationNoParent', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'parent')]],
326+
[Dummy::class, 'parentAnnotation', [new Type(Type::BUILTIN_TYPE_OBJECT, false, ParentDummy::class)]],
327+
];
328+
}
329+
296330
protected function isPhpDocumentorV5()
297331
{
298332
if (class_exists(InvalidTag::class)) {

‎src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php
+20-8Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71Dummy;
2121
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php71DummyExtended2;
2222
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php74Dummy;
23+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy;
24+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7ParentDummy;
2325
use Symfony\Component\PropertyInfo\Type;
2426

2527
/**
@@ -57,12 +59,15 @@ public function testGetProperties()
5759
'iteratorCollection',
5860
'iteratorCollectionWithKey',
5961
'nestedIterators',
62+
'parentAnnotation',
6063
'foo',
6164
'foo2',
6265
'foo3',
6366
'foo4',
6467
'foo5',
6568
'files',
69+
'propertyTypeStatic',
70+
'parentAnnotationNoParent',
6671
'a',
6772
'DOB',
6873
'Id',
@@ -105,12 +110,15 @@ public function testGetPropertiesWithCustomPrefixes()
105110
'iteratorCollection',
106111
'iteratorCollectionWithKey',
107112
'nestedIterators',
113+
'parentAnnotation',
108114
'foo',
109115
'foo2',
110116
'foo3',
111117
'foo4',
112118
'foo5',
113119
'files',
120+
'propertyTypeStatic',
121+
'parentAnnotationNoParent',
114122
'date',
115123
'c',
116124
'd',
@@ -143,12 +151,15 @@ public function testGetPropertiesWithNoPrefixes()
143151
'iteratorCollection',
144152
'iteratorCollectionWithKey',
145153
'nestedIterators',
154+
'parentAnnotation',
146155
'foo',
147156
'foo2',
148157
'foo3',
149158
'foo4',
150159
'foo5',
151160
'files',
161+
'propertyTypeStatic',
162+
'parentAnnotationNoParent',
152163
],
153164
$noPrefixExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
154165
);
@@ -184,20 +195,21 @@ public function typesProvider()
184195
/**
185196
* @dataProvider php7TypesProvider
186197
*/
187-
public function testExtractPhp7Type($property, array $type = null)
198+
public function testExtractPhp7Type(string $class, string $property, array $type = null)
188199
{
189-
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy', $property, []));
200+
$this->assertEquals($type, $this->extractor->getTypes($class, $property, []));
190201
}
191202

192203
public function php7TypesProvider()
193204
{
194205
return [
195-
['foo', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
196-
['bar', [new Type(Type::BUILTIN_TYPE_INT)]],
197-
['baz', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
198-
['buz', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy')]],
199-
['biz', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'stdClass')]],
200-
['donotexist', null],
206+
[Php7Dummy::class, 'foo', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
207+
[Php7Dummy::class, 'bar', [new Type(Type::BUILTIN_TYPE_INT)]],
208+
[Php7Dummy::class, 'baz', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
209+
[Php7Dummy::class, 'buz', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy')]],
210+
[Php7Dummy::class, 'biz', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Php7ParentDummy::class)]],
211+
[Php7Dummy::class, 'donotexist', null],
212+
[Php7ParentDummy::class, 'parent', [new Type(Type::BUILTIN_TYPE_OBJECT, false, \stdClass::class)]],
201213
];
202214
}
203215

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ class Dummy extends ParentDummy
130130
*/
131131
public $nestedIterators;
132132

133+
/**
134+
* @var parent
135+
*/
136+
public $parentAnnotation;
137+
133138
public static function getStatic()
134139
{
135140
}

‎src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class ParentDummy
4848
*/
4949
public $files;
5050

51+
/**
52+
* @var static
53+
*/
54+
public $propertyTypeStatic;
55+
56+
/**
57+
* @var parent
58+
*/
59+
public $parentAnnotationNoParent;
60+
5161
/**
5262
* @return bool|null
5363
*/

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php7Dummy.php
+1-1Lines changed: 1 addition & 1 deletion
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 extends \stdClass
17+
class Php7Dummy extends Php7ParentDummy
1818
{
1919
public function getFoo(): array
2020
{
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
13+
14+
class Php7ParentDummy extends \stdClass
15+
{
16+
public function getParent(): parent
17+
{
18+
}
19+
}

‎src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ private function getPhpTypeAndClass(string $docType): array
160160
return [$docType, null];
161161
}
162162

163+
if (\in_array($docType, ['parent', 'self', 'static'], true)) {
164+
return ['object', $docType];
165+
}
166+
163167
return ['object', substr($docType, 1)];
164168
}
165169
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.