Skip to content

Commit

Permalink
Add TypeInfo documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Feb 14, 2024
1 parent c42fcb6 commit d4d6b78
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions components/type_info.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
The TypeInfo Component
======================

The TypeInfo component extracts PHP types information. It aims to:

- Have a powerful Type definition that can handle union, intersections, and generics (and could be even more extended)

- Being able to get types from anything, such as properties, method arguments, return types, and raw strings (and can also be extended).


Installation
------------

.. code-block:: terminal
$ composer require symfony/type-info
.. include:: /components/require_autoload.rst.inc

Usage
-----

There is two ways to use this component. First one is to create a manually thanks
to :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::

use Symfony\Component\TypeInfo\Type;

Check failure on line 26 in components/type_info.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\TypeInfo\Type" does not exist

Type::int();
Type::nullable(Type::string());
Type::generic(Type::object(Collection::class), Type::int());
Type::list(Type::bool());
Type::intersection(Type::object(\Stringable::class), Type::object(\Iterator::class));

// Many others are available and can be
// found in Symfony\Component\TypeInfo\TypeFactoryTrait

Second way to use TypeInfo is to resolve a type based on reflection or a simple string::

use Symfony\Component\TypeInfo\Type;

Check failure on line 39 in components/type_info.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\TypeInfo\Type" does not exist
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;

Check failure on line 40 in components/type_info.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\TypeInfo\TypeResolver\TypeResolver" does not exist

// Instantiate a new resolver
$typeResolver = TypeResolver::create();

// Then resolve types for any subject
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance
$typeResolver->resolve('bool'); // returns a "bool" Type instance

// Types can be instantiated thanks to static factories
$type = Type::list(Type::nullable(Type::bool()));

// Type instances have several helper methods
$type->getBaseType() // returns an "array" Type instance
$type->getCollectionKeyType(); // returns an "int" Type instance

Check failure on line 54 in components/type_info.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected T_VARIABLE
$type->getCollectionValueType()->isNullable(); // returns true

.. note::

To support raw string resolving, you need to install ``phpstan/phpdoc-parser`` package.

0 comments on commit d4d6b78

Please sign in to comment.