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.

RouteCompiler::getPrefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
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;
17
18
use KM\Saffron\Route;
19
use KM\Saffron\RouteCompiled;
20
use KM\Saffron\Exception\InvalidArgument;
21
22
class RouteCompiler
23
{
24
    /**
25
     * @param Route $route
26
     * @return string
27
     */
28
    protected function getPrefix(Route $route)
29
    {
30
        // @TODO make it not ugly :)
31
        $pos = strpos($route->getUri(), '{');
32
33
        if (false !== $pos) {
34
            $length = max($pos - 1, 1);
35
        } else {
36
            $length = strlen($route->getUri());
37
        }
38
39
        return substr($route->getUri(), 0, $length);
40
    }
41
42
    /**
43
     * @param Route $route
44
     * @return string
45
     */
46
    protected function getUriRegex(Route $route)
47
    {
48
        $tokens = preg_split(
49
            '#([^}]?\{\w+\})#s',
50
            substr($route->getUri(), 1),
51
            -1,
52
            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
53
        );
54
55
        $regex = preg_quote(substr($route->getUri(), 0, 1), '#');
56
        foreach ($tokens as $token) {
57
            if (preg_match('#^(?P<delimiter>.)?\{(?P<placeholder>\w+)\}$#s', $token, $match)) {
58
                $regex .= sprintf(
59
                    '(%s(?P<%s>%s))%s',
60
                    isset($match['delimiter']) ? preg_quote($match['delimiter'], '#') : '',
61
                    preg_quote($match['placeholder'], '#'),
62
                    $route->getRequirement($match['placeholder']),
63
                    $route->hasDefault($match['placeholder']) ? '?' : ''
64
                );
65
            } else {
66
                $regex .= preg_quote($token, '#');
67
            }
68
        }
69
70
        return '#^'.$regex.'$#Us';
71
    }
72
73
    /**
74
     * @param Route $route
75
     * @return string
76
     */
77
    protected function getDomainRegex(Route $route)
78
    {
79
        $tokens = preg_split(
80
            '#({\w+\}[^{]?)#s',
81
            $route->getDomain(),
82
            -1,
83
            PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
84
        );
85
86
        $regex = '';
87
        foreach ($tokens as $token) {
88
            if (preg_match('#^\{(?P<placeholder>\w+)\}(?P<delimiter>.)?$#s', $token, $match)) {
89
                $regex .= sprintf(
90
                    '((?P<%s>%s)%s)%s',
91
                    preg_quote($match['placeholder'], '#'),
92
                    $route->getRequirement($match['placeholder']),
93
                    isset($match['delimiter']) ? preg_quote($match['delimiter'], '#') : '',
94
                    $route->hasDefault($match['placeholder']) ? '?' : ''
95
                );
96
            } else {
97
                $regex .= preg_quote($token, '#');
98
            }
99
        }
100
101
        return '#^'.$regex.'$#s';
102
    }
103
104
    protected function validateRoute(Route $route)
105
    {
106
        if (false !== strpos($route->getUri(), '{_') || false !== strpos($route->getDomain(), '{_')) {
107
            throw new InvalidArgument(
108
                sprintf(
109
                    'Placeholders cannot begin with _. Route: %s.',
110
                    $route->getName()
111
                )
112
            );
113
        }
114
    }
115
116
    /**
117
     * @param Route $route
118
     * @return RouteCompiled
119
     */
120
    public function compile(Route $route)
121
    {
122
        $this->validateRoute($route);
123
124
        $compiled = new RouteCompiled(
125
            $this->getPrefix($route),
126
            $this->getPrefix($route) != $route->getUri() ? $this->getUriRegex($route) : null,
127
            $route->hasDomain() ? $this->getDomainRegex($route) : null
128
        );
129
130
        return $compiled;
131
    }
132
}
133