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

PragmaResolver   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 32
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 13 2
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