Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3631834

Browse filesBrowse files
committed
minor #19554 [TypeInfo] Add documentation (Korbeil)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [TypeInfo] Add documentation | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes (symfony/symfony#52510) | Applies to | 7.1 | Fixed tickets | #19497 Commits ------- b6ffad3 [TypeInfo] Add documentation
2 parents 0c6dc92 + b6ffad3 commit 3631834
Copy full SHA for 3631834

File tree

1 file changed

+71
-0
lines changed
Filter options

1 file changed

+71
-0
lines changed

‎components/type_info.rst

Copy file name to clipboard
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
.. caution::
11+
12+
This component is :doc:`experimental </contributing/code/experimental>` and could be changed at any time
13+
without prior notice.
14+
15+
Installation
16+
------------
17+
18+
.. code-block:: terminal
19+
20+
$ composer require symfony/type-info
21+
22+
.. include:: /components/require_autoload.rst.inc
23+
24+
Usage
25+
-----
26+
27+
This component will gives you a :class:`Symfony\\Component\\TypeInfo\\Type` object that represents
28+
the PHP type of whatever you builded or asked to resolve.
29+
30+
There are two ways to use this component. First one is to create a type manually thanks
31+
to :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
32+
33+
use Symfony\Component\TypeInfo\Type;
34+
35+
Type::int();
36+
Type::nullable(Type::string());
37+
Type::generic(Type::object(Collection::class), Type::int());
38+
Type::list(Type::bool());
39+
Type::intersection(Type::object(\Stringable::class), Type::object(\Iterator::class));
40+
41+
// Many others are available and can be
42+
// found in Symfony\Component\TypeInfo\TypeFactoryTrait
43+
44+
45+
Second way to use TypeInfo is to resolve a type based on reflection or a simple string::
46+
47+
use Symfony\Component\TypeInfo\Type;
48+
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
49+
50+
// Instantiate a new resolver
51+
$typeResolver = TypeResolver::create();
52+
53+
// Then resolve types for any subject
54+
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance
55+
$typeResolver->resolve('bool'); // returns a "bool" Type instance
56+
57+
// Types can be instantiated thanks to static factories
58+
$type = Type::list(Type::nullable(Type::bool()));
59+
60+
// Type instances have several helper methods
61+
$type->getBaseType() // returns an "array" Type instance
62+
$type->getCollectionKeyType(); // returns an "int" Type instance
63+
$type->getCollectionValueType()->isNullable(); // returns true
64+
65+
Each of this rows will return you a Type instance that will corresponds to whatever static method you used to build it.
66+
We also can resolve a type from a string like we can see in this example with the `'bool'` parameter it is mostly
67+
designed that way so we can give TypeInfo a string from whatever was extracted from existing phpDoc within PropertyInfo.
68+
69+
.. note::
70+
71+
To support raw string resolving, you need to install ``phpstan/phpdoc-parser`` package.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.