Skip to content

Commit 8d587ae

Browse files
committed
Refactored FractionBuilder code for improved configurability and added RuntimeException handling
1 parent 69119a4 commit 8d587ae

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/FractionBuilder.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Illuminate\Foundation\Application;
1818
use InvalidArgumentException;
1919
use Laravel\SerializableClosure\SerializableClosure;
20+
use RuntimeException;
2021
use UnitEnum;
2122

2223
final class FractionBuilder implements Arrayable
@@ -60,14 +61,13 @@ public function __invoke(...$arguments): mixed
6061
'closure' => new SerializableClosure($this->closure),
6162
]);
6263

63-
$instance = $interpreter->then($this->then);
64-
65-
if ($this->queued || $this->deferred || $this->rescued) {
66-
// @phpstan-ignore-next-line
67-
$interpreter->configure($this->queued?->toArray() ?? $this->deferred?->toArray() ?? $this->rescued?->toArray());
64+
if ([$has, $configuration] = $this->configuration()) {
65+
if ($has) {
66+
$interpreter->configure($configuration);
67+
}
6868
}
6969

70-
$result = $instance->handle($this->application);
70+
$result = $interpreter->then($this->then)->handle($this->application);
7171

7272
if ($this->queued || $this->deferred) {
7373
return true;
@@ -88,4 +88,26 @@ public function toArray(): array
8888
'rescued' => $this->rescued,
8989
];
9090
}
91+
92+
/**
93+
* Determines which configurable should be applied.
94+
*/
95+
private function configuration(): array
96+
{
97+
foreach ([
98+
$this->queued,
99+
$this->deferred,
100+
$this->rescued,
101+
] as $instance) {
102+
if ($instance !== null) {
103+
if (! $instance instanceof Arrayable) {
104+
throw new RuntimeException(sprintf('The instance of [%s] should implement Arrayable', class_basename($instance)));
105+
}
106+
107+
return [true, $instance->toArray()];
108+
}
109+
}
110+
111+
return [false, []];
112+
}
91113
}

0 commit comments

Comments
 (0)