ValueFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 2
A __construct() 0 3 1
1
<?php
2
3
namespace Zenstruck\Callback;
4
5
use Zenstruck\Callback;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class ValueFactory
11
{
12
    /** @var callable */
13
    private $factory;
14
15
    /**
16
     * @param callable<string|array|Argument|null> $factory
17
     */
18
    public function __construct(callable $factory)
19
    {
20
        $this->factory = $factory;
21
    }
22
23
    public function __invoke(Argument $argument)
24
    {
25
        $stringTypeFactory = Parameter::factory(function() use ($argument) {
26
            if ($argument->isUnionType()) {
27
                throw new \LogicException(\sprintf('ValueFactory does not support union types. Inject "%s" instead.', Argument::class));
28
            }
29
30
            return $argument->type();
31
        });
32
33
        return Callback::createFor($this->factory)
34
            ->invoke(Parameter::union(
35
                Parameter::typed(Argument::class, $argument),
36
                Parameter::typed('array', $argument->types()),
37
                Parameter::typed('string', $stringTypeFactory),
38
                Parameter::untyped($stringTypeFactory)
39
            )->optional())
40
        ;
41
    }
42
}
43