Reflector.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Illuminate\Support;
  3. use ReflectionClass;
  4. use ReflectionEnum;
  5. use ReflectionMethod;
  6. use ReflectionNamedType;
  7. use ReflectionUnionType;
  8. class Reflector
  9. {
  10. /**
  11. * This is a PHP 7.4 compatible implementation of is_callable.
  12. *
  13. * @param mixed $var
  14. * @param bool $syntaxOnly
  15. * @return bool
  16. */
  17. public static function isCallable($var, $syntaxOnly = false)
  18. {
  19. if (! is_array($var)) {
  20. return is_callable($var, $syntaxOnly);
  21. }
  22. if (! isset($var[0], $var[1]) || ! is_string($var[1] ?? null)) {
  23. return false;
  24. }
  25. if ($syntaxOnly &&
  26. (is_string($var[0]) || is_object($var[0])) &&
  27. is_string($var[1])) {
  28. return true;
  29. }
  30. $class = is_object($var[0]) ? get_class($var[0]) : $var[0];
  31. $method = $var[1];
  32. if (! class_exists($class)) {
  33. return false;
  34. }
  35. if (method_exists($class, $method)) {
  36. return (new ReflectionMethod($class, $method))->isPublic();
  37. }
  38. if (is_object($var[0]) && method_exists($class, '__call')) {
  39. return (new ReflectionMethod($class, '__call'))->isPublic();
  40. }
  41. if (! is_object($var[0]) && method_exists($class, '__callStatic')) {
  42. return (new ReflectionMethod($class, '__callStatic'))->isPublic();
  43. }
  44. return false;
  45. }
  46. /**
  47. * Get the class name of the given parameter's type, if possible.
  48. *
  49. * @param \ReflectionParameter $parameter
  50. * @return string|null
  51. */
  52. public static function getParameterClassName($parameter)
  53. {
  54. $type = $parameter->getType();
  55. if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
  56. return;
  57. }
  58. return static::getTypeName($parameter, $type);
  59. }
  60. /**
  61. * Get the class names of the given parameter's type, including union types.
  62. *
  63. * @param \ReflectionParameter $parameter
  64. * @return array
  65. */
  66. public static function getParameterClassNames($parameter)
  67. {
  68. $type = $parameter->getType();
  69. if (! $type instanceof ReflectionUnionType) {
  70. return array_filter([static::getParameterClassName($parameter)]);
  71. }
  72. $unionTypes = [];
  73. foreach ($type->getTypes() as $listedType) {
  74. if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) {
  75. continue;
  76. }
  77. $unionTypes[] = static::getTypeName($parameter, $listedType);
  78. }
  79. return array_filter($unionTypes);
  80. }
  81. /**
  82. * Get the given type's class name.
  83. *
  84. * @param \ReflectionParameter $parameter
  85. * @param \ReflectionNamedType $type
  86. * @return string
  87. */
  88. protected static function getTypeName($parameter, $type)
  89. {
  90. $name = $type->getName();
  91. if (! is_null($class = $parameter->getDeclaringClass())) {
  92. if ($name === 'self') {
  93. return $class->getName();
  94. }
  95. if ($name === 'parent' && $parent = $class->getParentClass()) {
  96. return $parent->getName();
  97. }
  98. }
  99. return $name;
  100. }
  101. /**
  102. * Determine if the parameter's type is a subclass of the given type.
  103. *
  104. * @param \ReflectionParameter $parameter
  105. * @param string $className
  106. * @return bool
  107. */
  108. public static function isParameterSubclassOf($parameter, $className)
  109. {
  110. $paramClassName = static::getParameterClassName($parameter);
  111. return $paramClassName
  112. && (class_exists($paramClassName) || interface_exists($paramClassName))
  113. && (new ReflectionClass($paramClassName))->isSubclassOf($className);
  114. }
  115. /**
  116. * Determine if the parameter's type is a Backed Enum with a string backing type.
  117. *
  118. * @param \ReflectionParameter $parameter
  119. * @return bool
  120. */
  121. public static function isParameterBackedEnumWithStringBackingType($parameter)
  122. {
  123. if (! $parameter->getType() instanceof ReflectionNamedType) {
  124. return false;
  125. }
  126. $backedEnumClass = $parameter->getType()?->getName();
  127. if (is_null($backedEnumClass)) {
  128. return false;
  129. }
  130. if (enum_exists($backedEnumClass)) {
  131. $reflectionBackedEnum = new ReflectionEnum($backedEnumClass);
  132. return $reflectionBackedEnum->isBacked()
  133. && $reflectionBackedEnum->getBackingType()->getName() == 'string';
  134. }
  135. return false;
  136. }
  137. }