Skip to content

Commit 310fb60

Browse files
committed
Add more details to TypeInfo documentation
1 parent 41a647a commit 310fb60

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

components/type_info.rst

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
Resolvers
44+
~~~~~~~~~
45+
4346
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::
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,26 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
7082
static 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+
~~~~~~~~~~~~~~
7487

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

0 commit comments

Comments
 (0)