Skip to content

Commit 113b050

Browse files
committed
Improve Reflection API type hinting
1 parent 51c8253 commit 113b050

17 files changed

+111
-83
lines changed

Reflection/Reflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Reflection
1616
*
1717
* @link https://php.net/manual/en/reflection.getmodifiernames.php
1818
* @param int $modifiers Bitfield of the modifiers to get.
19-
* @return string[] An array of modifier names.
19+
* @return list<non-empty-string> An array of modifier names.
2020
*/
2121
#[TentativeType]
2222
public static function getModifierNames(#[LanguageLevelTypeAware(['8.0' => 'int'], default: '')] $modifiers): array {}

Reflection/ReflectionAttribute.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
/**
66
* @since 8.0
77
*
8-
* @template T of object
8+
* @template TAttributeClass of Attribute
99
*/
1010
class ReflectionAttribute implements Reflector
1111
{
12+
/**
13+
* @var class-string<TAttributeClass>
14+
*/
1215
public string $name;
1316

1417
/**
@@ -30,7 +33,7 @@ private function __construct() {}
3033
/**
3134
* Gets attribute name
3235
*
33-
* @return string The name of the attribute parameter.
36+
* @return class-string<TAttributeClass> The name of the attribute parameter.
3437
* @since 8.0
3538
*/
3639
#[Pure]
@@ -57,7 +60,7 @@ public function isRepeated(): bool {}
5760
/**
5861
* Gets list of passed attribute's arguments.
5962
*
60-
* @return array
63+
* @return list<mixed>
6164
* @since 8.0
6265
*/
6366
#[Pure]
@@ -66,7 +69,7 @@ public function getArguments(): array {}
6669
/**
6770
* Creates a new instance of the attribute with passed arguments
6871
*
69-
* @return T
72+
* @return TAttributeClass
7073
* @since 8.0
7174
*/
7275
public function newInstance(): object {}

Reflection/ReflectionClass.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
use JetBrains\PhpStorm\Pure;
99

1010
/**
11-
* @template T of object
1211
* The <b>ReflectionClass</b> class reports information about a class.
1312
*
1413
* @link https://php.net/manual/en/class.reflectionclass.php
14+
* @template TReflectedClass of object
1515
*/
1616
class ReflectionClass implements Reflector
1717
{
1818
/**
19-
* @var class-string<T> Name of the class, same as calling the {@see ReflectionClass::getName()} method
19+
* @var class-string<TReflectedClass> Name of the class, same as calling the {@see ReflectionClass::getName()} method
2020
*/
2121
#[Immutable]
2222
#[LanguageLevelTypeAware(['8.1' => 'string'], default: '')]
@@ -62,7 +62,7 @@ class ReflectionClass implements Reflector
6262
* Constructs a ReflectionClass
6363
*
6464
* @link https://php.net/manual/en/reflectionclass.construct.php
65-
* @param class-string<T>|T $objectOrClass Either a string containing the name of
65+
* @param class-string<TReflectedClass>|TReflectedClass $objectOrClass Either a string containing the name of
6666
* the class to reflect, or an object.
6767
* @throws ReflectionException if the class does not exist.
6868
*/
@@ -95,7 +95,7 @@ public function __toString(): string {}
9595
* Gets class name
9696
*
9797
* @link https://php.net/manual/en/reflectionclass.getname.php
98-
* @return string The class name.
98+
* @return class-string<TReflectedClass> The class name.
9999
*/
100100
#[Pure]
101101
#[TentativeType]
@@ -189,7 +189,7 @@ public function getDocComment(): string|false {}
189189
* Gets the constructor of the class
190190
*
191191
* @link https://php.net/manual/en/reflectionclass.getconstructor.php
192-
* @return ReflectionMethod|null A {@see ReflectionMethod} object reflecting
192+
* @return ReflectionMethod<TReflectedClass>|null A {@see ReflectionMethod} object reflecting
193193
* the class' constructor, or {@see null} if the class has no constructor.
194194
*/
195195
#[Pure]
@@ -210,8 +210,8 @@ public function hasMethod(#[LanguageLevelTypeAware(['8.0' => 'string'], default:
210210
* Gets a <b>ReflectionMethod</b> for a class method.
211211
*
212212
* @link https://php.net/manual/en/reflectionclass.getmethod.php
213-
* @param string $name The method name to reflect.
214-
* @return ReflectionMethod A {@see ReflectionMethod}
213+
* @param non-empty-string $name The method name to reflect.
214+
* @return ReflectionMethod<TReflectedClass> A {@see ReflectionMethod}
215215
* @throws ReflectionException if the method does not exist.
216216
*/
217217
#[Pure]
@@ -224,7 +224,7 @@ public function getMethod(#[LanguageLevelTypeAware(['8.0' => 'string'], default:
224224
* @link https://php.net/manual/en/reflectionclass.getmethods.php
225225
* @param int|null $filter Filter the results to include only methods
226226
* with certain attributes. Defaults to no filtering.
227-
* @return ReflectionMethod[] An array of {@see ReflectionMethod} objects
227+
* @return list<ReflectionMethod<TReflectedClass>> An array of {@see ReflectionMethod} objects
228228
* reflecting each method.
229229
*/
230230
#[Pure]
@@ -245,8 +245,8 @@ public function hasProperty(#[LanguageLevelTypeAware(['8.0' => 'string'], defaul
245245
* Gets a <b>ReflectionProperty</b> for a class's property
246246
*
247247
* @link https://php.net/manual/en/reflectionclass.getproperty.php
248-
* @param string $name The property name.
249-
* @return ReflectionProperty A {@see ReflectionProperty}
248+
* @param non-empty-string $name The property name.
249+
* @return ReflectionProperty<TReflectedClass> A {@see ReflectionProperty}
250250
* @throws ReflectionException If no property exists by that name.
251251
*/
252252
#[Pure]
@@ -260,7 +260,7 @@ public function getProperty(#[LanguageLevelTypeAware(['8.0' => 'string'], defaul
260260
* @param int|null $filter The optional filter, for filtering desired
261261
* property types. It's configured using the {@see ReflectionProperty} constants,
262262
* and defaults to all property types.
263-
* @return ReflectionProperty[]
263+
* @return list<ReflectionProperty<TReflectedClass>>
264264
*/
265265
#[Pure]
266266
#[TentativeType]
@@ -270,8 +270,8 @@ public function getProperties(#[LanguageLevelTypeAware(['8.0' => 'int|null'], de
270270
* Gets a ReflectionClassConstant for a class's property
271271
*
272272
* @link https://php.net/manual/en/reflectionclass.getreflectionconstant.php
273-
* @param string $name The class constant name.
274-
* @return ReflectionClassConstant|false A {@see ReflectionClassConstant}.
273+
* @param non-empty-string $name The class constant name.
274+
* @return ReflectionClassConstant<TReflectedClass>|false A {@see ReflectionClassConstant}.
275275
* @since 7.1
276276
*/
277277
#[Pure]
@@ -283,7 +283,7 @@ public function getReflectionConstant(string $name): ReflectionClassConstant|fal
283283
*
284284
* @link https://php.net/manual/en/reflectionclass.getreflectionconstants.php
285285
* @param int|null $filter [optional] allows the filtering of constants defined in a class by their visibility. Since 8.0.
286-
* @return ReflectionClassConstant[] An array of ReflectionClassConstant objects.
286+
* @return list<ReflectionClassConstant<TReflectedClass>> An array of ReflectionClassConstant objects.
287287
* @since 7.1
288288
*/
289289
#[Pure]
@@ -305,7 +305,7 @@ public function hasConstant(#[LanguageLevelTypeAware(['8.0' => 'string'], defaul
305305
*
306306
* @link https://php.net/manual/en/reflectionclass.getconstants.php
307307
* @param int|null $filter [optional] allows the filtering of constants defined in a class by their visibility. Since 8.0.
308-
* @return array An array of constants, where the keys hold the name and
308+
* @return array<non-empty-string, mixed> An array of constants, where the keys hold the name and
309309
* the values the value of the constants.
310310
*/
311311
#[Pure]
@@ -316,7 +316,7 @@ public function getConstants(#[PhpStormStubsElementAvailable(from: '8.0')] ?int
316316
* Gets defined constant
317317
*
318318
* @link https://php.net/manual/en/reflectionclass.getconstant.php
319-
* @param string $name Name of the constant.
319+
* @param non-empty-string $name Name of the constant.
320320
* @return mixed|false Value of the constant with the name name.
321321
* Returns {@see false} if the constant was not found in the class.
322322
*/
@@ -328,7 +328,7 @@ public function getConstant(#[LanguageLevelTypeAware(['8.0' => 'string'], defaul
328328
* Gets the interfaces
329329
*
330330
* @link https://php.net/manual/en/reflectionclass.getinterfaces.php
331-
* @return ReflectionClass[] An associative array of interfaces, with keys as interface
331+
* @return list<ReflectionClass> An associative array of interfaces, with keys as interface
332332
* names and the array values as {@see ReflectionClass} objects.
333333
*/
334334
#[Pure]
@@ -339,7 +339,7 @@ public function getInterfaces(): array {}
339339
* Gets the interface names
340340
*
341341
* @link https://php.net/manual/en/reflectionclass.getinterfacenames.php
342-
* @return string[] A numerical array with interface names as the values.
342+
* @return list<class-string> A numerical array with interface names as the values.
343343
*/
344344
#[Pure]
345345
#[TentativeType]
@@ -370,7 +370,7 @@ public function isInterface(): bool {}
370370
* Returns an array of traits used by this class
371371
*
372372
* @link https://php.net/manual/en/reflectionclass.gettraits.php
373-
* @return ReflectionClass[] an array with trait names in keys and
373+
* @return list<ReflectionClass> an array with trait names in keys and
374374
* instances of trait's {@see ReflectionClass} in values.
375375
* @since 5.4
376376
*/
@@ -382,7 +382,7 @@ public function getTraits(): array {}
382382
* Returns an array of names of traits used by this class
383383
*
384384
* @link https://php.net/manual/en/reflectionclass.gettraitnames.php
385-
* @return string[] An array with trait names in values.
385+
* @return list<class-string> An array with trait names in values.
386386
* Returns {@see null} in case of an error.
387387
* @since 5.4
388388
*/
@@ -394,7 +394,7 @@ public function getTraitNames(): array {}
394394
* Returns an array of trait aliases
395395
*
396396
* @link https://php.net/manual/en/reflectionclass.gettraitaliases.php
397-
* @return string[] an array with new method names in keys and original
397+
* @return array<non-empty-string, non-empty-string> an array with new method names in keys and original
398398
* names (in the format "TraitName::original") in values.
399399
* Returns {@see null} in case of an error.
400400
* @since 5.4
@@ -469,7 +469,7 @@ public function isInstance(#[LanguageLevelTypeAware(['8.0' => 'object'], default
469469
* @link https://php.net/manual/en/reflectionclass.newinstance.php
470470
* @param mixed ...$args Accepts a variable number of arguments which are
471471
* passed to the class constructor, much like {@see call_user_func}
472-
* @return T a new instance of the class.
472+
* @return TReflectedClass a new instance of the class.
473473
* @throws ReflectionException if the class constructor is not public or if
474474
* the class does not have a constructor and the $args parameter contains
475475
* one or more parameters.
@@ -480,7 +480,7 @@ public function newInstance(...$args) {}
480480
* Creates a new class instance without invoking the constructor.
481481
*
482482
* @link https://php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php
483-
* @return T a new instance of the class.
483+
* @return TReflectedClass a new instance of the class.
484484
* @throws ReflectionException if the class is an internal class that
485485
* cannot be instantiated without invoking the constructor. In PHP 5.6.0
486486
* onwards, this exception is limited only to internal classes that are final.
@@ -494,7 +494,7 @@ public function newInstanceWithoutConstructor(): object {}
494494
*
495495
* @link https://php.net/manual/en/reflectionclass.newinstanceargs.php
496496
* @param array $args The parameters to be passed to the class constructor as an array.
497-
* @return T|null a new instance of the class.
497+
* @return TReflectedClass|null a new instance of the class.
498498
* @throws ReflectionException if the class constructor is not public or if
499499
* the class does not have a constructor and the $args parameter contains
500500
* one or more parameters.
@@ -530,7 +530,7 @@ public function isSubclassOf(#[LanguageLevelTypeAware(['8.0' => 'ReflectionClass
530530
* Gets static properties
531531
*
532532
* @link https://php.net/manual/en/reflectionclass.getstaticproperties.php
533-
* @return array|null The static properties, as an array where the keys hold
533+
* @return array<non-empty-string, mixed>|null The static properties, as an array where the keys hold
534534
* the name and the values the value of the properties.
535535
*/
536536
#[Pure]
@@ -542,7 +542,7 @@ public function getStaticProperties() {}
542542
* Gets static property value
543543
*
544544
* @link https://php.net/manual/en/reflectionclass.getstaticpropertyvalue.php
545-
* @param string $name The name of the static property for which to return a value.
545+
* @param non-empty-string $name The name of the static property for which to return a value.
546546
* @param mixed $default [optional] A default value to return in case the class does
547547
* not declare a static property with the given name. If the property does
548548
* not exist and this argument is omitted, a {@see ReflectionException} is thrown.
@@ -559,7 +559,7 @@ public function getStaticPropertyValue(
559559
* Sets static property value
560560
*
561561
* @link https://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php
562-
* @param string $name Property name.
562+
* @param non-empty-string $name Property name.
563563
* @param mixed $value New property value.
564564
* @return void No value is returned.
565565
*/
@@ -573,7 +573,7 @@ public function setStaticPropertyValue(
573573
* Gets default properties
574574
*
575575
* @link https://php.net/manual/en/reflectionclass.getdefaultproperties.php
576-
* @return mixed[] An array of default properties, with the key being the name
576+
* @return array<non-empty-string, mixed> An array of default properties, with the key being the name
577577
* of the property and the value being the default value of the property
578578
* or {@see null} if the property doesn't have a default value. The function
579579
* does not distinguish between static and non static properties and does
@@ -659,20 +659,20 @@ public function getNamespaceName(): string {}
659659
* Gets short name
660660
*
661661
* @link https://php.net/manual/en/reflectionclass.getshortname.php
662-
* @return string The class short name.
662+
* @return non-empty-string The class short name.
663663
*/
664664
#[Pure]
665665
#[TentativeType]
666666
public function getShortName(): string {}
667667

668668
/**
669-
* @template T
669+
* @template TAttributeClass of Attribute
670670
*
671671
* Returns an array of class attributes.
672672
*
673-
* @param class-string<T>|null $name Name of an attribute class
673+
* @param class-string<TAttributeClass>|null $name Name of an attribute class
674674
* @param int $flags Сriteria by which the attribute is searched.
675-
* @return ReflectionAttribute<T>[]
675+
* @return list<ReflectionAttribute<TAttributeClass>>
676676
* @since 8.0
677677
*/
678678
#[Pure]
@@ -705,7 +705,7 @@ public function isEnum(): bool {}
705705
public function newLazyGhost(callable $initializer, int $options = 0): object {}
706706

707707
/**
708-
* @return T
708+
* @return TReflectedClass
709709
* @since 8.4
710710
*/
711711
public function newLazyProxy(callable $factory, int $options = 0): object {}

Reflection/ReflectionClassConstant.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
1212
*
1313
* @link https://www.php.net/manual/en/class.reflectionclassconstant.php
1414
* @since 7.1
15+
* @template TReflectedClass of object
1516
*/
1617
class ReflectionClassConstant implements Reflector
1718
{
1819
/**
19-
* @var string Constant name, same as calling the {@see ReflectionClassConstant::getName()} method
20+
* @var non-empty-string Constant name, same as calling the {@see ReflectionClassConstant::getName()} method
2021
*/
2122
#[Immutable]
2223
#[LanguageLevelTypeAware(['8.1' => 'string'], default: '')]
2324
public $name;
2425

2526
/**
26-
* @var class-string Fully qualified class name where this constant was defined
27+
* @var class-string<TReflectedClass> Fully qualified class name where this constant was defined
2728
*/
2829
#[Immutable]
2930
#[LanguageLevelTypeAware(['8.1' => 'string'], default: '')]
@@ -65,8 +66,8 @@ class ReflectionClassConstant implements Reflector
6566
/**
6667
* ReflectionClassConstant constructor.
6768
*
68-
* @param class-string|object $class Either a string containing the name of the class to reflect, or an object.
69-
* @param string $constant The name of the class constant.
69+
* @param class-string<TReflectedClass>|TReflectedClass $class Either a string containing the name of the class to reflect, or an object.
70+
* @param non-empty-string $constant The name of the class constant.
7071
* @since 7.1
7172
* @link https://php.net/manual/en/reflectionclassconstant.construct.php
7273
*/
@@ -123,7 +124,7 @@ public function getModifiers(): int {}
123124
* Get name of the constant
124125
*
125126
* @link https://php.net/manual/en/reflectionclassconstant.getname.php
126-
* @return string Returns the constant's name.
127+
* @return non-empty-string Returns the constant's name.
127128
* @since 7.1
128129
*/
129130
#[Pure]
@@ -184,13 +185,13 @@ public function isPublic(): bool {}
184185
public function __toString(): string {}
185186

186187
/**
187-
* @template T
188+
* @template TAttributeClass of Attribute
188189
*
189190
* Returns an array of constant attributes.
190191
*
191-
* @param class-string<T>|null $name Name of an attribute class
192+
* @param class-string<TAttributeClass>|null $name Name of an attribute class
192193
* @param int $flags Сriteria by which the attribute is searched.
193-
* @return ReflectionAttribute<T>[]
194+
* @return list<ReflectionAttribute<TAttributeClass>>
194195
* @since 8.0
195196
*/
196197
#[Pure]

Reflection/ReflectionConstant.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public function getExtension(): ?ReflectionExtension {}
3636
public function getExtensionName(): string|false {}
3737

3838
/**
39+
* @template TAttributeClass of Attribute
40+
*
41+
* Returns an array of extension attributes.
42+
*
43+
* @param class-string<TAttributeClass>|null $name Name of an attribute class
44+
* @param int $flags Сriteria by which the attribute is searched.
45+
* @return list<ReflectionAttribute<TAttributeClass>>
3946
* @since 8.5
4047
*/
4148
public function getAttributes(?string $name = null, int $flags = 0): array {}

0 commit comments

Comments
 (0)