Test Failed
Pull Request — master (#1221)
by Aleksei
18:39
created

Scope::getParentActor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
    private ?Actor $parentActor = null;
17
18
    public function __construct(
19 1434
        private readonly ?string $scopeName = null,
20
    ) {}
21 1434
22
    public function getScopeName(): ?string
23 1212
    {
24
        return $this->scopeName;
25 1212
    }
26
27
    /**
28
     * Link the current scope with its parent scope and container.
29
     *
30
     * @throws NamedScopeDuplicationException
31
     */
32
    public function setParent(
33 341
        \Spiral\Core\Container $parent,
34
        self $parentScope,
35 341
        Actor $actor,
36 341
    ): void {
37 341
        $this->parent = $parent;
38
        $this->parentScope = $parentScope;
39
        $this->parentActor = $actor;
40 341
41 300
        // Check a scope with the same name is not already registered
42 300
        if ($this->scopeName !== null) {
43 300
            $tmp = $this;
44 300
            while ($tmp->parentScope !== null) {
45
                $tmp = $tmp->parentScope;
46
                $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName);
47
            }
48
        }
49 610
    }
50
51 610
    public function getParent(): ?\Spiral\Core\Container
52
    {
53
        return $this->parent;
54 1089
    }
55
56 1089
    /**
57
     * Return list of parent scope names.
58
     * The first element is the current scope name, and the next is the closest parent scope name...
59
     *
60
     * @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...
61
     */
62
    public function getParentScopeNames(): array
63
    {
64
        $result = [$this->scopeName];
65
66
        $parent = $this;
67
        while ($parent->parentScope !== null) {
68
            $parent = $parent->parentScope;
69
            $result[] = $parent->scopeName;
70
        }
71
72
        return $result;
73
    }
74
75
    public function getParentScope(): ?self
76
    {
77
        return $this->parentScope;
78 1065
    }
79
80 1065
    public function getParentActor(): ?Actor
81
    {
82
        return $this->parentActor;
83 606
    }
84
85 606
    public function destruct(): void
86 606
    {
87
        $this->parent = null;
88
        $this->parentScope = null;
89
    }
90
}
91