@@ -15,6 +15,7 @@ composer require phpstan/phpdoc-parser # to support raw string resolving
15
15
<?php
16
16
17
17
use Symfony\Component\TypeInfo\Type;
18
+ use Symfony\Component\TypeInfo\TypeIdentifier;
18
19
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
19
20
20
21
// Instantiate a new resolver
@@ -27,10 +28,21 @@ $typeResolver->resolve('bool'); // returns a "bool" Type instance
27
28
// Types can be instantiated thanks to static factories
28
29
$type = Type::list(Type::nullable(Type::bool()));
29
30
30
- // Type instances have several helper methods
31
- $type->getBaseType(); // returns an "array" Type instance
32
- $type->getCollectionKeyType(); // returns an "int" Type instance
33
- $type->getCollectionValueType()->isNullable(); // returns true
31
+ // Type classes have their specific methods
32
+ Type::object(FooClass::class)->getClassName();
33
+ Type::enum(FooEnum::class, Type::int())->getBackingType();
34
+ Type::list(Type::int())->isList();
35
+
36
+ // Every type can be cast to string
37
+ (string) Type::generic(Type::object(Collection::class), Type::int()) // returns "Collection<int >"
38
+
39
+ // You can check that a type (or one of its wrapped/composed parts) is identified by one of some identifiers.
40
+ $type->isIdentifiedBy(Foo::class, Bar::class);
41
+ $type->isIdentifiedBy(TypeIdentifier::OBJECT);
42
+ $type->isIdentifiedBy('float');
43
+
44
+ // You can also check that a type satifies specific conditions
45
+ $type->isSatisfiedBy(fn (Type $type): bool => !$type->isNullable() && $type->isIdentifiedBy(TypeIdentifier::INT));
34
46
```
35
47
36
48
Resources
0 commit comments