@@ -43,9 +43,9 @@ to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
43
43
Resolvers
44
44
~~~~~~~~~
45
45
46
- The second way of using the component is to use ``TypeInfo `` to resolve a type
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 ::
46
+ The second way to use the component is by using ``TypeInfo `` to resolve a type
47
+ based on reflection or a simple string. This approach is designed for libraries
48
+ that need a simple way to describe a class or anything with a type::
49
49
50
50
use Symfony\Component\TypeInfo\Type;
51
51
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
@@ -82,13 +82,15 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
82
82
static method used. You can also resolve types from a string (as shown in the
83
83
``bool `` parameter of the previous example)
84
84
85
- PHPDoc parsing
85
+ PHPDoc Parsing
86
86
~~~~~~~~~~~~~~
87
87
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::
88
+ In many cases, you may not have cleanly typed properties or may need more precise
89
+ type definitions provided by advanced PHPDoc. To achieve this, you can use a string
90
+ resolver based on the PHPDoc annotations.
91
+
92
+ First, run the command ``composer require phpstan/phpdoc-parser `` to install the
93
+ PHP package required for string resolving. Then, follow these steps::
92
94
93
95
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
94
96
@@ -106,35 +108,40 @@ revolving. Then you would do as following::
106
108
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type
107
109
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns a collection with "int" as key and "string" as values Type
108
110
109
- Advanced usages
111
+ Advanced Usages
110
112
~~~~~~~~~~~~~~~
111
113
112
- There is many methods to manipulate and check types depending on your needs within the TypeInfo components.
114
+ The TypeInfo component provides various methods to manipulate and check types,
115
+ depending on your needs.
113
116
114
- If you need a check a simple Type ::
117
+ Checking a ** simple type ** ::
115
118
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
119
+ // define a simple integer type
120
+ $type = Type::int();
121
+ // check if the type matches a specific identifier
122
+ $type->isIdentifiedBy(TypeIdentifier::INT); // true
120
123
$type->isIdentifiedBy(TypeIdentifier::STRING); // false
121
124
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
+ // define a union type (equivalent to PHP's int|string)
126
+ $type = Type::union(Type::string(), Type::int());
127
+ // now the second check is true because the union type contains the string type
128
+ $type->isIdentifiedBy(TypeIdentifier::INT); // true
125
129
$type->isIdentifiedBy(TypeIdentifier::STRING); // true
126
130
127
131
class DummyParent {}
128
132
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
133
+
134
+ // define an object type
135
+ $type = Type::object(Dummy::class);
136
+
137
+ // check if the type is an object or matches a specific class
131
138
$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
139
+ $type->isIdentifiedBy(Dummy::class); // true
140
+ // check if it inherits/implements something
141
+ $type->isIdentifiedBy(DummyParent::class); // true
142
+ $type->isIdentifiedBy(DummyInterface::class); // true
136
143
137
- Sometimes you want to check for more than one thing at a time so a callable may be better to check everything: :
144
+ Using callables for ** complex checks ** :
138
145
139
146
class Foo
140
147
{
@@ -150,8 +157,7 @@ Sometimes you want to check for more than one thing at a time so a callable may
150
157
$stringType = $resolver->resolve($reflClass->getProperty('string'));
151
158
$floatType = $resolver->resolve($reflClass->getProperty('float'));
152
159
153
- // your callable to check whatever you need
154
- // here we want to validate a given type is a non nullable number
160
+ // define a callable to validate non-nullable number types
155
161
$isNonNullableNumber = function (Type $type): bool {
156
162
if ($type->isNullable()) {
157
163
return false;
@@ -165,5 +171,5 @@ Sometimes you want to check for more than one thing at a time so a callable may
165
171
};
166
172
167
173
$integerType->isSatisfiedBy($isNonNullableNumber); // true
168
- $stringType->isSatisfiedBy($isNonNullableNumber); // false
169
- $floatType->isSatisfiedBy($isNonNullableNumber); // false
174
+ $stringType->isSatisfiedBy($isNonNullableNumber); // false
175
+ $floatType->isSatisfiedBy($isNonNullableNumber); // false
0 commit comments