ReflectsClosures.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Illuminate\Support\Traits;
  3. use Closure;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Reflector;
  6. use ReflectionFunction;
  7. use ReflectionIntersectionType;
  8. use ReflectionUnionType;
  9. use RuntimeException;
  10. trait ReflectsClosures
  11. {
  12. /**
  13. * Get the class name of the first parameter of the given Closure.
  14. *
  15. * @param \Closure $closure
  16. * @return string
  17. *
  18. * @throws \ReflectionException
  19. * @throws \RuntimeException
  20. */
  21. protected function firstClosureParameterType(Closure $closure)
  22. {
  23. $types = array_values($this->closureParameterTypes($closure));
  24. if (! $types) {
  25. throw new RuntimeException('The given Closure has no parameters.');
  26. }
  27. if ($types[0] === null) {
  28. throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
  29. }
  30. return $types[0];
  31. }
  32. /**
  33. * Get the class names of the first parameter of the given Closure, including union types.
  34. *
  35. * @param \Closure $closure
  36. * @return array
  37. *
  38. * @throws \ReflectionException
  39. * @throws \RuntimeException
  40. */
  41. protected function firstClosureParameterTypes(Closure $closure)
  42. {
  43. $reflection = new ReflectionFunction($closure);
  44. $types = (new Collection($reflection->getParameters()))
  45. ->mapWithKeys(function ($parameter) {
  46. if ($parameter->isVariadic()) {
  47. return [$parameter->getName() => null];
  48. }
  49. return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
  50. })
  51. ->filter()
  52. ->values()
  53. ->all();
  54. if (empty($types)) {
  55. throw new RuntimeException('The given Closure has no parameters.');
  56. }
  57. if (isset($types[0]) && empty($types[0])) {
  58. throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
  59. }
  60. return $types[0];
  61. }
  62. /**
  63. * Get the class names / types of the parameters of the given Closure.
  64. *
  65. * @param \Closure $closure
  66. * @return array
  67. *
  68. * @throws \ReflectionException
  69. */
  70. protected function closureParameterTypes(Closure $closure)
  71. {
  72. $reflection = new ReflectionFunction($closure);
  73. return (new Collection($reflection->getParameters()))
  74. ->mapWithKeys(function ($parameter) {
  75. if ($parameter->isVariadic()) {
  76. return [$parameter->getName() => null];
  77. }
  78. return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
  79. })
  80. ->all();
  81. }
  82. /**
  83. * Get the class names / types of the return type of the given Closure.
  84. *
  85. * @param \Closure $closure
  86. * @return list<class-string>
  87. *
  88. * @throws \ReflectionException
  89. */
  90. protected function closureReturnTypes(Closure $closure)
  91. {
  92. $reflection = new ReflectionFunction($closure);
  93. if ($reflection->getReturnType() === null ||
  94. $reflection->getReturnType() instanceof ReflectionIntersectionType) {
  95. return [];
  96. }
  97. $types = $reflection->getReturnType() instanceof ReflectionUnionType
  98. ? $reflection->getReturnType()->getTypes()
  99. : [$reflection->getReturnType()];
  100. return (new Collection($types))
  101. ->reject(fn ($type) => $type->isBuiltin())
  102. ->reject(fn ($type) => in_array($type->getName(), ['static', 'self']))
  103. ->map(fn ($type) => $type->getName())
  104. ->values()
  105. ->all();
  106. }
  107. }