Reflector.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Illuminate\Support;
  3. use ReflectionAttribute;
  4. use ReflectionClass;
  5. use ReflectionEnum;
  6. use ReflectionMethod;
  7. use ReflectionNamedType;
  8. use ReflectionUnionType;
  9. class Reflector
  10. {
  11. /**
  12. * This is a PHP 7.4 compatible implementation of is_callable.
  13. *
  14. * @param mixed $var
  15. * @param bool $syntaxOnly
  16. * @return bool
  17. */
  18. public static function isCallable($var, $syntaxOnly = false)
  19. {
  20. if (! is_array($var)) {
  21. return is_callable($var, $syntaxOnly);
  22. }
  23. if (! isset($var[0], $var[1]) || ! is_string($var[1] ?? null)) {
  24. return false;
  25. }
  26. if ($syntaxOnly &&
  27. (is_string($var[0]) || is_object($var[0])) &&
  28. is_string($var[1])) {
  29. return true;
  30. }
  31. $class = is_object($var[0]) ? get_class($var[0]) : $var[0];
  32. $method = $var[1];
  33. if (! class_exists($class)) {
  34. return false;
  35. }
  36. if (method_exists($class, $method)) {
  37. return (new ReflectionMethod($class, $method))->isPublic();
  38. }
  39. if (is_object($var[0]) && method_exists($class, '__call')) {
  40. return (new ReflectionMethod($class, '__call'))->isPublic();
  41. }
  42. if (! is_object($var[0]) && method_exists($class, '__callStatic')) {
  43. return (new ReflectionMethod($class, '__callStatic'))->isPublic();
  44. }
  45. return false;
  46. }
  47. /**
  48. * Get the specified class attribute, optionally following an inheritance chain.
  49. *
  50. * @template TAttribute of object
  51. *
  52. * @param object|class-string $objectOrClass
  53. * @param class-string<TAttribute> $attribute
  54. * @return TAttribute|null
  55. */
  56. public static function getClassAttribute($objectOrClass, $attribute, $ascend = false)
  57. {
  58. return static::getClassAttributes($objectOrClass, $attribute, $ascend)->flatten()->first();
  59. }
  60. /**
  61. * Get the specified class attribute(s), optionally following an inheritance chain.
  62. *
  63. * @template TTarget of object
  64. * @template TAttribute of object
  65. *
  66. * @param TTarget|class-string<TTarget> $objectOrClass
  67. * @param class-string<TAttribute> $attribute
  68. * @return ($includeParents is true ? Collection<class-string<contravariant TTarget>, Collection<int, TAttribute>> : Collection<int, TAttribute>)
  69. */
  70. public static function getClassAttributes($objectOrClass, $attribute, $includeParents = false)
  71. {
  72. $reflectionClass = new ReflectionClass($objectOrClass);
  73. $attributes = [];
  74. do {
  75. $attributes[$reflectionClass->name] = new Collection(array_map(
  76. fn (ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance(),
  77. $reflectionClass->getAttributes($attribute)
  78. ));
  79. } while ($includeParents && false !== $reflectionClass = $reflectionClass->getParentClass());
  80. return $includeParents ? new Collection($attributes) : array_first($attributes);
  81. }
  82. /**
  83. * Get the class name of the given parameter's type, if possible.
  84. *
  85. * @param \ReflectionParameter $parameter
  86. * @return string|null
  87. */
  88. public static function getParameterClassName($parameter)
  89. {
  90. $type = $parameter->getType();
  91. if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
  92. return;
  93. }
  94. return static::getTypeName($parameter, $type);
  95. }
  96. /**
  97. * Get the class names of the given parameter's type, including union types.
  98. *
  99. * @param \ReflectionParameter $parameter
  100. * @return array
  101. */
  102. public static function getParameterClassNames($parameter)
  103. {
  104. $type = $parameter->getType();
  105. if (! $type instanceof ReflectionUnionType) {
  106. return array_filter([static::getParameterClassName($parameter)]);
  107. }
  108. $unionTypes = [];
  109. foreach ($type->getTypes() as $listedType) {
  110. if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) {
  111. continue;
  112. }
  113. $unionTypes[] = static::getTypeName($parameter, $listedType);
  114. }
  115. return array_filter($unionTypes);
  116. }
  117. /**
  118. * Get the given type's class name.
  119. *
  120. * @param \ReflectionParameter $parameter
  121. * @param \ReflectionNamedType $type
  122. * @return string
  123. */
  124. protected static function getTypeName($parameter, $type)
  125. {
  126. $name = $type->getName();
  127. if (! is_null($class = $parameter->getDeclaringClass())) {
  128. if ($name === 'self') {
  129. return $class->getName();
  130. }
  131. if ($name === 'parent' && $parent = $class->getParentClass()) {
  132. return $parent->getName();
  133. }
  134. }
  135. return $name;
  136. }
  137. /**
  138. * Determine if the parameter's type is a subclass of the given type.
  139. *
  140. * @param \ReflectionParameter $parameter
  141. * @param string $className
  142. * @return bool
  143. */
  144. public static function isParameterSubclassOf($parameter, $className)
  145. {
  146. $paramClassName = static::getParameterClassName($parameter);
  147. return $paramClassName
  148. && (class_exists($paramClassName) || interface_exists($paramClassName))
  149. && (new ReflectionClass($paramClassName))->isSubclassOf($className);
  150. }
  151. /**
  152. * Determine if the parameter's type is a Backed Enum with a string backing type.
  153. *
  154. * @param \ReflectionParameter $parameter
  155. * @return bool
  156. */
  157. public static function isParameterBackedEnumWithStringBackingType($parameter)
  158. {
  159. if (! $parameter->getType() instanceof ReflectionNamedType) {
  160. return false;
  161. }
  162. $backedEnumClass = $parameter->getType()?->getName();
  163. if (is_null($backedEnumClass)) {
  164. return false;
  165. }
  166. if (enum_exists($backedEnumClass)) {
  167. $reflectionBackedEnum = new ReflectionEnum($backedEnumClass);
  168. return $reflectionBackedEnum->isBacked()
  169. && $reflectionBackedEnum->getBackingType()->getName() == 'string';
  170. }
  171. return false;
  172. }
  173. }