Test Failed
Push — master ( 16c39a...9c7d46 )
by Kirill
07:07
created

Context::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend\Context;
11
12
use Railt\Io\Readable;
13
use Railt\SDL\Frontend\Type\TypeName;
14
use Railt\SDL\Frontend\Type\TypeNameInterface;
15
16
/**
17
 * Class Context
18
 */
19
class Context implements ContextInterface, \Countable
20
{
21
    /**
22
     * @var TypeNameInterface
23
     */
24
    private $current;
25
26
    /**
27
     * @var \SplStack|TypeNameInterface[]
28
     */
29
    private $stack;
30
31
    /**
32
     * @var Readable
33
     */
34
    private $file;
35
36
    /**
37
     * Context constructor.
38
     * @param Readable $file
39
     */
40
    public function __construct(Readable $file)
41
    {
42
        $this->file = $file;
43
        $this->stack = new \SplStack();
44
        $this->current = TypeName::global();
45
    }
46
47
    /**
48
     * @return Readable
49
     */
50
    public function getFile(): Readable
51
    {
52
        return $this->file;
53
    }
54
55
    /**
56
     * @return TypeNameInterface
57
     */
58
    public function current(): TypeNameInterface
59
    {
60
        return $this->current;
61
    }
62
63
    /**
64
     * @return int
65
     */
66
    public function count(): int
67
    {
68
        return $this->stack->count();
69
    }
70
71
    /**
72
     * @param TypeNameInterface $name
73
     * @return TypeNameInterface
74
     */
75
    public function create(TypeNameInterface $name): TypeNameInterface
76
    {
77
        $this->current = $name->in($this->current);
78
79
        $this->stack->push($this->current);
80
81
        return $this->current;
82
    }
83
84
    /**
85
     * @return TypeNameInterface
86
     */
87
    public function close(): TypeNameInterface
88
    {
89
        $current = $this->stack->pop();
90
91
        $this->current = $this->stack->count() ? $this->stack->top() : TypeName::global();
92
93
        return $current;
94
    }
95
}
96