Skip to content

Navigation Menu

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 0e9ac79

Browse filesBrowse files
committed
Add TypeInfo documentation
1 parent c42fcb6 commit 0e9ac79
Copy full SHA for 0e9ac79

File tree

1 file changed

+63
-0
lines changed
Filter options

1 file changed

+63
-0
lines changed

‎components/type_info.rst

Copy file name to clipboard
+63Lines changed: 63 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.