|
| 1 | +The TypeInfo Component |
| 2 | +====================== |
| 3 | + |
| 4 | +The TypeInfo component extracts type information from PHP elements like properties, |
| 5 | +arguments and return types. |
| 6 | + |
| 7 | +This component provides: |
| 8 | + |
| 9 | +* A powerful ``Type`` definition that can handle unions, intersections, and generics |
| 10 | + (and can be extended to support more types in the future); |
| 11 | +* A way to get types from PHP elements such as properties, method arguments, |
| 12 | + return types, and raw strings. |
| 13 | + |
| 14 | +.. caution:: |
| 15 | + |
| 16 | + This component is :doc:`experimental </contributing/code/experimental>` and |
| 17 | + could be changed at any time without prior notice. |
| 18 | + |
| 19 | +Installation |
| 20 | +------------ |
| 21 | + |
| 22 | +.. code-block:: terminal |
| 23 | +
|
| 24 | + $ composer require symfony/type-info |
| 25 | +
|
| 26 | +.. include:: /components/require_autoload.rst.inc |
| 27 | + |
| 28 | +Usage |
| 29 | +----- |
| 30 | + |
| 31 | +This component gives you a :class:`Symfony\\Component\\TypeInfo\\Type` object that |
| 32 | +represents the PHP type of anything you built or asked to resolve. |
| 33 | + |
| 34 | +There are two ways to use this component. First one is to create a type manually thanks |
| 35 | +to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following:: |
| 36 | + |
| 37 | + use Symfony\Component\TypeInfo\Type; |
| 38 | + |
| 39 | + Type::int(); |
| 40 | + Type::nullable(Type::string()); |
| 41 | + Type::generic(Type::object(Collection::class), Type::int()); |
| 42 | + Type::list(Type::bool()); |
| 43 | + Type::intersection(Type::object(\Stringable::class), Type::object(\Iterator::class)); |
| 44 | + |
| 45 | + // Many others are available and can be |
| 46 | + // found in Symfony\Component\TypeInfo\TypeFactoryTrait |
| 47 | + |
| 48 | +The second way of using the component is to use ``TypeInfo`` to resolve a type |
| 49 | +based on reflection or a simple string:: |
| 50 | + |
| 51 | + use Symfony\Component\TypeInfo\Type; |
| 52 | + use Symfony\Component\TypeInfo\TypeResolver\TypeResolver; |
| 53 | + |
| 54 | + // Instantiate a new resolver |
| 55 | + $typeResolver = TypeResolver::create(); |
| 56 | + |
| 57 | + // Then resolve types for any subject |
| 58 | + $typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance |
| 59 | + $typeResolver->resolve('bool'); // returns a "bool" Type instance |
| 60 | + |
| 61 | + // Types can be instantiated thanks to static factories |
| 62 | + $type = Type::list(Type::nullable(Type::bool())); |
| 63 | + |
| 64 | + // Type instances have several helper methods |
| 65 | + $type->getBaseType() // returns an "array" Type instance |
| 66 | + $type->getCollectionKeyType(); // returns an "int" Type instance |
| 67 | + $type->getCollectionValueType()->isNullable(); // returns true |
| 68 | + |
| 69 | +Each of this calls will return you a ``Type`` instance that corresponds to the |
| 70 | +static method used. You can also resolve types from a string (as shown in the |
| 71 | +``bool`` parameter of the previous example) |
| 72 | + |
| 73 | +.. note:: |
| 74 | + |
| 75 | + To support raw string resolving, you need to install ``phpstan/phpdoc-parser`` package. |
0 commit comments