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 ( 6a1cc3...5fc6ed )
by James
05:55
created

Compiler::doCompile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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