Passed
Push — master ( e44dc3...5dec7d )
by Kirill
07:30
created

PragmaResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 6
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\Compiler\Reader\Resolver;
11
12
use Railt\Compiler\Exception\UnknownPragmaException;
13
use Railt\Io\Readable;
14
use Railt\Lexer\TokenInterface;
15
use Railt\Parser\Parser;
16
17
/**
18
 * Class PragmaResolver
19
 */
20
class PragmaResolver implements ResolverInterface
21
{
22
    private const ALLOWED_PRAGMAS = [
23
        Parser::PRAGMA_ROOT,
24
        Parser::PRAGMA_RUNTIME,
25
        Parser::PRAGMA_LOOKAHEAD,
26
    ];
27
28
    /**
29
     * @var array
30
     */
31
    private $configs = [];
32
33
    /**
34
     * @param Readable $readable
35
     * @param TokenInterface $token
36
     * @throws \Railt\Io\Exception\ExternalFileException
37
     */
38
    public function resolve(Readable $readable, TokenInterface $token): void
39
    {
40
        if (! \in_array($token->value(1), self::ALLOWED_PRAGMAS, true)) {
41
            $error = \vsprintf('Unknown configuration pragma rule "%s" with value "%s"', [
42
                $token->value(1),
43
                $token->value(2)
44
            ]);
45
46
            throw (new UnknownPragmaException($error))->throwsIn($readable, $token->offset());
47
        }
48
49
        $this->configs[$token->value(1)] = $token->value(2);
50
    }
51
}
52