Completed
Push — master ( f6f8bd...61d974 )
by Kirill
02:08
created

PragmaResolver::bootResolvers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 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\Compiler\Reader\Resolver\PragmaResolver\ConfigResolver;
14
use Railt\Io\Readable;
15
use Railt\Lexer\TokenInterface;
16
use Railt\Parser\Configuration;
17
18
/**
19
 * Class PragmaResolver
20
 */
21
class PragmaResolver implements ResolverInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private const PARSER_RESOLVER = 'parser';
27
28
    /**
29
     * @var array|ConfigResolver[]
30
     */
31
    private $resolvers;
32
33
    /**
34
     * @var array
35
     */
36
    private $configs = [];
37
38
    /**
39
     * PragmaResolver constructor.
40
     */
41
    public function __construct()
42
    {
43
        $this->bootResolvers();
44
    }
45
46
    /**
47
     * @return void
48
     */
49
    private function bootResolvers(): void
50
    {
51
        $this->resolvers = [
52
            self::PARSER_RESOLVER => new ConfigResolver(self::PARSER_RESOLVER, [
53
                Configuration::PRAGMA_ROOT,
54
                Configuration::PRAGMA_LOOKAHEAD,
55
                Configuration::PRAGMA_RUNTIME
56
            ])
57
        ];
58
    }
59
60
    /**
61
     * @param Readable $readable
62
     * @param TokenInterface $token
63
     * @throws \Railt\Io\Exception\ExternalFileException
64
     */
65
    public function resolve(Readable $readable, TokenInterface $token): void
66
    {
67
        [$name, $value] = [$token->value(1), $token->value(2)];
0 ignored issues
show
Bug introduced by
The variable $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
68
69
        foreach ($this->resolvers as $group => $resolver) {
70
            if ($resolver->match($name)) {
0 ignored issues
show
Bug introduced by
The variable $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
71
                $name = $this->resolvePragmaName($readable, $token, $resolver->resolve($name));
0 ignored issues
show
Bug introduced by
The variable $name seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
72
                $this->set($group, $name, $value);
73
                return;
74
            }
75
        }
76
    }
77
78
    /**
79
     * @param string $group
80
     * @param string $name
81
     * @param string $value
82
     */
83
    private function set(string $group, string $name, string $value): void
84
    {
85
        if (! \array_key_exists($group, $this->configs)) {
86
            $this->configs[$group] = [];
87
        }
88
89
        $this->configs[$group][$name] = $value;
90
    }
91
92
    /**
93
     * @param Readable $readable
94
     * @param TokenInterface $token
95
     * @param null|string $name
96
     * @return string
97
     * @throws \Railt\Io\Exception\ExternalFileException
98
     */
99
    private function resolvePragmaName(Readable $readable, TokenInterface $token, ?string $name): string
100
    {
101
        if ($name === null) {
102
            $error = \vsprintf('Unknown configuration pragma rule "%s" with value "%s"', [
103
                $token->value(1),
104
                $token->value(2),
105
            ]);
106
107
            throw (new UnknownPragmaException($error))->throwsIn($readable, $token->offset());
108
        }
109
110
        return $name;
111
    }
112
113
    /**
114
     * @param string $group
115
     * @return iterable
116
     */
117
    public function all(string $group): iterable
118
    {
119
        return $this->configs[$group] ?? [];
120
    }
121
}
122