GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 74544a...72f380 )
by James
02:28
created

Compiler::subCompile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3
1
<?php
2
3
/**
4
 * This file is part of WebHelper Parser.
5
 *
6
 * (c) James <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WebHelper\Parser;
13
14
use WebHelper\Parser\Directive\SimpleDirective;
15
use WebHelper\Parser\Directive\BlockDirective;
16
use WebHelper\Parser\Directive\InclusionDirective;
17
use WebHelper\Parser\Exception\InvalidConfigException;
18
19
/**
20
 * Web server configuration generic compiler.
21
 *
22
 * @author James <[email protected]>
23
 */
24
class Compiler
25
{
26
    /**
27
     * The parser instance.
28
     *
29
     * @var Parser
30
     */
31
    private $parser;
32
33
    /**
34
     * The string to match as a starting multi-line directive.
35
     *
36
     * @var string
37
     */
38
    private $startMultiLine;
39
40
    /**
41
     * The string to match as an ending multi-line directive.
42
     *
43
     * @var string
44
     */
45
    private $endMultiLine;
46
47
    /**
48
     * The string to match a simple Directive.
49
     *
50
     * @var string
51
     */
52
    private $simpleDirective;
53
54
    /**
55
     * The string to match an inclusion Directive.
56
     *
57
     * @var string
58
     */
59
    private $inclusionDirective;
60
61
    /**
62
     * Constructor.
63
     *
64
     * @param string $startMultiLine     match as a starting multi-line directive
65
     * @param string $endMultiLine       match as an ending multi-line directive
66
     * @param string $simpleDirective    match a simple directive
67
     * @param string $inclusionDirective match an inclusion directive
68
     */
69 7
    public function __construct($startMultiLine, $endMultiLine, $simpleDirective, $inclusionDirective)
70
    {
71 7
        $this->startMultiLine = $startMultiLine;
72 7
        $this->endMultiLine = $endMultiLine;
73 7
        $this->simpleDirective = $simpleDirective;
74 7
        $this->inclusionDirective = $inclusionDirective;
75 7
    }
76
77
    /**
78
     * Sets the parser instance.
79
     *
80
     * @param Parser $parser the parser instance
81
     */
82 7
    public function setParser(Parser $parser)
83
    {
84 7
        $this->parser = $parser;
85
86 7
        return $this;
87
    }
88
89
    /**
90
     * Does a nested array of lines depending on container Directives.
91
     *
92
     * @param array  $activeConfig a clean config array of lines
93
     * @param string $context      the context name
94
     * @param string $value        an optional context value
95
     *
96
     * @return Directive\BlockDirective a full context of directives
97
     */
98 10
    public function doCompile($activeConfig, $context = 'main', $value = '')
99
    {
100 10
        $tempConfig = [];
101
102 10
        while (!empty($activeConfig)) {
103 10
            $lineConfig = array_shift($activeConfig);
104 10
            $tempConfig[] = $this->subCompile($activeConfig, $lineConfig);
105 9
        }
106
107 8
        return $this->buildBlockDirective($context, $value, $tempConfig);
108
    }
109
110
    /**
111
     * Looks for a container directive.
112
     *
113
     * @param array  $activeConfig a clean config array of directives
114
     * @param string $lineConfig   a line
115
     *
116
     * @return Directive\DirectiveInterface a directive or a container of directives
117
     *
118
     * @throws Exception\InvalidConfigException if a simple directive has invalid syntax
119
     */
120 10
    private function subCompile(&$activeConfig, $lineConfig)
121
    {
122 10
        if (preg_match($this->startMultiLine, $lineConfig, $container)) {
123 9
            return $this->findEndingKey(trim($container['key']), trim($container['value']), $activeConfig);
124
        }
125
126 9
        if (!preg_match($this->simpleDirective, $lineConfig, $container)) {
127 1
            throw InvalidConfigException::forSimpleDirectiveSyntaxError($lineConfig);
128
        }
129
130 9
        return $this->buildSimpleDirective(trim($container['key']), trim($container['value']));
131
    }
132
133
    /**
134
     * Finds the end of a container directive.
135
     *
136
     * @param string $context      a container's name
137
     * @param string $contextValue a container's value
138
     * @param array  $activeConfig a clean config array of lines
139
     *
140
     * @return Directive\BlockDirective a container of directives
141
     *
142
     * @throws Exception\InvalidConfigException if a container does not end correctly
143
     */
144 9
    private function findEndingKey($context, $contextValue, &$activeConfig)
145
    {
146 9
        $lines = [];
147 9
        $endMultiLine = sprintf($this->endMultiLine, $context);
148
149 9
        while (!empty($activeConfig)) {
150 8
            $lineConfig = array_shift($activeConfig);
151
152 8
            if (preg_match($endMultiLine, $lineConfig)) {
153 7
                return $this->buildBlockDirective($context, $contextValue, $lines);
154
            }
155
156 8
            $lines[] = $this->subCompile($activeConfig, $lineConfig);
157 8
        }
158
159 1
        throw InvalidConfigException::forEndingKeyNotFound($context);
160
    }
161
162
    /**
163
     * Builds a BlockDirective.
164
     *
165
     * @param string $context      a container's name
166
     * @param string $contextValue a container's value
167
     * @param array  $lines        an array of directives
168
     *
169
     * @return Directive\BlockDirective the BlockDirective
170
     */
171 8
    private function buildBlockDirective($context, $contextValue, $lines)
172
    {
173 8
        $block = new BlockDirective($context, $contextValue);
174 8
        foreach ($lines as $directive) {
175 8
            $block->add($directive);
176 8
        }
177
178 8
        return $block;
179
    }
180
181
    /**
182
     * Build a SimpleDirective or an InclusionDirective.
183
     *
184
     * Remember that inclusion are parsed as simple directives but are block directives
185
     *
186
     * @see Directive\InclusionDirective Inclusion Doc
187
     *
188
     * @param string $key   a directive's name
189
     * @param string $value a directive's value
190
     *
191
     * @return Directive\SimpleDirective|Directive\InclusionDirective the Directive
192
     */
193 9
    private function buildSimpleDirective($key, $value)
194
    {
195 9
        if (preg_match($this->inclusionDirective, $key)) {
196 1
            return new InclusionDirective($key, $value, $this->parser);
197
        }
198
199 9
        return new SimpleDirective($key, $value);
200
    }
201
}
202