Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e9ac79

Browse files
committed
Add TypeInfo documentation
1 parent c42fcb6 commit 0e9ac79

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

components/type_info.rst

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
The TypeInfo Component
2+
======================
3+
4+
The TypeInfo component extracts PHP types information. It aims to:
5+
6+
- Have a powerful Type definition that can handle union, intersections, and generics (and could be even more extended)
7+
8+
- Being able to get types from anything, such as properties, method arguments, return types, and raw strings (and can also be extended).
9+
10+
11+
Installation
12+
------------
13+
14+
.. code-block:: terminal
15+
16+
$ composer require symfony/type-info
17+
18+
.. include:: /components/require_autoload.rst.inc
19+
20+
Usage
21+
-----
22+
23+
There is two ways to use this component. First one is to create a manually thanks
24+
to :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
25+
26+
<?php
27+
28+
use Symfony\Component\TypeInfo\Type;
29+
30+
Type::int();
31+
Type::nullable(Type::string());
32+
Type::generic(Type::object(Collection::class), Type::int());
33+
Type::list(Type::bool());
34+
Type::intersection(Type::object(\Stringable::class), Type::object(\Iterator::class));
35+
36+
// Many others are available and can be
37+
// found in Symfony\Component\TypeInfo\TypeFactoryTrait
38+
39+
Second way to use TypeInfo is to resolve a type based on reflection or a simple string::
40+
41+
<?php
42+
43+
use Symfony\Component\TypeInfo\Type;
44+
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
45+
46+
// Instantiate a new resolver
47+
$typeResolver = TypeResolver::create();
48+
49+
// Then resolve types for any subject
50+
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance
51+
$typeResolver->resolve('bool'); // returns a "bool" Type instance
52+
53+
// Types can be instantiated thanks to static factories
54+
$type = Type::list(Type::nullable(Type::bool()));
55+
56+
// Type instances have several helper methods
57+
$type->getBaseType() // returns an "array" Type instance
58+
$type->getCollectionKeyType(); // returns an "int" Type instance
59+
$type->getCollectionValueType()->isNullable(); // returns true
60+
61+
.. note::
62+
63+
To support raw string resolving, you need to install ``phpstan/phpdoc-parser`` package.

0 commit comments

Comments
 (0)