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 ( e0baf9...7f2ebd )
by James
03:10
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 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
     * Sets an absolute path prefix.
81
     *
82
     * @param string $prefix an absolute path prefix
83
     */
84 7
    public function setPrefix($prefix = '')
85
    {
86 7
        $this->prefix = $prefix;
87
88 7
        return $this;
89
    }
90
91
    /**
92
     * Does a nested array of lines depending on container Directives.
93
     *
94
     * @param array  $activeConfig a clean config array of lines
95
     * @param string $context      the context name
96
     * @param string $value        an optional context value
97
     *
98
     * @return Directive\BlockDirective a full context of directives
99
     */
100 7
    public function doCompile($activeConfig, $context = 'main', $value = '')
101
    {
102 7
        $tempConfig = [];
103
104 7
        while (!empty($activeConfig)) {
105 7
            $lineConfig = array_shift($activeConfig);
106 7
            $tempConfig[] = $this->subCompile($activeConfig, $lineConfig);
107 6
        }
108
109 5
        return $this->buildBlockDirective($context, $value, $tempConfig);
110
    }
111
112
    /**
113
     * Looks for a container directive.
114
     *
115
     * @param array  $activeConfig a clean config array of directives
116
     * @param string $lineConfig   a line
117
     *
118
     * @return Directive\DirectiveInterface a directive or a container of directives
119
     *
120
     * @throws Exception\InvalidConfigException if a simple directive has invalid syntax
121
     */
122 7
    private function subCompile(&$activeConfig, $lineConfig)
123
    {
124 7
        if (preg_match($this->startMultiLine, $lineConfig, $container)) {
125 6
            return $this->findEndingKey(trim($container['key']), trim($container['value']), $activeConfig);
126
        }
127
128 6
        if (!preg_match($this->simpleDirective, $lineConfig, $container)) {
129 1
            throw InvalidConfigException::forSimpleDirectiveSyntaxError($lineConfig);
130
        }
131
132 6
        return $this->buildSimpleDirective(trim($container['key']), trim($container['value']));
133
    }
134
135
    /**
136
     * Finds the end of a container directive.
137
     *
138
     * @param string $context      a container's name
139
     * @param string $contextValue a container's value
140
     * @param array  $activeConfig a clean config array of lines
141
     *
142
     * @return Directive\BlockDirective a container of directives
143
     *
144
     * @throws Exception\InvalidConfigException if a container does not end correctly
145
     */
146 6
    private function findEndingKey($context, $contextValue, &$activeConfig)
147
    {
148 6
        $lines = [];
149 6
        $endMultiLine = sprintf($this->endMultiLine, $context);
150
151 6
        while (!empty($activeConfig)) {
152 5
            $lineConfig = array_shift($activeConfig);
153
154 5
            if (preg_match($endMultiLine, $lineConfig)) {
155 4
                return $this->buildBlockDirective($context, $contextValue, $lines);
156
            }
157
158 5
            $lines[] = $this->subCompile($activeConfig, $lineConfig);
159 5
        }
160
161 1
        throw InvalidConfigException::forEndingKeyNotFound($context);
162
    }
163
164
    /**
165
     * Builds a BlockDirective.
166
     *
167
     * @param string $context      a container's name
168
     * @param string $contextValue a container's value
169
     * @param array  $lines        an array of directives
170
     *
171
     * @return Directive\BlockDirective the BlockDirective
172
     */
173 5
    private function buildBlockDirective($context, $contextValue, $lines)
174
    {
175 5
        $block = new BlockDirective($context, $contextValue);
176 5
        foreach ($lines as $directive) {
177 5
            $block->add($directive);
178 5
        }
179
180 5
        return $block;
181
    }
182
183
    /**
184
     * Build a SimpleDirective or an InclusionDirective.
185
     *
186
     * Remember that inclusion are parsed as simple directives but are block directives
187
     *
188
     * @see Directive\InclusionDirective Inclusion Doc
189
     *
190
     * @param string $key   a directive's name
191
     * @param string $value a directive's value
192
     *
193
     * @return Directive\SimpleDirective|Directive\InclusionDirective the Directive
194
     */
195 6
    private function buildSimpleDirective($key, $value)
196
    {
197 6
        if (preg_match($this->inclusionDirective, $key)) {
198 1
            return new InclusionDirective($key, $value, $this->prefix);
199
        }
200
201 6
        return new SimpleDirective($key, $value);
202
    }
203
}
204