MarshalDelegatorsConfig::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 20
ccs 14
cts 15
cp 0.9333
rs 9.8666
cc 3
nc 3
nop 1
crap 3.0026
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Container;
6
7
use Psr\Container\ContainerInterface;
8
use function is_callable;
9
10
class MarshalDelegatorsConfig
11
{
12 16
    public function __invoke(ContainerConfig $dependencies): ContainerConfig
13
    {
14 16
        foreach ($dependencies->get('delegators') as $service => $delegatorNames) {
15 2
            $factory = $this->delegateFactories($dependencies, $service);
16 2
            if (!is_callable($factory)) {
17
                continue;
18
            }
19 2
            $dependencies->set(ContainerDelegatorFactory::class, static function () use ($delegatorNames, $factory) {
20 1
                return new ContainerDelegatorFactory($delegatorNames, $factory);
0 ignored issues
show
Bug introduced by
It seems like $factory can also be of type null; however, parameter $factory of Antidot\Container\Contai...rFactory::__construct() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
                return new ContainerDelegatorFactory($delegatorNames, /** @scrutinizer ignore-type */ $factory);
Loading history...
21 2
            });
22 2
            $dependencies->set(
23 2
                $service,
24 2
                static function (ContainerInterface $container) use ($service) {
25 1
                    $callable = $container->get(ContainerDelegatorFactory::class);
26 1
                    return $callable($container, $service);
27 2
                }
28
            );
29
        }
30
31 16
        return $dependencies;
32
    }
33
34 2
    private function delegateFactories(
35
        ContainerConfig $dependencies,
36
        string $service
37
    ): ?callable {
38 2
        if (false === $dependencies->has($service)) {
39
            return null;
40
        }
41
        // Marshal from factory
42 2
        $serviceFactory = $dependencies->get($service);
43 2
        return static function (ContainerInterface $container) use ($service, $serviceFactory) {
44 1
            return is_callable($serviceFactory)
45 1
                ? $serviceFactory($container, $service)
46 1
                : (new $serviceFactory())($container, $service);
47 2
        };
48
    }
49
}
50