Passed
Push — master ( 8e874c...48b189 )
by Aleksei
05:44 queued 21s
created

Scope   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 75.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 24
c 1
b 0
f 0
dl 0
loc 75
ccs 22
cts 29
cp 0.7586
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getParentScope() 0 3 1
A setParent() 0 12 4
A getParentScopeNames() 0 11 2
A destruct() 0 4 1
A getParent() 0 3 1
A __construct() 0 3 1
A getScopeName() 0 3 1
A getParentFactory() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\Exception\Scope\NamedScopeDuplicationException;
8
use Spiral\Core\FactoryInterface;
9
10
/**
11
 * @internal
12
 */
13
final class Scope
14
{
15
    private ?\Spiral\Core\Container $parent = null;
16
    private ?self $parentScope = null;
17
    private ?FactoryInterface $parentFactory = null;
18
19 1306
    public function __construct(
20
        private readonly ?string $scopeName = null,
21
    ) {
22 1306
    }
23
24 1144
    public function getScopeName(): ?string
25
    {
26 1144
        return $this->scopeName;
27
    }
28
29
    /**
30
     * Link the current scope with its parent scope and container.
31
     *
32
     * @throws NamedScopeDuplicationException
33
     */
34 37
    public function setParent(\Spiral\Core\Container $parent, self $parentScope, FactoryInterface $factory): void
35
    {
36 37
        $this->parent = $parent;
37 37
        $this->parentScope = $parentScope;
38 37
        $this->parentFactory = $factory;
39
40
        // Check a scope with the same name is not already registered
41 37
        if ($this->scopeName !== null) {
42 18
            $tmp = $this;
43 18
            while ($tmp->parentScope !== null) {
44 18
                $tmp = $tmp->parentScope;
45 18
                $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName);
46
            }
47
        }
48
    }
49
50 549
    public function getParent(): ?\Spiral\Core\Container
51
    {
52 549
        return $this->parent;
53
    }
54
55 921
    public function getParentFactory(): ?FactoryInterface
56
    {
57 921
        return $this->parentFactory;
58
    }
59
60
    /**
61
     * Return list of parent scope names.
62
     * The first element is the current scope name, and the next is the closest parent scope name...
63
     *
64
     * @return array<int<0, max>, string|null>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int<0, max>, string|null> at position 2 could not be parsed: Expected '>' at position 2, but found 'int'.
Loading history...
65
     */
66
    public function getParentScopeNames(): array
67
    {
68
        $result = [$this->scopeName];
69
70
        $parent = $this;
71
        while ($parent->parentScope !== null) {
72
            $parent = $parent->parentScope;
73
            $result[] = $parent->scopeName;
74
        }
75
76
        return $result;
77
    }
78
79 884
    public function getParentScope(): ?self
80
    {
81 884
        return $this->parentScope;
82
    }
83
84 634
    public function destruct(): void
85
    {
86 634
        $this->parent = null;
87 634
        $this->parentScope = null;
88
    }
89
}
90