16
16
17
17
final class Products
18
18
{
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
+ }
20
25
21
26
public static function monitor (Amount $ amount = null ): Product
22
27
{
23
- return self ::$ items [__FUNCTION__ ] ??= Product:: create ($ amount ?? Amount:: taxable (MoneyFactory:: random ()) );
28
+ return self ::$ items [__FUNCTION__ ] ??= self :: generate ($ amount );
24
29
}
25
30
26
31
public static function keyboard (Amount $ amount = null ): Product
27
32
{
28
- return self ::$ items [__FUNCTION__ ] ??= Product:: create ($ amount ?? Amount:: taxable (MoneyFactory:: random ()) );
33
+ return self ::$ items [__FUNCTION__ ] ??= self :: generate ($ amount );
29
34
}
30
35
31
36
public static function mouse (Amount $ amount = null ): Product
32
37
{
33
- return self ::$ items [__FUNCTION__ ] ??= Product:: create ($ amount ?? Amount:: taxable (MoneyFactory:: random ()) );
38
+ return self ::$ items [__FUNCTION__ ] ??= self :: generate ($ amount );
34
39
}
35
40
36
41
public static function all (): array
37
42
{
38
43
$ reflection = new ReflectionClass (self ::class);
39
44
$ methods = $ reflection ->getMethods (ReflectionMethod::IS_STATIC );
40
45
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 ,
48
51
)
49
52
);
50
53
}
@@ -53,4 +56,11 @@ public static function clean(): void
53
56
{
54
57
self ::$ items = [];
55
58
}
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
+ }
56
66
}
0 commit comments