Skip to content

Commit

Permalink
refactoring closure reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 3, 2020
1 parent 35a7883 commit 8d8d620
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\ReflectsClosures;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\Process\Process;

class Event
{
use Macroable, ManagesFrequencies;
use Macroable, ManagesFrequencies, ReflectsClosures;

/**
* The command string.
Expand Down Expand Up @@ -812,12 +813,12 @@ public function onFailureWithOutput(Closure $callback, $onlyIfOutputExists = fal
*/
protected function withOutputCallback(Closure $callback, $onlyIfOutputExists = false)
{
return function () use ($callback, $onlyIfOutputExists) {
return function (Container $container) use ($callback, $onlyIfOutputExists) {
$output = $this->output && file_exists($this->output) ? file_get_contents($this->output) : '';

return $onlyIfOutputExists && empty($output)
? null
: $callback($output);
: $container->call($callback, ['output' => $output]);
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Support/Traits/ReflectsClosures.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ protected function closureParameterTypes(Closure $closure)
{
$reflection = new ReflectionFunction($closure);

return array_map(function (ReflectionParameter $parameter) {
return collect($reflection->getParameters())->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return;
return [$parameter->getName() => null];
}

return $parameter->getClass()->name ?? null;
}, $reflection->getParameters());
return [$parameter->getName() => $parameter->getClass()->name ?? null];
})->all();
}

/**
Expand All @@ -40,7 +40,7 @@ protected function closureParameterTypes(Closure $closure)
*/
protected function firstClosureParameterType(Closure $closure)
{
$types = $this->closureParameterTypes($closure);
$types = array_values($this->closureParameterTypes($closure));

if (! $types) {
throw new RuntimeException('The given Closure has no parameters.');
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/SupportReflectsClosuresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ReflectsClosuresClass

public static function reflect($closure)
{
return (new static)->closureParameterTypes($closure);
return array_values((new static)->closureParameterTypes($closure));
}

public static function reflectFirst($closure)
Expand Down

0 comments on commit 8d8d620

Please sign in to comment.