Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a170c98

Browse files
committed
[Serializer][PropertyAccessor] Allow to ignore generics
1 parent b85a083 commit a170c98

15 files changed

+145
-30
lines changed

UPGRADE-6.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ MonologBridge
172172

173173
* [BC break] Add native return type to `Logger::clear()` and to `DebugProcessor::clear()`
174174

175+
PropertyInfo
176+
------------
177+
178+
* [BC Break] `PhpDocExtractor` and `PhpStanExtractor` no longer support the `ignore_docblock_generics` context value
179+
175180
PsrHttpMessageBridge
176181
--------------------
177182

src/Symfony/Component/PropertyInfo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Make properties writable when a setter in camelCase exists, similar to the camelCase getter
8+
* Allow `PhpDocExtractor` and `PhpStanExtractor` to ignore non-collection generics using `ignore_docblock_generics` context value
89

910
6.1
1011
---

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ interface ConstructorArgumentTypeExtractorInterface
2929
*
3030
* @internal
3131
*/
32-
public function getTypesFromConstructor(string $class, string $property): ?array;
32+
public function getTypesFromConstructor(string $class, string $property, array $context = []): ?array;
3333
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(
3131
public function getTypes(string $class, string $property, array $context = []): ?array
3232
{
3333
foreach ($this->extractors as $extractor) {
34-
$value = $extractor->getTypesFromConstructor($class, $property);
34+
$value = $extractor->getTypesFromConstructor($class, $property, $context);
3535
if (null !== $value) {
3636
return $value;
3737
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public function getLongDescription(string $class, string $property, array $conte
113113

114114
public function getTypes(string $class, string $property, array $context = []): ?array
115115
{
116+
if (true === $ignoreDocblockGenerics = ($context['ignore_docblock_generics'] ?? false)) {
117+
trigger_deprecation('symfony/property-info', '6.4', 'Ignoring docblock generics by setting "ignore_docblock_generics" context value to "true" is deprecated without replacement.');
118+
}
119+
116120
/** @var $docBlock DocBlock */
117121
[$docBlock, $source, $prefix] = $this->getDocBlock($class, $property);
118122
if (!$docBlock) {
@@ -130,7 +134,7 @@ public function getTypes(string $class, string $property, array $context = []):
130134
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
131135
foreach ($docBlock->getTagsByName($tag) as $tag) {
132136
if ($tag && !$tag instanceof InvalidTag && null !== $tag->getType()) {
133-
foreach ($this->phpDocTypeHelper->getTypes($tag->getType()) as $type) {
137+
foreach ($this->phpDocTypeHelper->getTypes($tag->getType(), $ignoreDocblockGenerics) as $type) {
134138
switch ($type->getClassName()) {
135139
case 'self':
136140
case 'static':
@@ -164,8 +168,12 @@ public function getTypes(string $class, string $property, array $context = []):
164168
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $types[0])];
165169
}
166170

167-
public function getTypesFromConstructor(string $class, string $property): ?array
171+
public function getTypesFromConstructor(string $class, string $property, array $context = []): ?array
168172
{
173+
if (true === $ignoreDocblockGenerics = ($context['ignore_docblock_generics'] ?? false)) {
174+
trigger_deprecation('symfony/property-info', '6.4', 'Ignoring docblock generics by setting "ignore_docblock_generics" context value to "true" is deprecated without replacement.');
175+
}
176+
169177
$docBlock = $this->getDocBlockFromConstructor($class, $property);
170178

171179
if (!$docBlock) {
@@ -176,7 +184,7 @@ public function getTypesFromConstructor(string $class, string $property): ?array
176184
/** @var DocBlock\Tags\Var_|DocBlock\Tags\Return_|DocBlock\Tags\Param $tag */
177185
foreach ($docBlock->getTagsByName('param') as $tag) {
178186
if ($tag && null !== $tag->getType()) {
179-
$types[] = $this->phpDocTypeHelper->getTypes($tag->getType());
187+
$types[] = $this->phpDocTypeHelper->getTypes($tag->getType(), $ignoreDocblockGenerics);
180188
}
181189
}
182190

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function __construct(array $mutatorPrefixes = null, array $accessorPrefix
7575

7676
public function getTypes(string $class, string $property, array $context = []): ?array
7777
{
78+
if (true === $ignoreDocblockGenerics = ($context['ignore_docblock_generics'] ?? false)) {
79+
trigger_deprecation('symfony/property-info', '6.4', 'Ignoring docblock generics by setting "ignore_docblock_generics" context value to "true" is deprecated without replacement.');
80+
}
81+
7882
/** @var PhpDocNode|null $docNode */
7983
[$docNode, $source, $prefix, $declaringClass] = $this->getDocBlock($class, $property);
8084
$nameScope = $this->nameScopeFactory->create($class, $declaringClass);
@@ -111,7 +115,7 @@ public function getTypes(string $class, string $property, array $context = []):
111115
continue;
112116
}
113117

114-
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value, $nameScope) as $type) {
118+
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value, $nameScope, $ignoreDocblockGenerics) as $type) {
115119
switch ($type->getClassName()) {
116120
case 'self':
117121
case 'static':
@@ -144,14 +148,18 @@ public function getTypes(string $class, string $property, array $context = []):
144148
return [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $types[0])];
145149
}
146150

147-
public function getTypesFromConstructor(string $class, string $property): ?array
151+
public function getTypesFromConstructor(string $class, string $property, array $context = []): ?array
148152
{
153+
if (true === $ignoreDocblockGenerics = ($context['ignore_docblock_generics'] ?? false)) {
154+
trigger_deprecation('symfony/property-info', '6.4', 'Ignoring docblock generics by setting "ignore_docblock_generics" context value to "true" is deprecated without replacement.');
155+
}
156+
149157
if (null === $tagDocNode = $this->getDocBlockFromConstructor($class, $property)) {
150158
return null;
151159
}
152160

153161
$types = [];
154-
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode, $this->nameScopeFactory->create($class)) as $type) {
162+
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode, $this->nameScopeFactory->create($class), $ignoreDocblockGenerics) as $type) {
155163
$types[] = $type;
156164
}
157165

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function getTypes(string $class, string $property, array $context = []):
156156
return null;
157157
}
158158

159-
public function getTypesFromConstructor(string $class, string $property): ?array
159+
public function getTypesFromConstructor(string $class, string $property, array $context = []): ?array
160160
{
161161
try {
162162
$reflection = new \ReflectionClass($class);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,15 @@ public static function promotedPropertyProvider(): array
447447
['promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
448448
];
449449
}
450+
451+
/**
452+
* @group legacy
453+
*/
454+
public function testIgnoreGenerics()
455+
{
456+
$this->assertCount(1, $this->extractor->getTypes(PhpDocGenericsDocblock::class, 'foo'));
457+
$this->assertNull($this->extractor->getTypes(PhpDocGenericsDocblock::class, 'foo', ['ignore_docblock_generics' => true]));
458+
}
450459
}
451460

452461
class EmptyDocBlock
@@ -465,3 +474,9 @@ public function setOmittedType(array $omittedTagType)
465474
{
466475
}
467476
}
477+
478+
class PhpDocGenericsDocblock
479+
{
480+
/** @var \stcClass<int> */
481+
public $foo;
482+
}

src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpStanExtractorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,15 @@ public static function php80TypesProvider()
473473
[Php80PromotedDummy::class, 'promoted', null],
474474
];
475475
}
476+
477+
/**
478+
* @group legacy
479+
*/
480+
public function testIgnoreGenerics()
481+
{
482+
$this->assertCount(1, $this->extractor->getTypes(PhpDocGenericsDocblock::class, 'foo'));
483+
$this->assertNull($this->extractor->getTypes(PhpDocGenericsDocblock::class, 'foo', ['ignore_docblock_generics' => true]));
484+
}
476485
}
477486

478487
class PhpStanOmittedParamTagTypeDocBlock
@@ -486,3 +495,9 @@ public function setOmittedType(array $omittedTagType)
486495
{
487496
}
488497
}
498+
499+
class PhpStanGenericsDocblock
500+
{
501+
/** @var \stcClass<int> */
502+
public $foo;
503+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getTypes($class, $property, array $context = []): ?array
3939
return [new Type(Type::BUILTIN_TYPE_INT)];
4040
}
4141

42-
public function getTypesFromConstructor(string $class, string $property): ?array
42+
public function getTypesFromConstructor(string $class, string $property, array $context = []): ?array
4343
{
4444
return [new Type(Type::BUILTIN_TYPE_STRING)];
4545
}

0 commit comments

Comments
 (0)