Skip to content

Commit 5cf517d

Browse files
committed
minor #20407 [TypeInfo] Add more details to TypeInfo documentation (Korbeil)
This PR was merged into the 7.2 branch. Discussion ---------- [TypeInfo] Add more details to TypeInfo documentation This pull request is to revamp current TypeInfo documentation. I tried to make the most of the recent changes while answering `@javiereguiluz`'s issue (thanks to "PHPDoc parsing" part). It should fixes #20389, #20064 and #20014. Commits ------- dc999f3 Add more details to TypeInfo documentation
2 parents c51ca3b + dc999f3 commit 5cf517d

File tree

1 file changed

+97
-3
lines changed

1 file changed

+97
-3
lines changed

components/type_info.rst

+97-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
4040
// Many others are available and can be
4141
// found in Symfony\Component\TypeInfo\TypeFactoryTrait
4242

43+
Resolvers
44+
~~~~~~~~~
45+
4346
The second way of using the component is to use ``TypeInfo`` to resolve a type
44-
based on reflection or a simple string::
47+
based on reflection or a simple string, this is aimed towards libraries that wants to
48+
describe a class or anything that has a type easily::
4549

4650
use Symfony\Component\TypeInfo\Type;
4751
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
4852

53+
class Dummy
54+
{
55+
public function __construct(
56+
public int $id,
57+
) {
58+
}
59+
}
60+
4961
// Instantiate a new resolver
5062
$typeResolver = TypeResolver::create();
5163

@@ -70,6 +82,88 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
7082
static method used. You can also resolve types from a string (as shown in the
7183
``bool`` parameter of the previous example)
7284

73-
.. note::
85+
PHPDoc parsing
86+
~~~~~~~~~~~~~~
7487

75-
To support raw string resolving, you need to install ``phpstan/phpdoc-parser`` package.
88+
But most times you won't have clean typed properties or you want a more precise type
89+
thank to advanced PHPDoc, to do that you would want a string resolver based on that PHPDoc.
90+
First you will require ``phpstan/phpdoc-parser`` package from composer to support string
91+
revolving. Then you would do as following::
92+
93+
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
94+
95+
class Dummy
96+
{
97+
public function __construct(
98+
public int $id,
99+
/** @var string[] $tags */
100+
public array $tags,
101+
) {
102+
}
103+
}
104+
105+
$typeResolver = TypeResolver::create();
106+
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type
107+
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns a collection with "int" as key and "string" as values Type
108+
109+
Advanced usages
110+
~~~~~~~~~~~~~~~
111+
112+
There is many methods to manipulate and check types depending on your needs within the TypeInfo components.
113+
114+
If you need a check a simple Type::
115+
116+
// You need to check if a Type
117+
$type = Type::int(); // with a simple int type
118+
// You can check if a given type comply with a given identifier
119+
$type->isIdentifiedBy(TypeIdentifier::INT); // true
120+
$type->isIdentifiedBy(TypeIdentifier::STRING); // false
121+
122+
$type = Type::union(Type::string(), Type::int()); // with an union of int and string types
123+
// You can now see that the second check will pass to true since we have an union with a string type
124+
$type->isIdentifiedBy(TypeIdentifier::INT); // true
125+
$type->isIdentifiedBy(TypeIdentifier::STRING); // true
126+
127+
class DummyParent {}
128+
class Dummy extends DummyParent implements DummyInterface {}
129+
$type = Type::object(Dummy::class); // with an object Type
130+
// You can check is the Type is an object, or even if it's a given class
131+
$type->isIdentifiedBy(TypeIdentifier::OBJECT); // true
132+
$type->isIdentifiedBy(Dummy::class); // true
133+
// Or inherits/implements something
134+
$type->isIdentifiedBy(DummyParent::class); // true
135+
$type->isIdentifiedBy(DummyInterface::class); // true
136+
137+
Sometimes you want to check for more than one thing at a time so a callable may be better to check everything::
138+
139+
class Foo
140+
{
141+
private int $integer;
142+
private string $string;
143+
private ?float $float;
144+
}
145+
146+
$reflClass = new \ReflectionClass(Foo::class);
147+
148+
$resolver = TypeResolver::create();
149+
$integerType = $resolver->resolve($reflClass->getProperty('integer'));
150+
$stringType = $resolver->resolve($reflClass->getProperty('string'));
151+
$floatType = $resolver->resolve($reflClass->getProperty('float'));
152+
153+
// your callable to check whatever you need
154+
// here we want to validate a given type is a non nullable number
155+
$isNonNullableNumber = function (Type $type): bool {
156+
if ($type->isNullable()) {
157+
return false;
158+
}
159+
160+
if ($type->isIdentifiedBy(TypeIdentifier::INT) || $type->isIdentifiedBy(TypeIdentifier::FLOAT)) {
161+
return true;
162+
}
163+
164+
return false;
165+
};
166+
167+
$integerType->isSatisfiedBy($isNonNullableNumber); // true
168+
$stringType->isSatisfiedBy($isNonNullableNumber); // false
169+
$floatType->isSatisfiedBy($isNonNullableNumber); // false

0 commit comments

Comments
 (0)