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

Factory::__construct()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.9332
c 1
b 0
f 0
cc 3
nc 1
nop 2
crap 3
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