Skip to content

Commit 49240c9

Browse files
committed
Add more details to TypeInfo documentation
1 parent da93c7b commit 49240c9

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

@@ -76,6 +88,26 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
7688
static method used. You can also resolve types from a string (as shown in the
7789
``bool`` parameter of the previous example)
7890

79-
.. note::
91+
PHPDoc parsing
92+
~~~~~~~~~~~~~~
8093

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

Comments
 (0)