Passed
Push — master ( 41e710...91ffae )
by Aleksei
06:19 queued 21s
created

Factory::validateNewInstance()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.6111
cc 5
nc 6
nop 3
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\FactoryInterface;
8
use Spiral\Core\Internal\Common\DestructorTrait;
9
use Spiral\Core\Internal\Common\Registry;
10
use Spiral\Core\Internal\Factory\Ctx;
11
12
/**
13
 * @internal
14
 */
15
final class Factory implements FactoryInterface
16
{
17
    use DestructorTrait;
18
19
    private State $state;
20
    private Scope $scope;
21
    private Actor $actor;
22
23 1443
    public function __construct(Registry $constructor)
24
    {
25 1443
        $constructor->set('factory', $this);
26
27 1443
        $this->state = $constructor->get('state', State::class);
28 1443
        $this->scope = $constructor->get('scope', Scope::class);
29 1443
        $this->actor = $constructor->get('actor', Actor::class);
30
    }
31
32
    /**
33
     * @param \Stringable|string|null $context Related to parameter caused injection if any.
34
     *
35
     * @throws \Throwable
36
     */
37 1238
    public function make(
38
        string $alias,
39
        array $parameters = [],
40
        \Stringable|string|null $context = null,
41
        ?Tracer $tracer = null,
42
    ): mixed {
43 1238
        if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) {
44 638
            return $this->state->singletons[$alias];
45
        }
46
47
48 1238
        $this->actor->resolveType($alias, $binding, $singleton, $injector, $actor, false);
49 1237
        if ($parameters === [] && $singleton !== null) {
50 99
            return $singleton;
51
        }
52
53 1237
        $tracer ??= new Tracer();
54
55
        // Resolve without binding
56 1237
        if ($binding === null) {
57 1080
            $tracer->push(
58 1080
                false,
59 1080
                action: 'autowire',
0 ignored issues
show
Bug introduced by
'autowire' of type string is incompatible with the type boolean expected by parameter $nextLevel of Spiral\Core\Internal\Tracer::push(). ( Ignorable by Annotation )

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

59
                /** @scrutinizer ignore-type */ action: 'autowire',
Loading history...
60 1080
                alias: $alias,
61 1080
                scope: $this->scope->getScopeName(),
0 ignored issues
show
Bug introduced by
$this->scope->getScopeName() of type null|string is incompatible with the type boolean expected by parameter $nextLevel of Spiral\Core\Internal\Tracer::push(). ( Ignorable by Annotation )

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

61
                /** @scrutinizer ignore-type */ scope: $this->scope->getScopeName(),
Loading history...
62 1080
                context: $context,
0 ignored issues
show
Bug introduced by
$context of type Stringable|null|string is incompatible with the type boolean expected by parameter $nextLevel of Spiral\Core\Internal\Tracer::push(). ( Ignorable by Annotation )

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

62
                /** @scrutinizer ignore-type */ context: $context,
Loading history...
63 1080
            );
64
            try {
65
                // No direct instructions how to construct class, make is automatically
66 1080
                return $this->actor->autowire(
67 1080
                    new Ctx(alias: $alias, class: $alias, context: $context, singleton: $parameters === [] ? null : false),
68 1080
                    $parameters,
69 1080
                    $actor,
70 1080
                    $tracer,
71 1080
                );
72
            } finally {
73 1080
                $tracer->pop(false);
74
            }
75
        }
76
77
        try {
78 1108
            $tracer->push(
79 1108
                false,
80 1108
                action: 'resolve from binding',
81 1108
                alias: $alias,
82 1108
                scope: $this->scope->getScopeName(),
83 1108
                context: $context,
84 1108
                binding: $binding,
85 1108
            );
86 1108
            $tracer->push(true);
87
88 1108
            $actor->disableBinding($alias);
89 1108
            return $actor->resolveBinding($binding, $alias, $context, $parameters, $tracer);
90
        } finally {
91 1108
            $actor->enableBinding($alias, $binding);
92 1108
            $tracer->pop(true);
93 1108
            $tracer->pop(false);
94
        }
95
    }
96
}
97