From 8b15c88f45179d0756679aa7355bce8806e42853 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Sun, 7 Jun 2026 15:34:16 +0200 Subject: [PATCH] Further reflection api improvements --- Reflection/ReflectionAttribute.php | 2 +- Reflection/ReflectionClass.php | 24 ++++++++++++++++++----- Reflection/ReflectionClassConstant.php | 4 ++-- Reflection/ReflectionConstant.php | 2 +- Reflection/ReflectionEnum.php | 3 ++- Reflection/ReflectionEnumBackedCase.php | 1 + Reflection/ReflectionEnumUnitCase.php | 1 + Reflection/ReflectionExtension.php | 12 ++++++------ Reflection/ReflectionFunctionAbstract.php | 6 +++--- Reflection/ReflectionMethod.php | 8 ++++---- Reflection/ReflectionParameter.php | 6 +++--- Reflection/ReflectionProperty.php | 4 ++-- Reflection/Reflector.php | 3 ++- 13 files changed, 47 insertions(+), 29 deletions(-) diff --git a/Reflection/ReflectionAttribute.php b/Reflection/ReflectionAttribute.php index 9fbc62012..964c496ec 100644 --- a/Reflection/ReflectionAttribute.php +++ b/Reflection/ReflectionAttribute.php @@ -5,7 +5,7 @@ /** * @since 8.0 * - * @template TAttributeClass of object + * @template-covariant TAttributeClass of object */ class ReflectionAttribute implements Reflector { diff --git a/Reflection/ReflectionClass.php b/Reflection/ReflectionClass.php index e79efe0b6..4b0969cce 100644 --- a/Reflection/ReflectionClass.php +++ b/Reflection/ReflectionClass.php @@ -11,7 +11,7 @@ * The ReflectionClass class reports information about a class. * * @link https://php.net/manual/en/class.reflectionclass.php - * @template TReflectedClass of object + * @template-covariant TReflectedClass of object */ class ReflectionClass implements Reflector { @@ -62,7 +62,7 @@ class ReflectionClass implements Reflector * Constructs a ReflectionClass * * @link https://php.net/manual/en/reflectionclass.construct.php - * @param class-string|TReflectedClass $objectOrClass Either a string containing the name of + * @param TReflectedClass|class-string $objectOrClass Either a string containing the name of * the class to reflect, or an object. * @throws ReflectionException if the class does not exist. */ @@ -210,7 +210,7 @@ public function hasMethod(#[LanguageLevelTypeAware(['8.0' => 'string'], default: * Gets a ReflectionMethod for a class method. * * @link https://php.net/manual/en/reflectionclass.getmethod.php - * @param non-empty-string $name The method name to reflect. + * @param non-falsy-string $name The method name to reflect. * @return ReflectionMethod A {@see ReflectionMethod} * @throws ReflectionException if the method does not exist. */ @@ -494,7 +494,7 @@ public function newInstanceWithoutConstructor(): object {} * Creates a new class instance from given arguments. * * @link https://php.net/manual/en/reflectionclass.newinstanceargs.php - * @param array $args The parameters to be passed to the class constructor as an array. + * @param array $args The parameters to be passed to the class constructor as an array. * @return TReflectedClass|null a new instance of the class. * @throws ReflectionException if the class constructor is not public or if * the class does not have a constructor and the $args parameter contains @@ -672,7 +672,7 @@ public function getShortName(): string {} * @template TAttributeClass of object * @param class-string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. - * @return list> + * @return ($name is null ? list> : list>) * @since 8.0 */ #[Pure] @@ -700,42 +700,56 @@ private function __clone(): void {} public function isEnum(): bool {} /** + * @param (callable(TReflectedClass): void) $initializer + * @return TReflectedClass * @since 8.4 */ public function newLazyGhost(callable $initializer, int $options = 0): object {} /** + * @param (callable(TReflectedClass): TReflectedClass) $factory * @return TReflectedClass * @since 8.4 */ public function newLazyProxy(callable $factory, int $options = 0): object {} /** + * @param TReflectedClass $object + * @param (callable(TReflectedClass): void) $initializer * @since 8.4 */ public function resetAsLazyGhost(object $object, callable $initializer, int $options = 0): void {} /** + * @param TReflectedClass $object + * @param (callable(TReflectedClass): TReflectedClass) $factory * @since 8.4 */ public function resetAsLazyProxy(object $object, callable $factory, int $options = 0): void {} /** + * @param TReflectedClass $object + * @return TReflectedClass * @since 8.4 */ public function initializeLazyObject(object $object): object {} /** + * @param TReflectedClass $object + * @return TReflectedClass * @since 8.4 */ public function isUninitializedLazyObject(object $object): bool {} /** + * @param TReflectedClass $object * @since 8.4 */ public function markLazyObjectAsInitialized(object $object): object {} /** + * @param TReflectedClass $object + * @return null|(callable(TReflectedClass): void) * @since 8.4 */ public function getLazyInitializer(object $object): ?callable {} diff --git a/Reflection/ReflectionClassConstant.php b/Reflection/ReflectionClassConstant.php index 7475e81b7..2e8c567a9 100644 --- a/Reflection/ReflectionClassConstant.php +++ b/Reflection/ReflectionClassConstant.php @@ -89,7 +89,7 @@ public static function export($class, $name, $return = false) {} /** * Gets declaring class * - * @return ReflectionClass + * @return ReflectionClass * @link https://php.net/manual/en/reflectionclassconstant.getdeclaringclass.php * @since 7.1 */ @@ -190,7 +190,7 @@ public function __toString(): string {} * @template TAttributeClass of object * @param class-string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. - * @return list> + * @return ($name is null ? list> : list>) * @since 8.0 */ #[Pure] diff --git a/Reflection/ReflectionConstant.php b/Reflection/ReflectionConstant.php index c69e4384f..ebbdc3530 100644 --- a/Reflection/ReflectionConstant.php +++ b/Reflection/ReflectionConstant.php @@ -41,7 +41,7 @@ public function getExtensionName(): string|false {} * @template TAttributeClass of object * @param class-string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. - * @return list> + * @return ($name is null ? list> : list>) * @since 8.5 */ public function getAttributes(?string $name = null, int $flags = 0): array {} diff --git a/Reflection/ReflectionEnum.php b/Reflection/ReflectionEnum.php index a6e1ec18b..43b39b280 100644 --- a/Reflection/ReflectionEnum.php +++ b/Reflection/ReflectionEnum.php @@ -5,7 +5,8 @@ /** * @link https://php.net/manual/en/class.reflectionenum.php * @since 8.1 - * @template TReflectedClass of UnitEnum + * @template-covariant TReflectedClass of UnitEnum + * @extends ReflectionClass */ class ReflectionEnum extends ReflectionClass { diff --git a/Reflection/ReflectionEnumBackedCase.php b/Reflection/ReflectionEnumBackedCase.php index fa3e1d339..462fea55c 100644 --- a/Reflection/ReflectionEnumBackedCase.php +++ b/Reflection/ReflectionEnumBackedCase.php @@ -6,6 +6,7 @@ * @link https://php.net/manual/en/class.reflectionenumbackedcase.php * @since 8.1 * @template TReflectedClass of BackedEnum + * @extends ReflectionEnumUnitCase */ class ReflectionEnumBackedCase extends ReflectionEnumUnitCase { diff --git a/Reflection/ReflectionEnumUnitCase.php b/Reflection/ReflectionEnumUnitCase.php index 13710f290..c2dd9ee73 100644 --- a/Reflection/ReflectionEnumUnitCase.php +++ b/Reflection/ReflectionEnumUnitCase.php @@ -6,6 +6,7 @@ * @link https://php.net/manual/en/class.reflectionenumunitcase.php * @since 8.1 * @template TReflectedClass of UnitEnum + * @extends ReflectionClassConstant */ class ReflectionEnumUnitCase extends ReflectionClassConstant { diff --git a/Reflection/ReflectionExtension.php b/Reflection/ReflectionExtension.php index c827868ab..b7713a109 100644 --- a/Reflection/ReflectionExtension.php +++ b/Reflection/ReflectionExtension.php @@ -80,7 +80,7 @@ public function getVersion(): ?string {} * Gets extension functions * * @link https://php.net/manual/en/reflectionextension.getfunctions.php - * @return ReflectionFunction[] An associative array of {@see ReflectionFunction} objects, + * @return array An associative array of {@see ReflectionFunction} objects, * for each function defined in the extension with the keys being the function * names. If no function are defined, an empty array is returned. */ @@ -92,7 +92,7 @@ public function getFunctions(): array {} * Gets constants * * @link https://php.net/manual/en/reflectionextension.getconstants.php - * @return array An associative array with constant names as keys. + * @return array An associative array with constant names as keys. */ #[Pure] #[TentativeType] @@ -102,7 +102,7 @@ public function getConstants(): array {} * Gets extension ini entries * * @link https://php.net/manual/en/reflectionextension.getinientries.php - * @return array An associative array with the ini entries as keys, + * @return array An associative array with the ini entries as keys, * with their defined values as values. */ #[Pure] @@ -113,7 +113,7 @@ public function getINIEntries(): array {} * Gets classes * * @link https://php.net/manual/en/reflectionextension.getclasses.php - * @return ReflectionClass[] An array of {@see ReflectionClass} objects, one + * @return array> An array of {@see ReflectionClass} objects, one * for each class within the extension. If no classes are defined, * an empty array is returned. */ @@ -125,7 +125,7 @@ public function getClasses(): array {} * Gets class names * * @link https://php.net/manual/en/reflectionextension.getclassnames.php - * @return string[] An array of class names, as defined in the extension. + * @return list> An array of class names, as defined in the extension. * If no classes are defined, an empty array is returned. */ #[Pure] @@ -136,7 +136,7 @@ public function getClassNames(): array {} * Gets dependencies * * @link https://php.net/manual/en/reflectionextension.getdependencies.php - * @return string[] An associative array with dependencies as keys and + * @return array An associative array with dependencies as keys and * either Required, Optional or Conflicts as the values. */ #[Pure] diff --git a/Reflection/ReflectionFunctionAbstract.php b/Reflection/ReflectionFunctionAbstract.php index 3845c1190..ed5fb377a 100644 --- a/Reflection/ReflectionFunctionAbstract.php +++ b/Reflection/ReflectionFunctionAbstract.php @@ -124,7 +124,7 @@ public function getClosureThis(): ?object {} * Returns the scope associated to the closure * * @link https://php.net/manual/en/reflectionfunctionabstract.getclosurescopeclass.php - * @return ReflectionClass|null Returns the class on success or {@see null} + * @return ReflectionClass|null Returns the class on success or {@see null} * on failure. * @since 5.4 */ @@ -133,7 +133,7 @@ public function getClosureThis(): ?object {} public function getClosureScopeClass(): ?ReflectionClass {} /** - * @return ReflectionClass|null Returns the class on success or {@see null} + * @return ReflectionClass|null Returns the class on success or {@see null} * on failure. * @since 8.0 */ @@ -321,7 +321,7 @@ public function hasReturnType(): bool {} * @template TAttributeClass of object * @param class-string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. - * @return list> + * @return ($name is null ? list> : list>) * @since 8.0 */ #[Pure] diff --git a/Reflection/ReflectionMethod.php b/Reflection/ReflectionMethod.php index b11ce8cd9..5d3ebd8c0 100644 --- a/Reflection/ReflectionMethod.php +++ b/Reflection/ReflectionMethod.php @@ -17,7 +17,7 @@ class ReflectionMethod extends ReflectionFunctionAbstract { /** - * @var string Name of the method, same as calling the {@see ReflectionMethod::getName()} method + * @var non-falsy-string Name of the method, same as calling the {@see ReflectionMethod::getName()} method */ #[Immutable] #[LanguageLevelTypeAware(['8.1' => 'string'], default: '')] @@ -73,7 +73,7 @@ class ReflectionMethod extends ReflectionFunctionAbstract * @param non-empty-string|class-string|TReflectedClass $objectOrMethod Classname, object * (instance of the class) that contains the method or class name and * method name delimited by ::. - * @param non-empty-string|null $method Name of the method if the first argument is a + * @param non-falsy-string|null $method Name of the method if the first argument is a * classname or an object. * @throws ReflectionException if the class or method does not exist. */ @@ -264,7 +264,7 @@ public function invokeArgs(#[LanguageLevelTypeAware(['8.0' => 'object|null'], de * Gets declaring class for the reflected method. * * @link https://php.net/manual/en/reflectionmethod.getdeclaringclass.php - * @return ReflectionClass A {@see ReflectionClass} object of the class that the + * @return ReflectionClass A {@see ReflectionClass} object of the class that the * reflected method is part of. */ #[Pure] @@ -275,7 +275,7 @@ public function getDeclaringClass(): ReflectionClass {} * Gets the method prototype (if there is one). * * @link https://php.net/manual/en/reflectionmethod.getprototype.php - * @return ReflectionMethod A {@see ReflectionMethod} instance of the method prototype. + * @return ReflectionMethod A {@see ReflectionMethod} instance of the method prototype. * @throws ReflectionException if the method does not have a prototype */ #[Pure] diff --git a/Reflection/ReflectionParameter.php b/Reflection/ReflectionParameter.php index c9d0d26ec..9e93627cd 100644 --- a/Reflection/ReflectionParameter.php +++ b/Reflection/ReflectionParameter.php @@ -102,7 +102,7 @@ public function getDeclaringFunction(): ReflectionFunctionAbstract {} * Gets declaring class * * @link https://php.net/manual/en/reflectionparameter.getdeclaringclass.php - * @return ReflectionClass|null A {@see ReflectionClass} object or {@see null} if + * @return ReflectionClass|null A {@see ReflectionClass} object or {@see null} if * called on function. */ #[Pure] @@ -113,7 +113,7 @@ public function getDeclaringClass(): ?ReflectionClass {} * Gets the class type hinted for the parameter as a ReflectionClass object. * * @link https://php.net/manual/en/reflectionparameter.getclass.php - * @return ReflectionClass|null A {@see ReflectionClass} object. + * @return ReflectionClass|null A {@see ReflectionClass} object. * @see ReflectionParameter::getType() */ #[Deprecated(reason: "Use ReflectionParameter::getType() and the ReflectionType APIs should be used instead.", since: "8.0")] @@ -281,7 +281,7 @@ public function isPromoted(): bool {} * @template TAttributeClass of object * @param class-string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. - * @return list> + * @return ($name is null ? list> : list>) * @since 8.0 */ #[Pure] diff --git a/Reflection/ReflectionProperty.php b/Reflection/ReflectionProperty.php index 5f9da00d7..8e1f894fb 100644 --- a/Reflection/ReflectionProperty.php +++ b/Reflection/ReflectionProperty.php @@ -230,7 +230,7 @@ public function getModifiers(): int {} * Gets declaring class * * @link https://php.net/manual/en/reflectionproperty.getdeclaringclass.php - * @return ReflectionClass A {@see ReflectionClass} object. + * @return ReflectionClass A {@see ReflectionClass} object. */ #[Pure] #[TentativeType] @@ -359,7 +359,7 @@ public function getDefaultValue(): mixed {} * @template TAttributeClass of object * @param class-string|null $name Name of an attribute class * @param int $flags Сriteria by which the attribute is searched. - * @return list> + * @return ($name is null ? list> : list>) * @since 8.0 */ #[Pure] diff --git a/Reflection/Reflector.php b/Reflection/Reflector.php index da7f0b81e..831334d25 100644 --- a/Reflection/Reflector.php +++ b/Reflection/Reflector.php @@ -15,7 +15,8 @@ interface Reflector extends Stringable * * @link https://php.net/manual/en/reflector.export.php * @return string|null - * @removed 7.4 + * @deprecated 7.4 + * @removed 8.0 */ public static function export();