Test Failed
Pull Request — master (#952)
by Maxim
16:49 queued 07:48
created

Factory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
dl 0
loc 40
ccs 21
cts 21
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A getParametersCount() 0 3 1
A __toString() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Config;
6
7
/**
8
 * Make a value using a closure.
9
 */
10
final class Factory extends Binding
11
{
12
    use \Spiral\Core\Exception\Traits\ClosureRendererTrait;
0 ignored issues
show
Bug introduced by
The trait Spiral\Core\Exception\Traits\ClosureRendererTrait requires the property $class which is not provided by Spiral\Core\Config\Factory.
Loading history...
13
14
    public readonly \Closure $factory;
15
    private readonly int $parametersCount;
16
    private ?string $definition;
17
18 307
    public function __construct(
19
        callable $callable,
20
        public readonly bool $singleton = false,
21
    ) {
22 307
        $this->factory = $callable(...);
0 ignored issues
show
Bug introduced by
The property factory is declared read-only in Spiral\Core\Config\Factory.
Loading history...
23 307
        $this->parametersCount = (new \ReflectionFunction($this->factory))->getNumberOfParameters();
0 ignored issues
show
Bug introduced by
The property parametersCount is declared read-only in Spiral\Core\Config\Factory.
Loading history...
24
        /** @psalm-suppress TypeDoesNotContainType */
25 307
        $this->definition = match (true) {
26 307
            \is_string($callable) => $callable,
27 307
            \is_array($callable) => \sprintf(
28 307
                '%s::%s()',
29 307
                \is_object($callable[0]) ? $callable[0]::class : $callable[0],
30 307
                $callable[1],
31 307
            ),
32 307
            \is_object($callable) && $callable::class !== \Closure::class => 'object ' . $callable::class,
33 307
            default => null,
34 307
        };
35
    }
36
37 294
    public function __toString(): string
38
    {
39 294
        $this->definition ??= $this->renderClosureSignature(new \ReflectionFunction($this->factory));
40
41 294
        return \sprintf(
42 294
            'Factory from %s',
43 294
            $this->definition,
44 294
        );
45
    }
46
47 307
    public function getParametersCount(): int
48
    {
49 307
        return $this->parametersCount;
50
    }
51
}
52