Skip to content

Commit 9e7c1cb

Browse files
Fix PseudoType support
1 parent 1083e8a commit 9e7c1cb

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public static function constructorTypesProvider()
428428
/**
429429
* @dataProvider pseudoTypesProvider
430430
*/
431-
public function testPseudoTypes($property, array $type)
431+
public function testPseudoTypes($property, ?array $type)
432432
{
433433
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\PseudoTypesDummy', $property));
434434
}
@@ -445,6 +445,16 @@ public static function pseudoTypesProvider(): array
445445
['numericString', [new Type(Type::BUILTIN_TYPE_STRING, false, null)]],
446446
['traitString', [new Type(Type::BUILTIN_TYPE_STRING, false, null)]],
447447
['positiveInt', [new Type(Type::BUILTIN_TYPE_INT, false, null)]],
448+
['true', [new Type(Type::BUILTIN_TYPE_TRUE, false, null)]],
449+
['false', [new Type(Type::BUILTIN_TYPE_FALSE, false, null)]],
450+
['valueOfStrings', null],
451+
['valueOfIntegers', null],
452+
['keyOfStrings', null],
453+
['keyOfIntegers', null],
454+
['arrayKey', null],
455+
['intMask', [new Type(Type::BUILTIN_TYPE_INT, false, null)]],
456+
['conditional', null],
457+
['offsetAccess', null],
448458
];
449459
}
450460

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)