Test Failed
Pull Request — master (#870)
by Aleksei
08:56
created

Scope::getParentScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\Exception\Scope\NamedScopeDuplicationException;
8
9
/**
10
 * @internal
11
 */
12
final class Scope
13
{
14
    private ?\Spiral\Core\Container $parent = null;
15
    private ?self $parentScope = null;
16
17
    public function __construct(
18
        private readonly ?string $scopeName = null,
19
    ) {
20
    }
21
22
    public function getScopeName(): ?string
23
    {
24
        return $this->scopeName;
25
    }
26
27
    /**
28
     * Link the current scope with its parent scope and container.
29
     *
30
     * @throws NamedScopeDuplicationException
31
     */
32
    public function setParent(\Spiral\Core\Container $parent, self $parentScope): void
33
    {
34
        $this->parent = $parent;
35
        $this->parentScope = $parentScope;
36
37
        // Check a scope with the same name is not already registered
38
        if ($this->scopeName !== null) {
39
            $tmp = $this;
40
            while ($tmp->parentScope !== null) {
41
                $tmp = $tmp->parentScope;
42
                $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName);
43
            }
44
        }
45
    }
46
47
    public function getParent(): ?\Spiral\Core\Container
48
    {
49
        return $this->parent;
50
    }
51
52
    /**
53
     * Return list of parent scope names.
54
     * The first element is the current scope name, and the next is the closest parent scope name...
55
     *
56
     * @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...
57
     */
58
    public function getParentScopeNames(): array
59
    {
60
        $result = [$this->scopeName];
61
62
        $parent = $this;
63
        while ($parent->parentScope !== null) {
64
            $parent = $parent->parentScope;
65
            $result[] = $parent->scopeName;
66
        }
67
68
        return $result;
69
    }
70
71
    public function getParentScope(): ?self
72
    {
73
        return $this->parentScope;
74
    }
75
76
    public function destruct(): void
77
    {
78
        $this->parent = null;
79
        $this->parentScope = null;
80
    }
81
}
82