@@ -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
@@ -76,6 +88,26 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
76
88
static method used. You can also resolve types from a string (as shown in the
77
89
``bool `` parameter of the previous example)
78
90
79
- .. note ::
91
+ PHPDoc parsing
92
+ ~~~~~~~~~~~~~~
80
93
81
- To support raw string resolving, you need to install ``phpstan/phpdoc-parser `` package.
94
+ But most times you won't have clean typed properties or you want a more precise type
95
+ thank to advanced PHPDoc, to do that you would want a string resolver based on that PHPDoc.
96
+ First you will require ``phpstan/phpdoc-parser `` package from composer to support string
97
+ revolving. Then you would do as following::
98
+
99
+ use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
100
+
101
+ class Dummy
102
+ {
103
+ public function __construct(
104
+ public int $id,
105
+ /** @var string[] $tags */
106
+ public array $tags,
107
+ ) {
108
+ }
109
+ }
110
+
111
+ $typeResolver = TypeResolver::create();
112
+ $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type
113
+ $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns a collection with "int" as key and "string" as values Type
0 commit comments