Skip to content

Commit eac0be4

Browse files
committed
feat: improve products generator
1 parent ad1e83f commit eac0be4

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

tests/Utils/Products.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,38 @@
1616

1717
final class Products
1818
{
19-
private static array $items = [];
19+
public static array $items = [];
20+
21+
public static function generate(?Amount $amount = null): Product
22+
{
23+
return Product::create($amount ?? Amount::taxable(MoneyFactory::random()));
24+
}
2025

2126
public static function monitor(Amount $amount = null): Product
2227
{
23-
return self::$items[__FUNCTION__] ??= Product::create($amount ?? Amount::taxable(MoneyFactory::random()));
28+
return self::$items[__FUNCTION__] ??= self::generate($amount);
2429
}
2530

2631
public static function keyboard(Amount $amount = null): Product
2732
{
28-
return self::$items[__FUNCTION__] ??= Product::create($amount ?? Amount::taxable(MoneyFactory::random()));
33+
return self::$items[__FUNCTION__] ??= self::generate($amount);
2934
}
3035

3136
public static function mouse(Amount $amount = null): Product
3237
{
33-
return self::$items[__FUNCTION__] ??= Product::create($amount ?? Amount::taxable(MoneyFactory::random()));
38+
return self::$items[__FUNCTION__] ??= self::generate($amount);
3439
}
3540

3641
public static function all(): array
3742
{
3843
$reflection = new ReflectionClass(self::class);
3944
$methods = $reflection->getMethods(ReflectionMethod::IS_STATIC);
4045

41-
return filter(
42-
static fn(?Product $product) => null != $product,
43-
map(
44-
static fn(
45-
ReflectionMethod $method
46-
) => $method->getReturnType()->getName() === Product::class ? $method->invoke(null) : null,
47-
$methods
46+
return map(
47+
static fn(ReflectionMethod $method): Product => $method->invoke(null),
48+
filter(
49+
self::onlyNamedProductGenerator(),
50+
$methods,
4851
)
4952
);
5053
}
@@ -53,4 +56,11 @@ public static function clean(): void
5356
{
5457
self::$items = [];
5558
}
59+
60+
private static function onlyNamedProductGenerator(): callable
61+
{
62+
return static function (ReflectionMethod $method): bool {
63+
return $method->getName() !== 'generate' && $method->getReturnType()->getName() === Product::class;
64+
};
65+
}
5666
}

0 commit comments

Comments
 (0)