Test Failed
Push — master ( 872399...b79e07 )
by Kirill
02:27
created

AbstractDefinition::getDictionary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Reflection;
11
12
use Railt\Io\Exception\ExternalFileException;
13
use Railt\Io\Readable;
14
use Railt\Reflection\Common\Jsonable;
15
use Railt\Reflection\Common\Serializable;
16
use Railt\Reflection\Contracts\Definition;
17
use Railt\Reflection\Contracts\Definition\TypeDefinition;
18
use Railt\Reflection\Contracts\Dictionary;
19
use Railt\Reflection\Contracts\Document as DocumentInterface;
20
use Railt\Reflection\Contracts\Invocation\TypeInvocation;
21
use Railt\Reflection\Contracts\Type as TypeInterface;
22
use Railt\Reflection\Exception\ReflectionException;
23
24
/**
25
 * Class AbstractDefinition
26
 */
27
abstract class AbstractDefinition implements Definition, \JsonSerializable
28
{
29
    use Jsonable;
30
    use Serializable;
31
32
    /**
33
     * @var Document
34
     */
35
    protected $document;
36
37
    /**
38
     * @var int
39
     */
40
    protected $offset = 0;
41
42
    /**
43
     * @var int|null
44
     */
45
    protected $line;
46
47
    /**
48
     * @var int|null
49
     */
50
    protected $column;
51
52
    /**
53
     * AbstractDefinition constructor.
54
     * @param Document $document
55
     */
56 9
    public function __construct(Document $document)
57
    {
58 9
        $this->document = $document;
59 9
    }
60
61
    /**
62
     * @param TypeInterface|string $type
63
     * @return bool
64
     * @throws ReflectionException
65
     */
66
    public static function typeOf($type): bool
67
    {
68
        switch (true) {
69
            case $type instanceof TypeInterface:
70
                break;
71
            case \is_string($type):
72
                $type = Type::of($type);
73
                break;
74
            default:
75
                throw new ReflectionException('Unsupported argument');
76
        }
77
78
        return static::getType()->instanceOf($type);
79
    }
80
81
    /**
82
     * @param int $offset
83
     * @return Definition|TypeDefinition|TypeInvocation|$this
84
     */
85 1
    public function withOffset(int $offset): Definition
86
    {
87 1
        $this->offset = $offset;
88
89 1
        $this->line = $this->column = null;
90
91 1
        return $this;
92
    }
93
94
    /**
95
     * @param int $line
96
     * @return Definition|TypeDefinition|TypeInvocation|$this
97
     */
98 9
    public function withLine(?int $line): Definition
99
    {
100 9
        $this->line = \is_int($line) ? \max(1, $line) : $line;
101
102 9
        return $this;
103
    }
104
105
    /**
106
     * @param int $column
107
     * @return Definition|TypeDefinition|TypeInvocation|$this
108
     */
109
    public function withColumn(?int $column): Definition
110
    {
111
        $this->column = \is_int($column) ? \max(1, $column) : $column;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return DocumentInterface
118
     */
119 17
    public function getDocument(): DocumentInterface
120
    {
121 17
        return $this->document;
122
    }
123
124
    /**
125
     * @return Dictionary
126
     */
127 8
    public function getDictionary(): Dictionary
128
    {
129 8
        return $this->document->getDictionary();
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function __toString(): string
136
    {
137
        return \sprintf('?<%s>', static::getType());
138
    }
139
140
    /**
141
     * @param string|TypeDefinition $type
142
     * @return string|null
143
     * @throws ExternalFileException
144
     */
145 25
    protected function nameOf($type): ?string
146
    {
147
        switch (true) {
148 25
            case \is_string($type):
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
149 9
                return $type;
150
151 25
            case $type instanceof TypeDefinition:
152 25
                return $type->getName();
153
        }
154
155
        throw (new ReflectionException('Unsupported argument'))->throwsIn($this->getFile(), $this->getLine(),
156
                $this->getColumn());
157
    }
158
159
    /**
160
     * @return Readable
161
     */
162 1
    public function getFile(): Readable
163
    {
164 1
        return $this->document->getFile();
165
    }
166
167
    /**
168
     * @return int
169
     */
170
    public function getLine(): int
171
    {
172
        if ($this->line === null) {
173
            $this->line = $this->getFile()->getPosition($this->offset)->getLine();
174
        }
175
176
        return $this->line;
177
    }
178
179
    /**
180
     * @return int
181
     */
182 1
    public function getColumn(): int
183
    {
184 1
        if ($this->column === null) {
185 1
            $this->column = $this->getFile()->getPosition($this->offset)->getColumn();
186
        }
187
188 1
        return $this->column;
189
    }
190
191
    /**
192
     * @param string|TypeDefinition|null $type
193
     * @return null|TypeDefinition
194
     * @throws ExternalFileException
195
     */
196
    protected function fetchOrNull($type): ?TypeDefinition
197
    {
198
        return $type === null ? $this->fetch($type) : null;
199
    }
200
201
    /**
202
     * @param string|TypeDefinition $type
203
     * @return TypeDefinition
204
     * @throws ExternalFileException
205
     */
206 10
    protected function fetch($type): TypeDefinition
207
    {
208
        switch (true) {
209 10
            case \is_string($type):
210 10
                return $this->document->getDictionary()->get($type, $this);
211
212 8
            case $type instanceof TypeDefinition:
213 8
                return $type;
214
        }
215
216
        throw (new ReflectionException('Unsupported argument'))->throwsIn($this->getFile(), $this->getLine(),
217
                $this->getColumn());
218
    }
219
220
    /**
221
     * @param \Throwable $error
222
     * @return ExternalFileException
223
     */
224
    protected function error(\Throwable $error): ExternalFileException
225
    {
226
        if (! $error instanceof ExternalFileException) {
227
            $error = new ReflectionException($error->getMessage(), $error->getCode(), $error);
228
        }
229
230
        return $error->throwsIn($this->getFile(), $this->getLine(), $this->getColumn());
231
    }
232
}
233