Reflector.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * @param bool $ascend
  55. * @return TAttribute|null
  56. */
  57. public static function getClassAttribute($objectOrClass, $attribute, $ascend = false)
  58. {
  59. return static::getClassAttributes($objectOrClass, $attribute, $ascend)->flatten()->first();
  60. }
  61. /**
  62. * Get the specified class attribute(s), optionally following an inheritance chain.
  63. *
  64. * @template TTarget of object
  65. * @template TAttribute of object
  66. *
  67. * @param TTarget|class-string<TTarget> $objectOrClass
  68. * @param class-string<TAttribute> $attribute
  69. * @param bool $includeParents
  70. * @return ($includeParents is true ? Collection<class-string<contravariant TTarget>, Collection<int, TAttribute>> : Collection<int, TAttribute>)
  71. */
  72. public static function getClassAttributes($objectOrClass, $attribute, $includeParents = false)
  73. {
  74. $reflectionClass = new ReflectionClass($objectOrClass);
  75. $attributes = [];
  76. do {
  77. $attributes[$reflectionClass->name] = new Collection(array_map(
  78. fn (ReflectionAttribute $reflectionAttribute) => $reflectionAttribute->newInstance(),
  79. $reflectionClass->getAttributes($attribute)
  80. ));
  81. } while ($includeParents && false !== $reflectionClass = $reflectionClass->getParentClass());
  82. return $includeParents ? new Collection($attributes) : array_first($attributes);
  83. }
  84. /**
  85. * Get the class name of the given parameter's type, if possible.
  86. *
  87. * @param \ReflectionParameter $parameter
  88. * @return string|null
  89. */
  90. public static function getParameterClassName($parameter)
  91. {
  92. $type = $parameter->getType();
  93. if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
  94. return;
  95. }
  96. return static::getTypeName($parameter, $type);
  97. }
  98. /**
  99. * Get the class names of the given parameter's type, including union types.
  100. *
  101. * @param \ReflectionParameter $parameter
  102. * @return array
  103. */
  104. public static function getParameterClassNames($parameter)
  105. {
  106. $type = $parameter->getType();
  107. if (! $type instanceof ReflectionUnionType) {
  108. return array_filter([static::getParameterClassName($parameter)]);
  109. }
  110. $unionTypes = [];
  111. foreach ($type->getTypes() as $listedType) {
  112. if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) {
  113. continue;
  114. }
  115. $unionTypes[] = static::getTypeName($parameter, $listedType);
  116. }
  117. return array_filter($unionTypes);
  118. }
  119. /**
  120. * Get the given type's class name.
  121. *
  122. * @param \ReflectionParameter $parameter
  123. * @param \ReflectionNamedType $type
  124. * @return string
  125. */
  126. protected static function getTypeName($parameter, $type)
  127. {
  128. $name = $type->getName();
  129. if (! is_null($class = $parameter->getDeclaringClass())) {
  130. if ($name === 'self') {
  131. return $class->getName();
  132. }
  133. if ($name === 'parent' && $parent = $class->getParentClass()) {
  134. return $parent->getName();
  135. }
  136. }
  137. return $name;
  138. }
  139. /**
  140. * Determine if the parameter's type is a subclass of the given type.
  141. *
  142. * @param \ReflectionParameter $parameter
  143. * @param string $className
  144. * @return bool
  145. */
  146. public static function isParameterSubclassOf($parameter, $className)
  147. {
  148. $paramClassName = static::getParameterClassName($parameter);
  149. return $paramClassName
  150. && (class_exists($paramClassName) || interface_exists($paramClassName))
  151. && (new ReflectionClass($paramClassName))->isSubclassOf($className);
  152. }
  153. /**
  154. * Determine if the parameter's type is a Backed Enum with a string backing type.
  155. *
  156. * @param \ReflectionParameter $parameter
  157. * @return bool
  158. */
  159. public static function isParameterBackedEnumWithStringBackingType($parameter)
  160. {
  161. if (! $parameter->getType() instanceof ReflectionNamedType) {
  162. return false;
  163. }
  164. $backedEnumClass = $parameter->getType()?->getName();
  165. if (is_null($backedEnumClass)) {
  166. return false;
  167. }
  168. if (enum_exists($backedEnumClass)) {
  169. $reflectionBackedEnum = new ReflectionEnum($backedEnumClass);
  170. return $reflectionBackedEnum->isBacked()
  171. && $reflectionBackedEnum->getBackingType()->getName() == 'string';
  172. }
  173. return false;
  174. }
  175. }