8000 [Serializer][PropertyInfo][Validator] TypeInfo 7.2 compatibility · symfony/property-info@402ab84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 402ab84

Browse files
committed
[Serializer][PropertyInfo][Validator] TypeInfo 7.2 compatibility
1 parent 3748f85 commit 402ab84

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

Tests/Extractor/PhpDocExtractorTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
2828
use Symfony\Component\PropertyInfo\Type as LegacyType;
2929
use Symfony\Component\TypeInfo\Type;
30+
use Symfony\Component\TypeInfo\Type\NullableType;
3031

3132
/**
3233
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -562,7 +563,14 @@ public static function typeProvider(): iterable
562563
yield ['f', Type::list(Type::object(\DateTimeImmutable::class)), null, null];
563564
yield ['g', Type::nullable(Type::array()), 'Nullable array.', null];
564565
yield ['h', Type::nullable(Type::string()), null, null];
565-
yield ['i', Type::union(Type::int(), Type::string(), Type::null()), null, null];
566+
567+
// BC layer for type-info < 7.2
568+
if (!class_exists(NullableType::class)) {
569+
yield ['i', Type::union(Type::int(), Type::string(), Type::null()), null, null];
570+
} else {
571+
yield ['i', Type::nullable(Type::union(Type::int(), Type::string())), null, null];
572+
}
573+
566574
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class)), null, null];
567575
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::list(Type::int())), null, null];
568576
yield ['donotexist', null, null, null];
@@ -629,7 +637,14 @@ public static function typeWithNoPrefixesProvider()
629637
yield ['f', null];
630638
yield ['g', Type::nullable(Type::array())];
631639
yield ['h', Type::nullable(Type::string())];
632-
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
640+
641+
// BC layer for type-info < 7.2
642+
if (!class_exists(NullableType::class)) {
643+
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
644+
} else {
645+
yield ['i', Type::nullable(Type::union(Type::int(), Type::string()))];
646+
}
647+
633648
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))];
634649
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::list(Type::int()))];
635650
yield ['donotexist', null];
@@ -693,7 +708,14 @@ public static function typeWithCustomPrefixesProvider(): iterable
693708
yield ['f', Type::list(Type::object(\DateTimeImmutable::class))];
694709
yield ['g', Type::nullable(Type::array())];
695710
yield ['h', Type::nullable(Type::string())];
696-
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
711+
712+
// BC layer for type-info < 7.2
713+
if (!class_exists(NullableType::class)) {
714+
yield ['i', Type::union(Type::int(), Type::string(), Type::null())];
715+
} else {
716+
yield ['i', Type::nullable(Type::union(Type::int(), Type::string()))];
717+
}
718+
697719
yield ['j', Type::nullable(Type::object(\DateTimeImmutable::class))];
698720
yield ['nullableCollectionOfNonNullableElements', Type::nullable(Type::list(Type::int()))];
699721
yield ['nonNullableCollectionOfNullableElements', Type::list(Type::nullable(Type::int()))];

Tests/Extractor/PhpStanExtractorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\PropertyInfo\Type as LegacyType;
3737
use Symfony\Component\TypeInfo\Exception\LogicException;
3838
use Symfony\Component\TypeInfo\Type;
39+
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
3940

4041
require_once __DIR__.'/../Fixtures/Extractor/DummyNamespace.php';
4142

@@ -869,7 +870,14 @@ public function testPseudoTypes(string $property, ?Type $type)
869870
public static function pseudoTypesProvider(): iterable
870871
{
871872
yield ['classString', Type::string()];
872-
yield ['classStringGeneric', Type::generic(Type::string(), Type::object(\stdClass::class))];
873+
874+
// BC layer for type-info < 7.2
875+
if (!interface_exists(WrappingTypeInterface::class)) {
876+
yield ['classStringGeneric', Type::generic(Type::string(), Type::object(\stdClass::class))];
877+
} else {
878+
yield ['classStringGeneric', Type::string()];
879+
}
880+
873881
yield ['htmlEscapedString', Type::string()];
874882
yield ['lowercaseString', Type::string()];
875883
yield ['nonEmptyLowercaseString', Type::string()];

Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\Component\PropertyInfo\Tests\Fixtures\SnakeCaseDummy;
3434
use Symfony\Component\PropertyInfo\Type as LegacyType;
3535
use Symfony\Component\TypeInfo\Type;
36+
use Symfony\Component\TypeInfo\Type\NullableType;
3637
use Symfony\Component\TypeInfo\TypeResolver\PhpDocAwareReflectionTypeResolver;
3738

3839
/**
@@ -772,7 +773,14 @@ public static function php80TypesProvider(): iterable
772773
yield ['foo', Type::nullable(Type::array())];
773774
yield ['bar', Type::nullable(Type::int())];
774775
yield ['timeout', Type::union(Type::int(), Type::float())];
775-
yield ['optional', Type::union(Type::nullable(Type::int()), Type::nullable(Type::float()))];
776+
777+
// BC layer for type-info < 7.2
778+
if (!class_exists(NullableType::class)) {
779+
yield ['optional', Type::union(Type::nullable(Type::int()), Type::nullable(Type::float()))];
780+
} else {
781+
yield ['optional', Type::nullable(Type::union(Type::float(), Type::int()))];
782+
}
783+
776784
yield ['string', Type::union(Type::string(), Type::object(\Stringable::class))];
777785
yield ['payload', Type::mixed()];
778786
yield ['data', Type::mixed()];

Util/PhpDocTypeHelper.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ public function getType(DocType $varType): ?Type
128128
$nullable = true;
129129
}
130130

131-
return $this->createType($varType, $nullable);
131+
$type = $this->createType($varType);
132+
133+
return $nullable ? Type::nullable($type) : $type;
132134
}
133135

134136
$varTypes = [];
@@ -156,8 +158,7 @@ public function getType(DocType $varType): ?Type
156158

157159
$unionTypes = [];
158160
foreach ($varTypes as $varType) {
159-
$t = $this->createType($varType, $nullable);
160-
if (null !== $t) {
161+
if (null !== $t = $this->createType($varType)) {
161162
$unionTypes[] = $t;
162163
}
163164
}
@@ -238,7 +239,7 @@ private function createLegacyType(DocType $type, bool $nullable): ?LegacyType
238239
/**
239240
* Creates a {@see Type} from a PHPDoc type.
240241
*/
241-
private function createType(DocType $docType, bool $nullable): ?Type
242+
private function createType(DocType $docType): ?Type
242243
{
243244
$docTypeString = (string) $docType;
244245

@@ -262,9 +263,8 @@ private function createType(DocType $docType, bool $nullable): ?Type
262263
}
263264

264265
$type = null !== $class ? Type::object($class) : Type::builtin($phpType);
265-
$type = Type::collection($type, ...$variableTypes);
266266

267-
return $nullable ? Type::nullable($type) : $type;
267+
return Type::collection($type, ...$variableTypes);
268268
}
269269

270270
if (!$docTypeString) {
@@ -277,9 +277,8 @@ private function createType(DocType $docType, bool $nullable): ?Type
277277

278278
if (str_starts_with($docTypeString, 'list<') && $docType instanceof Array_) {
279279
$collectionValueType = $this->getType($docType->getValueType());
280-
$type = Type::list($collectionValueType);
281280

282-
return $nullable ? Type::nullable($type) : $type;
281+
return Type::list($collectionValueType);
283282
}
284283

285284
if (str_starts_with($docTypeString, 'array<') && $docType instanceof Array_) {
@@ -288,16 +287,14 @@ private function createType(DocType $docType, bool $nullable): ?Type
288287
$collectionKeyType = $this->getType($docType->getKeyType());
289288
$collectionValueType = $this->getType($docType->getValueType());
290289

291-
$type = Type::array($collectionValueType, $collectionKeyType);
292-
293-
return $nullable ? Type::nullable($type) : $type;
290+
return Type::array($collectionValueType, $collectionKeyType);
294291
}
295292

296293
if ($docType instanceof PseudoType) {
297294
if ($docType->underlyingType() instanceof Integer) {
298-
return $nullable ? Type::nullable(Type::int()) : Type::int();
295+
return Type::int();
299296
} elseif ($docType->underlyingType() instanceof String_) {
300-
return $nullable ? Type::nullable(Type::string()) : Type::string();
297+
return Type::string();
301298
}
302299
}
303300

@@ -314,12 +311,10 @@ private function createType(DocType $docType, bool $nullable): ?Type
314311
[$phpType, $class] = $this->getPhpTypeAndClass($docTypeString);
315312

316313
if ('array' === $docTypeString) {
317-
return $nullable ? Type::nullable(Type::array()) : Type::array();
314+
return Type::array();
318315
}
319316

320-
$type = null !== $class ? Type::object($class) : Type::builtin($phpType);
321-
322-
return $nullable ? Type::nullable($type) : $type;
317+
return null !== $class ? Type::object($class) : Type::builtin($phpType);
323318
}
324319

325320
private function normalizeType(string $docType): string

0 commit comments

Comments
 (0)
0