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.

Route::init()   B
last analyzed

Complexity

Conditions 7
Paths 18

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 18
nop 0
dl 0
loc 35
rs 8.4266
c 0
b 0
f 0
1
<?php
2
/**
3
 * Pimf
4
 *
5
 * @copyright Copyright (c)  Gjero Krsteski (http://krsteski.de)
6
 * @license   http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace Pimf;
10
11
/**
12
 * Route
13
 *
14
 * This class is a relationship of HTTP method(s), an HTTP URI to create
15
 * a Pimf application route. The Pimf application will determine
16
 * the one Route object to dispatch for the current HTTP request.
17
 *
18
 * Each route object will have a URI pattern. This pattern must match the
19
 * current HTTP request's URI for the route object to be dispatched by
20
 * the Pimf application. The route pattern may contain parameters, segments
21
 * prefixed with a colon (:). For example:
22
 *
23
 *     /controller/:action/:id
24
 *
25
 * When the route is dispatched, it's parameters array will be populated
26
 * with the values of the corresponding HTTP request URI segments.
27
 *
28
 * @package Pimf
29
 * @author  Gjero Krsteski <[email protected]>
30
 */
31
class Route
32
{
33
    /**
34
     * @var bool
35
     */
36
    private $matched = true;
37
38
    /**
39
     * @var array
40
     */
41
    private $params = array();
42
43
    /**
44
     * The route pattern (e.g. "/controller/:action/:id")
45
     *
46
     * @var string
47
     */
48
    private $rule;
49
50
    /**
51
     * Target to custom controller/action
52
     *
53
     * @var array
54
     */
55
    private $target;
56
57
    /**
58
     * Array of URL parameter names
59
     *
60
     * @var array
61
     */
62
    protected $names = array();
63
64
    /**
65
     * Array of URL parameter names with + at the end
66
     *
67
     * @var array
68
     */
69
    protected $namesPath = array();
70
71
    /**
72
     * Conditions for this route's URL parameters
73
     *
74
     * @var array
75
     */
76
    private $conditions;
77
78
    /**
79
     * @param string $rule
80
     * @param array  $target
81
     * @param array  $conditions
82
     */
83
    public function __construct($rule, array $target = array(), array $conditions = array())
84
    {
85
        $this->rule = $rule;
86
        $this->conditions = $conditions;
87
        $this->target = $target;
88
    }
89
90
    /**
91
     * @return Route
92
     */
93
    public function init()
94
    {
95
        //convert URL params into regex patterns, construct a regex for this route, load params
96
        $regex = preg_replace_callback(
97
            '#:([\w]+)\+?#', array($this, 'computeUrlRegex'), str_replace(')', ')?', (string)$this->rule)
98
        );
99
100
        if (substr($this->rule, -1) === '/') {
101
            $regex .= '?';
102
        }
103
104
        //cache URL params names and values if this route matches the current HTTP request
105
        $params = array();
106
        if (!preg_match('#^' . $regex . '$#', self::computeUri(), $params)) {
107
            $this->matched = false;
108
109
            return $this;
110
        }
111
112
        foreach ($this->names as $name) {
113
            if (isset($params[$name])) {
114
                if (isset($this->namesPath[$name])) {
115
                    $this->params[$name] = explode('/', urldecode($params[$name]));
116
                } else {
117
                    $this->params[$name] = urldecode($params[$name]);
118
                }
119
            }
120
        }
121
122
        foreach ($this->target as $key => $value) {
123
            $this->params[$key] = $value;
124
        }
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param array $matches
131
     *
132
     * @return string
133
     */
134
    private function computeUrlRegex(array $matches)
135
    {
136
        $this->names[] = $matches[1];
137
138
        if (isset($this->conditions[$matches[1]])) {
139
            return '(?P<' . $matches[1] . '>' . $this->conditions[$matches[1]] . ')';
140
        }
141
142
        if (substr($matches[0], -1) === '+') {
143
144
            $this->namesPath[$matches[1]] = 1;
145
146
            return '(?P<' . $matches[1] . '>.+)';
147
        }
148
149
        return '(?P<' . $matches[1] . '>[^/]+)';
150
    }
151
152
    /**
153
     * @throws \RuntimeException If request-uri does not match site base-url
154
     * @return string
155
     */
156
    private function computeUri()
157
    {
158
        $uri = Uri::full();
159
        $pos = strpos($uri, '?');
160
        $app = Config::get('app');
161
        $app_url = empty($app['url']) ? "" : $app['url'];
162
        $base_uri = parse_url($app_url);
163
        $base_path = isset($base_uri['path']) ? $base_uri['path'] : "";
164
165
        if (strlen($base_path) > 0) {
166
            // if $base_path exists
167
            if (strpos($uri, $base_path) == 0) {
168
                $uri = substr($uri, strlen($base_path));
169
            } else {
170
                throw \RuntimeException('request uri does not match site base url');
171
            }
172
        }
173
174
        if ($pos !== false) {
175
            return substr($uri, 0, $pos);
176
        }
177
178
        return $uri;
179
    }
180
181
    /**
182
     * @return boolean
183
     */
184
    public function matches()
185
    {
186
        return $this->matched;
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    public function getParams()
193
    {
194
        return $this->params;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getRule()
201
    {
202
        return $this->rule;
203
    }
204
}
205