@@ -40,12 +40,24 @@ to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
4040 // Many others are available and can be
4141 // found in Symfony\Component\TypeInfo\TypeFactoryTrait
4242
43- The second way of using the component is to use ``TypeInfo `` to resolve a type
44- based on reflection or a simple string::
43+ Resolvers
44+ ~~~~~~~~~
45+
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::
4549
4650 use Symfony\Component\TypeInfo\Type;
4751 use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
4852
53+ class Dummy
54+ {
55+ public function __construct(
56+ public int $id,
57+ ) {
58+ }
59+ }
60+
4961 // Instantiate a new resolver
5062 $typeResolver = TypeResolver::create();
5163
@@ -70,6 +82,94 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
7082static method used. You can also resolve types from a string (as shown in the
7183``bool `` parameter of the previous example)
7284
73- .. note ::
85+ PHPDoc Parsing
86+ ~~~~~~~~~~~~~~
87+
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::
7494
75- To support raw string resolving, you need to install ``phpstan/phpdoc-parser `` package.
95+ use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
96+
97+ class Dummy
98+ {
99+ public function __construct(
100+ public int $id,
101+ /** @var string[] $tags */
102+ public array $tags,
103+ ) {
104+ }
105+ }
106+
107+ $typeResolver = TypeResolver::create();
108+ $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type
109+ $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns a collection with "int" as key and "string" as values Type
110+
111+ Advanced Usages
112+ ~~~~~~~~~~~~~~~
113+
114+ The TypeInfo component provides various methods to manipulate and check types,
115+ depending on your needs.
116+
117+ Checking a **simple type **::
118+
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
123+ $type->isIdentifiedBy(TypeIdentifier::STRING); // false
124+
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
129+ $type->isIdentifiedBy(TypeIdentifier::STRING); // true
130+
131+ class DummyParent {}
132+ class Dummy extends DummyParent implements DummyInterface {}
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
138+ $type->isIdentifiedBy(TypeIdentifier::OBJECT); // 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
143+
144+ Using callables for **complex checks **:
145+
146+ class Foo
147+ {
148+ private int $integer;
149+ private string $string;
150+ private ?float $float;
151+ }
152+
153+ $reflClass = new \R eflectionClass(Foo::class);
154+
155+ $resolver = TypeResolver::create();
156+ $integerType = $resolver->resolve($reflClass->getProperty('integer'));
157+ $stringType = $resolver->resolve($reflClass->getProperty('string'));
158+ $floatType = $resolver->resolve($reflClass->getProperty('float'));
159+
160+ // define a callable to validate non-nullable number types
161+ $isNonNullableNumber = function (Type $type): bool {
162+ if ($type->isNullable()) {
163+ return false;
164+ }
165+
166+ if ($type->isIdentifiedBy(TypeIdentifier::INT) || $type->isIdentifiedBy(TypeIdentifier::FLOAT)) {
167+ return true;
168+ }
169+
170+ return false;
171+ };
172+
173+ $integerType->isSatisfiedBy($isNonNullableNumber); // true
174+ $stringType->isSatisfiedBy($isNonNullableNumber); // false
175+ $floatType->isSatisfiedBy($isNonNullableNumber); // false
0 commit comments