Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

Scope::leave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Twig\NodeVisitor;
12
13
/**
14
 * Modified version of \Symfony\Bridge\Twig\NodeVisitor\Scope.
15
 *
16
 * @author Jean-François Simon <[email protected]>
17
 * @author Yaroslav Honcharuk <[email protected]>
18
 */
19
class Scope
20
{
21
    /**
22
     * @var self
23
     */
24
    private $parent;
25
26
    /**
27
     * @var array
28
     */
29
    private $data = [];
30
31
    /**
32
     * @var bool
33
     */
34
    private $left = false;
35
36
    /**
37
     * @param self|null $parent
38
     */
39 10
    public function __construct(self $parent = null)
40
    {
41 10
        $this->parent = $parent;
42 10
    }
43
44
    /**
45
     * Opens a new child scope.
46
     *
47
     * @return self
48
     */
49 7
    public function enter()
50
    {
51 7
        return new self($this);
52
    }
53
54
    /**
55
     * Closes current scope and returns parent one.
56
     *
57
     * @return self|null
58
     */
59 5
    public function leave()
60
    {
61 5
        $this->left = true;
62
63 5
        return $this->parent;
64
    }
65
66
    /**
67
     * Stores data into current scope.
68
     *
69
     * @param string $key
70
     * @param mixed  $value
71
     *
72
     * @return $this
73
     *
74
     * @throws \LogicException
75
     */
76 7
    public function set($key, $value)
77
    {
78 7
        if ($this->left) {
79 1
            throw new \LogicException('Left scope is not mutable.');
80
        }
81
82 6
        $this->data[$key] = $value;
83
84 6
        return $this;
85
    }
86
87
    /**
88
     * Tests if a data is visible from current scope.
89
     *
90
     * @param string $key
91
     *
92
     * @return bool
93
     */
94 6
    public function has($key)
95
    {
96 6
        if (array_key_exists($key, $this->data)) {
97 5
            return true;
98
        }
99
100 6
        return false;
101
    }
102
103
    /**
104
     * Returns data visible from current scope.
105
     *
106
     * @param string $key
107
     * @param mixed  $default
108
     *
109
     * @return mixed
110
     */
111 7
    public function get($key, $default = null)
112
    {
113 7
        if (array_key_exists($key, $this->data)) {
114 6
            return $this->data[$key];
115
        }
116
117 5
        return $default;
118
    }
119
}
120