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