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.

Generator::conditionUriRegex()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * Copyright 2014 Krzysztof Magosa
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
namespace KM\Saffron\UrlMatcher;
17
18
use KM\Saffron\RoutesCollection;
19
use KM\Saffron\Route;
20
use KM\Saffron\Code;
21
22
class Generator extends \KM\Saffron\Generator
23
{
24
    /**
25
     * @var RoutesCollection
26
     */
27
    protected $collection;
28
29
    /**
30
     * @var Code
31
     */
32
    protected $code;
33
34
    public function __construct(RoutesCollection $collection)
35
    {
36
        $this->collection = $collection;
37
        $this->code = new Code();
38
    }
39
40
    protected function conditionPrefix(Route $route, &$conditions)
41
    {
42
        $compiled = $route->getCompiled();
43
44
        if ($compiled->hasUriRegex()) {
45
            $conditions[] = sprintf(
46
                '0 === strpos($uri, %s)',
47
                var_export($compiled->getPrefix(), true)
48
            );
49
        } else {
50
            $conditions[] = sprintf(
51
                '$uri == %s',
52
                var_export($compiled->getPrefix(), true)
53
            );
54
        }
55
    }
56
57
    protected function conditionUriRegex(Route $route, array &$conditions)
58
    {
59
        $compiled = $route->getCompiled();
60
61
        if ($compiled->hasUriRegex()) {
62
            $conditions[] = sprintf(
63
                'preg_match(%s, $uri, $uriMatch)',
64
                var_export($compiled->getUriRegex(), true)
65
            );
66
        }
67
    }
68
69
    protected function conditionHttps(Route $route, array &$conditions)
70
    {
71
        if ($route->hasHttps()) {
72
            $conditions[] = sprintf(
73
                '$https === %s',
74
                var_export($route->getHttps(), true)
75
            );
76
        }
77
    }
78
79
    protected function getArraysOfParameters(Route $route)
80
    {
81
        $arrays = [];
82
        $compiled = $route->getCompiled();
83
84
        $arrays[] = sprintf(
85
            "['_route' => %s, '_request' => \$request]",
86
            var_export($route->getName(), true)
87
        );
88
89
        if ($route->hasDefaults()) {
90
            $arrays[] = $this->formatArray($route->getDefaults());
91
        }
92
93
        if ($route->hasDomain()) {
94
            $arrays[] = '$domainMatch';
95
        }
96
        if ($compiled->hasUriRegex()) {
97
            $arrays[] = '$uriMatch';
98
        }
99
100
        return $arrays;
101
    }
102
103
    protected function generateRoute(Route $route)
104
    {
105
        $conditions = [];
106
        $this->conditionPrefix($route, $conditions);
107
        $this->conditionUriRegex($route, $conditions);
108
        $this->conditionHttps($route, $conditions);
109
110
        $this->code->append(
111
            sprintf(
112
                'if (%s) {',
113
                implode(' && ', $conditions)
114
            )
115
        );
116
117
        if ($route->hasMethod()) {
118
            $this->code->append(
119
                sprintf(
120
                    'if (in_array($method, %s)) {',
121
                    $this->formatArray($route->getMethod())
122
                )
123
            );
124
        }
125
126
        $this->code
127
            ->append('return new RoutingResult(')
128
            ->append('true,')
129
            ->append('false,')
130
            ->append('false,')
131
            ->append('[],')
132
            ->append($this->formatArray($route->getTarget()).',');
133
134
        $arrays = $this->getArraysOfParameters($route);
135
        if (count($arrays) >= 2) {
136
            $this->code->append(
137
                sprintf(
138
                    '$this->filterParameters(array_replace(%s))',
139
                    implode(', ', $this->getArraysOfParameters($route))
140
                )
141
            );
142
        } else {
143
            $this->code->append(
144
                sprintf(
145
                    '$this->filterParameters(%s)',
146
                    $this->getArraysOfParameters($route)[0]
147
                )
148
            );
149
        }
150
151
        $this->code->append(');');
152
153
        if ($route->hasMethod()) {
154
            $this->code->append('}');
155
            $this->code->append('else {');
156
            $this->code->append(
157
                sprintf(
158
                    '$allowedMethods = array_merge($allowedMethods, %s);',
159
                    $this->formatArray($route->getMethod())
160
                )
161
            );
162
            $this->code->append('}');
163
        }
164
165
        $this->code->append('}');
166
    }
167
168
    protected function generateMatchMethod()
169
    {
170
        $this->code
171
            ->append('public function match(Request $request) {')
172
            ->append('$uri = $request->getUri();')
173
            ->append('$allowedMethods = [];');
174
175
        if ($this->collection->hasDomain()) {
176
            $this->code->append('$domain = $request->getDomain();');
177
        }
178
179
        if ($this->collection->hasHttps()) {
180
            $this->code->append('$https = $request->getHttps();');
181
        }
182
183
        if ($this->collection->hasMethod()) {
184
            $this->code->append('$method = $request->getMethod();');
185
        }
186
187
        foreach ($this->collection->groupByDomain() as $routes) {
188
            if ($routes->first()->hasDomain()) {
189
                $this->code->append(
190
                    sprintf(
191
                        'if (preg_match(%s, $domain, $domainMatch)) {',
192
                        var_export(
193
                            $routes->first()->getCompiled()->getDomainRegex(),
194
                            true
195
                        )
196
                    )
197
                );
198
            }
199
200
            foreach ($routes as $route) {
201
                $this->generateRoute($route);
202
            }
203
204
            if ($routes->first()->hasDomain()) {
205
                $this->code->append('}');
206
            }
207
        }
208
209
        $this->code
210
            ->append('return new RoutingResult(')
211
            ->append('false,')
212
            ->append('!empty($allowedMethods),')
213
            ->append('empty($allowedMethods),')
214
            ->append('array_unique($allowedMethods),')
215
            ->append('[],')
216
            ->append('[]')
217
            ->append(');');
218
219
        $this->code->append('}');
220
    }
221
222
    /**
223
     * @param string $className
224
     */
225
    protected function generateHeader($className)
226
    {
227
        $this->code->append(
228
<<<EOB
229
            <?php
230
            use KM\Saffron\UrlMatcher;
231
            use KM\Saffron\Request;
232
            use KM\Saffron\RoutingResult;
233
            use KM\Saffron\UrlMatcher\Base;
234
235
            class $className extends Base {
236
EOB
237
        );
238
    }
239
240
    protected function generateFooter()
241
    {
242
        $this->code->append('}');
243
    }
244
245
    /**
246
     * @param string $className
247
     * @return string
248
     */
249
    public function generate($className)
250
    {
251
        $this->generateHeader($className);
252
        $this->generateMatchMethod();
253
        $this->generateFooter();
254
255
        return (string)$this->code;
256
    }
257
}
258