Skip to content

Commit

Permalink
Upgrade LazyBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Sep 27, 2023
1 parent 12a1f90 commit f91e6e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/Collection/LazyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace h4kuna\DataType\Collection;

use Closure;
use h4kuna\DataType\Exceptions\InvalidStateException;

/**
* @template T
* @phpstan-type formatCallback callable(static $self): T
* @phpstan-type defaultCallback callable(string|int $key, static $self, mixed $options): T
* @phpstan-type formatCallback Closure(static $self): T
* @phpstan-type defaultCallback Closure(string|int $key, static $self, mixed $options): T
*/
class LazyBuilder
{
/** @var ?defaultCallback */
private $default = null;
private ?Closure $default = null;

/**
* @var array<string|int, T>
Expand All @@ -35,7 +36,7 @@ public function __construct(
*/
public function add(string|int $key, $setup): void
{
if (is_callable($setup)) {
if (self::isCallable($setup)) {
$this->factories[$key] = $setup;
unset($this->formats[$key]);
} else {
Expand All @@ -58,7 +59,7 @@ public function get(string|int $key)
if (isset($this->formats[$key]) === false) {
if (isset($this->factories[$key])) {
$service = $this->factories[$key];
$format = is_callable($service) ? $service($this) : $service;
$format = self::isCallable($service) ? $service($this) : $service;
} else {
$format = $this->getDefault()($key, $this, null);
}
Expand All @@ -73,7 +74,7 @@ public function get(string|int $key)
/**
* @return defaultCallback
*/
public function getDefault(): callable
public function getDefault(): Closure
{
if ($this->default === null) {
$this->default = $this->createDefaultCallback();
Expand All @@ -90,7 +91,7 @@ public function setDefault($default): void
{
if ($this->default !== null) {
throw new InvalidStateException('Default format could be setup only onetime.');
} elseif (is_callable($default) === false) {
} elseif (self::isCallable($default) === false) {
$default = $this->createDefaultCallback($default);
}

Expand All @@ -102,7 +103,7 @@ public function setDefault($default): void
* @param T|null $object
* @return defaultCallback
*/
protected function createDefaultCallback($object = null): callable
protected function createDefaultCallback($object = null): Closure
{
if ($object === null) {
return static fn (string|int $key
Expand All @@ -111,4 +112,13 @@ protected function createDefaultCallback($object = null): callable
throw new InvalidStateException('Default format is not setup.');
}


/**
* @return ($object is Closure ? true : false)
*/
private static function isCallable(mixed $object): bool
{
return $object instanceof Closure; // keep Closure instead of is_callable, don't support class::__invoke()
}

}
2 changes: 1 addition & 1 deletion tests/src/Unit/Collection/LazyBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class LazyBuilderTest extends TestCase
public function testDefaultIsNotDefinedFailed(): void
{
Assert::throws(fn () => (new LazyBuilder())->get('any'), InvalidStateException::class);
Assert::throws(fn () => (new LazyBuilder())->setDefault('any'), InvalidStateException::class);
Assert::throws(fn () => (new LazyBuilder())->setDefault('any'), InvalidStateException::class); // @phpstan-ignore-line

$lazyBuilder = new LazyBuilder();
$lazyBuilder->setDefault(fn () => 'hello');;
Expand Down

0 comments on commit f91e6e6

Please sign in to comment.