Skip to content

Commit ead41a7

Browse files
bug #62686 [PropertyInfo] Fix PseudoType support in PhpDocTypeHelper (VincentLanglet)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [PropertyInfo] Fix PseudoType support in PhpDocTypeHelper | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62636 | License | MIT The PR on 7.3 is #62684 I'm not sure how you want to deal with the merges since there will be conflict when merging up 6.4 into 7.3 so I opened both PR to show the changes Commits ------- e2b7844 [PropertyInfo] Fix PseudoType support in PhpDocTypeHelper
2 parents 169a250 + e2b7844 commit ead41a7

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\PropertyInfo\Tests\Extractor;
1313

14+
use phpDocumentor\Reflection\PseudoTypes\IntMask;
15+
use phpDocumentor\Reflection\PseudoTypes\IntMaskOf;
1416
use PHPUnit\Framework\TestCase;
1517
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1618
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
@@ -428,7 +430,7 @@ public static function constructorTypesProvider()
428430
/**
429431
* @dataProvider pseudoTypesProvider
430432
*/
431-
public function testPseudoTypes($property, array $type)
433+
public function testPseudoTypes($property, ?array $type)
432434
{
433435
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\PseudoTypesDummy', $property));
434436
}
@@ -445,6 +447,17 @@ public static function pseudoTypesProvider(): array
445447
['numericString', [new Type(Type::BUILTIN_TYPE_STRING, false, null)]],
446448
['traitString', [new Type(Type::BUILTIN_TYPE_STRING, false, null)]],
447449
['positiveInt', [new Type(Type::BUILTIN_TYPE_INT, false, null)]],
450+
['true', [new Type(Type::BUILTIN_TYPE_TRUE, false, null)]],
451+
['false', [new Type(Type::BUILTIN_TYPE_FALSE, false, null)]],
452+
['valueOfStrings', null],
453+
['valueOfIntegers', null],
454+
['keyOfStrings', null],
455+
['keyOfIntegers', null],
456+
['arrayKey', null],
457+
['intMask', class_exists(IntMask::class) ? [new Type(Type::BUILTIN_TYPE_INT, false, null)] : null],
458+
['intMaskOf', class_exists(IntMaskOf::class) ? [new Type(Type::BUILTIN_TYPE_INT, false, null)] : null],
459+
['conditional', null],
460+
['offsetAccess', null],
448461
];
449462
}
450463

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,37 @@ class PseudoTypesDummy
4545

4646
/** @var literal-string */
4747
public $literalString;
48+
49+
/** @var true */
50+
public $true;
51+
52+
/** @var false */
53+
public $false;
54+
55+
/** @var value-of<self::STRINGS> */
56+
public $valueOfStrings;
57+
58+
/** @var value-of<self::INTEGERS> */
59+
public $valueOfIntegers;
60+
61+
/** @var key-of<self::STRINGS> */
62+
public $keyOfStrings;
63+
64+
/** @var key-of<self::INTEGERS> */
65+
public $keyOfIntegers;
66+
67+
/** @var array-key */
68+
public $arrayKey;
69+
70+
/** @var int-mask<1,2,4> */
71+
public $intMask;
72+
73+
/** @var int-mask-of<1|2|4> */
74+
public $intMaskOf;
75+
76+
/** @var (T is int ? string : int) */
77+
public $conditional;
78+
79+
/** @var self::STRINGS['A'] */
80+
public $offsetAccess;
4881
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,28 @@ private function createType(DocType $type, bool $nullable): ?Type
156156
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyTypes, $collectionValueTypes);
157157
}
158158

159+
$docType = $this->normalizeType($docType);
160+
[$phpType, $class] = $this->getPhpTypeAndClass($docType);
161+
162+
if ('array' === $docType) {
163+
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, null, null);
164+
}
165+
166+
if (null === $class) {
167+
return new Type($phpType, $nullable, $class);
168+
}
169+
159170
if ($type instanceof PseudoType) {
160171
if ($type->underlyingType() instanceof Integer) {
161172
return new Type(Type::BUILTIN_TYPE_INT, $nullable, null);
162173
} elseif ($type->underlyingType() instanceof String_) {
163174
return new Type(Type::BUILTIN_TYPE_STRING, $nullable, null);
175+
} else {
176+
// It's safer to fall back to other extractors here, as resolving pseudo types correctly is not easy at the moment
177+
return null;
164178
}
165179
}
166180

167-
$docType = $this->normalizeType($docType);
168-
[$phpType, $class] = $this->getPhpTypeAndClass($docType);
169-
170-
if ('array' === $docType) {
171-
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, null, null);
172-
}
173-
174181
return new Type($phpType, $nullable, $class);
175182
}
176183

0 commit comments

Comments
 (0)