Skip to content

Commit fb5362c

Browse files
authored
Reflection deprecation (#578)
* Clarify what a built-in type is. * Add PHP 8 equivalent of ReflectionParameter::isArray(). * Leave note on ReflectionParameter::getClass() for what to use instead. * Clarify order of the array in getParameters(). * Add example to aid working with union types. * Add updated equivalent of isCallable().
1 parent ca9dbbb commit fb5362c

File tree

6 files changed

+99
-9
lines changed

6 files changed

+99
-9
lines changed

reference/reflection/reflectionfunctionabstract/getparameters.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<void />
1515
</methodsynopsis>
1616
<para>
17-
Get the parameters as an array of <type>ReflectionParameter</type>.
17+
Get the parameters as an array of <type>ReflectionParameter</type>,
18+
in the order in which they are defined in the source.
1819
</para>
1920

2021
&warn.undocumented.func;

reference/reflection/reflectionnamedtype/isbuiltin.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<void />
1515
</methodsynopsis>
1616
<para>
17-
Checks if the type is a built-in type in PHP.
17+
Checks if the type is a built-in type in PHP. A built-in type is any type that
18+
is not a class, interface, or trait.
1819
</para>
1920
</refsect1>
2021

reference/reflection/reflectionparameter/getclass.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
<para>
2020
Gets the class type hinted for the parameter as a <classname>ReflectionClass</classname> object.
2121
</para>
22+
<para>
23+
As of PHP 8.0.0 this function is deprecated and not recommended. Instead, use
24+
<methodname>ReflectionParameter::getType</methodname> to get the <classname>ReflectionType</classname>
25+
of the parameter, then interrogate that object to determine the parameter type.
26+
</para>
2227

2328
&warn.undocumented.func;
2429

@@ -36,7 +41,7 @@
3641
or the declared type is not a class or interface.
3742
</para>
3843
</refsect1>
39-
44+
4045
<refsect1 role="examples">
4146
&reftitle.examples;
4247
<para>
@@ -55,12 +60,6 @@ echo $aParameter->getClass()->name;
5560
?>
5661
]]>
5762
</programlisting>
58-
&example.outputs;
59-
<screen>
60-
<![CDATA[
61-
Exception
62-
]]>
63-
</screen>
6463
</example>
6564
</para>
6665
</refsect1>

reference/reflection/reflectionparameter/gettype.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ NULL
9292
]]>
9393
</screen>
9494
</example>
95+
<example>
96+
<title><methodname>ReflectionParameter::getType</methodname> Usage in PHP 8.0.0 and later</title>
97+
<para>
98+
As of PHP 8.0.0, this method may return a <classname>ReflectionNamedType</classname> instance or
99+
a <classname>ReflectionUnionType</classname> instance. The latter is a collection of the former.
100+
To analyze a type, it is often convenient to normalize it to an array of <classname>ReflectionNamedType</classname>
101+
objects. The following function will return an array of <literal>0</literal> or more <classname>ReflectionNamedType</classname>
102+
instances.
103+
</para>
104+
<programlisting role="php">
105+
<![CDATA[
106+
<?php
107+
function getAllTypes(ReflectionParameter $reflectionParameter): array
108+
{
109+
$reflectionType = $reflectionParameter->getType();
110+
111+
if (!$reflectionType) return [];
112+
113+
return $reflectionType instanceof ReflectionUnionType
114+
? $reflectionType->getTypes()
115+
: [$reflectionType];
116+
}
117+
?>
118+
]]>
119+
</programlisting>
120+
</example>
95121
</para>
96122
</refsect1>
97123

reference/reflection/reflectionparameter/isarray.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<refsynopsisdiv>
1111
&warn.deprecated.function-8-0-0;
12+
<para>See the example below for an alternative way to derive this information.</para>
1213
</refsynopsisdiv>
1314

1415
<refsect1 role="description">
@@ -34,6 +35,37 @@
3435
</para>
3536
</refsect1>
3637

38+
<refsect1 role="examples">
39+
&reftitle.examples;
40+
<para>
41+
<example>
42+
<title>PHP 8.0.0 equivalent</title>
43+
<para>
44+
As of PHP 8.0.0, the following code will report if a type declares arrays,
45+
including as part of a union.
46+
</para>
47+
<programlisting role="php">
48+
<![CDATA[
49+
<?php
50+
function declaresArray(ReflectionParameter $reflectionParameter): bool
51+
{
52+
$reflectionType = $reflectionParameter->getType();
53+
54+
if (!$reflectionType) return false;
55+
56+
$types = $reflectionType instanceof ReflectionUnionType
57+
? $reflectionType->getTypes()
58+
: [$reflectionType];
59+
60+
return in_array('array', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
61+
}
62+
?>
63+
]]>
64+
</programlisting>
65+
</example>
66+
</para>
67+
</refsect1>
68+
3769
<refsect1 role="seealso">
3870
&reftitle.seealso;
3971
<para>

reference/reflection/reflectionparameter/iscallable.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<refsynopsisdiv>
1111
&warn.deprecated.function-8-0-0;
12+
<para>See the example below for an alternative way to derive this information.</para>
1213
</refsynopsisdiv>
1314

1415
<refsect1 role="description">
@@ -38,6 +39,36 @@
3839
</para>
3940
</refsect1>
4041

42+
<refsect1 role="examples">
43+
&reftitle.examples;
44+
<para>
45+
<example>
46+
<title>PHP 8.0.0 equivalent</title>
47+
<para>
48+
As of PHP 8.0.0, the following code will report if a type supports callables,
49+
including as part of a union.
50+
</para>
51+
<programlisting role="php">
52+
<![CDATA[
53+
<?php
54+
function declaresCallable(ReflectionParameter $reflectionParameter): bool
55+
{
56+
$reflectionType = $reflectionParameter->getType();
57+
58+
if (!$reflectionType) return false;
59+
60+
$types = $reflectionType instanceof ReflectionUnionType
61+
? $reflectionType->getTypes()
62+
: [$reflectionType];
63+
64+
return in_array('callable', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
65+
}
66+
?>
67+
]]>
68+
</programlisting>
69+
</example>
70+
</para>
71+
</refsect1>
4172

4273
</refentry>
4374

0 commit comments

Comments
 (0)