@@ -40,12 +40,24 @@ to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
40
40
// Many others are available and can be
41
41
// found in Symfony\Component\TypeInfo\TypeFactoryTrait
42
42
43
+ Resolvers
44
+ ~~~~~~~~~
45
+
43
46
The second way of using the component is to use ``TypeInfo `` to resolve a type
44
- based on reflection or a simple string::
47
+ based on reflection or a simple string, this is aimed towards libraries that wants to
48
+ describe a class or anything that has a type easily::
45
49
46
50
use Symfony\Component\TypeInfo\Type;
47
51
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
48
52
53
+ class Dummy
54
+ {
55
+ public function __construct(
56
+ public int $id,
57
+ ) {
58
+ }
59
+ }
60
+
49
61
// Instantiate a new resolver
50
62
$typeResolver = TypeResolver::create();
51
63
@@ -70,6 +82,88 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
70
82
static method used. You can also resolve types from a string (as shown in the
71
83
``bool `` parameter of the previous example)
72
84
73
- .. note ::
85
+ PHPDoc parsing
86
+ ~~~~~~~~~~~~~~
74
87
75
- To support raw string resolving, you need to install ``phpstan/phpdoc-parser `` package.
88
+ But most times you won't have clean typed properties or you want a more precise type
89
+ thank to advanced PHPDoc, to do that you would want a string resolver based on that PHPDoc.
90
+ First you will require ``phpstan/phpdoc-parser `` package from composer to support string
91
+ revolving. Then you would do as following::
92
+
93
+ use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
94
+
95
+ class Dummy
96
+ {
97
+ public function __construct(
98
+ public int $id,
99
+ /** @var string[] $tags */
100
+ public array $tags,
101
+ ) {
102
+ }
103
+ }
104
+
105
+ $typeResolver = TypeResolver::create();
106
+ $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type
107
+ $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns a collection with "int" as key and "string" as values Type
108
+
109
+ Advanced usages
110
+ ~~~~~~~~~~~~~~~
111
+
112
+ There is many methods to manipulate and check types depending on your needs within the TypeInfo components.
113
+
114
+ If you need a check a simple Type::
115
+
116
+ // You need to check if a Type
117
+ $type = Type::int(); // with a simple int type
118
+ // You can check if a given type comply with a given identifier
119
+ $type->isIdentifiedBy(TypeIdentifier::INT); // true
120
+ $type->isIdentifiedBy(TypeIdentifier::STRING); // false
121
+
122
+ $type = Type::union(Type::string(), Type::int()); // with an union of int and string types
123
+ // You can now see that the second check will pass to true since we have an union with a string type
124
+ $type->isIdentifiedBy(TypeIdentifier::INT); // true
125
+ $type->isIdentifiedBy(TypeIdentifier::STRING); // true
126
+
127
+ class DummyParent {}
128
+ class Dummy extends DummyParent implements DummyInterface {}
129
+ $type = Type::object(Dummy::class); // with an object Type
130
+ // You can check is the Type is an object, or even if it's a given class
131
+ $type->isIdentifiedBy(TypeIdentifier::OBJECT); // true
132
+ $type->isIdentifiedBy(Dummy::class); // true
133
+ // Or inherits/implements something
134
+ $type->isIdentifiedBy(DummyParent::class); // true
135
+ $type->isIdentifiedBy(DummyInterface::class); // true
136
+
137
+ Sometimes you want to check for more than one thing at a time so a callable may be better to check everything::
138
+
139
+ class Foo
140
+ {
141
+ private int $integer;
142
+ private string $string;
143
+ private ?float $float;
144
+ }
145
+
146
+ $reflClass = new \ReflectionClass(Foo::class);
147
+
148
+ $resolver = TypeResolver::create();
149
+ $integerType = $resolver->resolve($reflClass->getProperty('integer'));
150
+ $stringType = $resolver->resolve($reflClass->getProperty('string'));
151
+ $floatType = $resolver->resolve($reflClass->getProperty('float'));
152
+
153
+ // your callable to check whatever you need
154
+ // here we want to validate a given type is a non nullable number
155
+ $isNonNullableNumber = function (Type $type): bool {
156
+ if ($type->isNullable()) {
157
+ return false;
158
+ }
159
+
160
+ if ($type->isIdentifiedBy(TypeIdentifier::INT) || $type->isIdentifiedBy(TypeIdentifier::FLOAT)) {
161
+ return true;
162
+ }
163
+
164
+ return false;
165
+ };
166
+
167
+ $integerType->isSatisfiedBy($isNonNullableNumber); // true
168
+ $stringType->isSatisfiedBy($isNonNullableNumber); // false
169
+ $floatType->isSatisfiedBy($isNonNullableNumber); // false
0 commit comments