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.
Passed
Push — master ( 09ff6d...312062 )
by
unknown
02:21
created

Router::getAddresses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System PHP Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Kernel\Http;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class Router
20
 * @package O2System\Kernel\Http
21
 */
22
class Router
23
{
24
    /**
25
     * Router::$uri
26
     *
27
     * @var Router\Uri
0 ignored issues
show
Bug introduced by
The type O2System\Kernel\Http\Router\Uri was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
     */
29
    protected $uri;
30
    
31
    /**
32
     * Router::$addresses
33
     *
34
     * @var Router\Addresses
35
     */
36
    protected $addresses;
37
38
    // ------------------------------------------------------------------------
39
    
40
    public function getUri()
41
    {
42
        return $this->uri;
43
    }
44
45
    public function setAddresses(Router\Addresses $addresses)
46
    {
47
        $this->addresses = $addresses;
48
49
        return $this;
50
    }
51
    
52
    public function getAddresses()
53
    {
54
        return $this->addresses;
55
    }
56
57
    // ------------------------------------------------------------------------
58
59
    public function parseRequest(Message\Uri $uri = null)
60
    {
61
        $this->uri = is_null($uri) ? server_request()->getUri() : $uri;
0 ignored issues
show
Documentation Bug introduced by
It seems like is_null($uri) ? server_request()->getUri() : $uri of type O2System\Kernel\Http\Message\Uri is incompatible with the declared type O2System\Kernel\Http\Router\Uri of property $uri.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
        $uriSegments = $this->uri->getSegments()->getParts();
63
        $uriString = $this->uri->getSegments()->getString();
64
65
        if($this->uri->getSegments()->getTotalParts()) {
66
            if(strpos(end($uriSegments), '.json') !== false) {
67
                output()->setContentType('application/json');
0 ignored issues
show
Bug introduced by
The method setContentType() does not exist on O2System\Kernel\Cli\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
                output()->/** @scrutinizer ignore-call */ setContentType('application/json');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
                $endSegment = str_replace('.json', '', end($uriSegments));
69
                array_pop($uriSegments);
70
                array_push($uriSegments, $endSegment);
71
                $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments));
72
                $uriString = $this->uri->getSegments()->getString();
73
            } elseif(strpos(end($uriSegments), '.xml') !== false) {
74
                output()->setContentType('application/xml');
75
                $endSegment = str_replace('.xml', '', end($uriSegments));
76
                array_pop($uriSegments);
77
                array_push($uriSegments, $endSegment);
78
                $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments));
79
                $uriString = $this->uri->getSegments()->getString();
80
            }
81
        } else {
82
            $uriPath = urldecode(
83
                parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH)
84
            );
85
86
            $uriPathParts = explode('public/', $uriPath);
87
            $uriPath = end($uriPathParts);
88
89
            if ($uriPath !== '/') {
90
                $uriString = $uriPath;
91
                $uriSegments = array_filter(explode('/', $uriString));
92
93
                $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments));
94
                $uriString = $this->uri->getSegments()->getString();
95
            }
96
        }
97
98
        if ($this->addresses instanceof Router\Addresses) {
0 ignored issues
show
introduced by
$this->addresses is always a sub-type of O2System\Kernel\Http\Router\Addresses.
Loading history...
99
            // Domain routing
100
            if (null !== ($domain = $this->addresses->getDomain())) {
101
                if (is_array($domain)) {
102
                    $uriSegments = array_merge($domain, $uriSegments);
103
                    $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments));
104
                    $uriString = $this->uri->getSegments()->getString();
105
                }
106
            } elseif (false !== ($subdomain = $this->uri->getSubdomain())) {
107
                if (is_array($subdomain)) {
108
                    $uriSegments = array_merge($subdomain, $uriSegments);
109
                    $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments));
110
                    $uriString = $this->uri->getSegments()->getString();
111
                }
112
            }
113
        }
114
115
        // Try to translate from uri string
116
        if (false !== ($action = $this->addresses->getTranslation($uriString))) {
117
            if ( ! $action->isValidHttpMethod(server_request()->getMethod()) && ! $action->isAnyHttpMethod()) {
118
                output()->sendError(405);
119
            } else {
120
                if (false !== ($parseSegments = $action->getParseUriString($uriString))) {
121
                    $uriSegments = $parseSegments;
122
                } else {
123
                    $uriSegments = [];
124
                }
125
126
                $this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments));
127
                $uriString = $this->uri->getSegments()->getString();
0 ignored issues
show
Unused Code introduced by
The assignment to $uriString is dead and can be removed.
Loading history...
128
129
                $this->parseAction($action, $uriSegments);
0 ignored issues
show
Bug introduced by
It seems like $action can also be of type true; however, parameter $action of O2System\Kernel\Http\Router::parseAction() does only seem to accept O2System\Kernel\Http\Router\Datastructures\Action, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
                $this->parseAction(/** @scrutinizer ignore-type */ $action, $uriSegments);
Loading history...
130
            }
131
        }
132
133
        // Let's the app do the rest when there is no controller found
134
        // the app should redirect to PAGE 404
135
    }
136
137
    // ------------------------------------------------------------------------
138
139
    protected function parseAction(Router\Datastructures\Action $action, array $uriSegments = [])
140
    {
141
        $closure = $action->getClosure();
142
        if (empty($closure)) {
143
            output()->sendError(204);
144
        }
145
146
        if ($closure instanceof Controller) {
147
            $uriSegments = empty($uriSegments)
148
                ? $action->getClosureParameters()
149
                : $uriSegments;
150
            $this->setController(
151
                (new Router\Datastructures\Controller($closure))
152
                    ->setRequestMethod('index'),
153
                $uriSegments
154
            );
155
        } elseif ($closure instanceof Router\Datastructures\Controller) {
156
            $this->setController($closure, $action->getClosureParameters());
157
        } elseif (is_array($closure)) {
158
            $uri = (new Message\Uri())
0 ignored issues
show
Unused Code introduced by
The assignment to $uri is dead and can be removed.
Loading history...
159
                ->withSegments(new Message\Uri\Segments(''))
160
                ->withQuery('');
161
            $this->parseRequest($this->uri->addSegments($closure));
162
        } else {
163
            if (class_exists($closure)) {
164
                $this->setController(
165
                    (new Router\Datastructures\Controller($closure))
166
                        ->setRequestMethod('index'),
167
                    $uriSegments
168
                );
169
            } elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) {
170
                $this->setController(
171
                    (new Router\Datastructures\Controller($matches[ 1 ]))
172
                        ->setRequestMethod($matches[ 3 ]),
173
                    $uriSegments
174
                );
175
            } elseif (is_string($closure) && $closure !== '') {
176
                if (is_json($closure)) {
177
                    output()->setContentType('application/json');
178
                    output()->send($closure);
0 ignored issues
show
Bug introduced by
The method send() does not exist on O2System\Kernel\Cli\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
                    output()->/** @scrutinizer ignore-call */ send($closure);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
                } else {
180
                    output()->send($closure);
181
                }
182
            } elseif (is_array($closure) || is_object($closure)) {
183
                output()->send($closure);
184
            } elseif (is_numeric($closure)) {
185
                output()->sendError($closure);
186
            } else {
187
                output()->sendError(204);
188
                exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
189
            }
190
        }
191
    }
192
193
    // ------------------------------------------------------------------------
194
195
    protected function setController(
196
        Router\Datastructures\Controller $controller,
197
        array $uriSegments = []
198
    ) {
199
        if ( ! $controller->isValid()) {
200
            output()->sendError(400);
201
        }
202
203
        // Add Controller PSR4 Namespace
204
        loader()->addNamespace($controller->getNamespaceName(), $controller->getFileInfo()->getPath());
0 ignored issues
show
Bug introduced by
The function loader was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
        /** @scrutinizer ignore-call */ 
205
        loader()->addNamespace($controller->getNamespaceName(), $controller->getFileInfo()->getPath());
Loading history...
205
206
        $controllerMethod = $controller->getRequestMethod();
207
        $controllerMethod = empty($controllerMethod) ? reset($uriSegments) : $controllerMethod;
208
        $controllerMethod = camelcase($controllerMethod);
209
210
        // Set default controller method to index
211
        if ( ! $controller->hasMethod($controllerMethod) &&
212
            ! $controller->hasMethod('route')
213
        ) {
214
            $controllerMethod = 'index';
215
        }
216
217
        // has route method, controller method set to index as default
218
        if (empty($controllerMethod)) {
219
            $controllerMethod = 'index';
220
        }
221
222
        if (camelcase(reset($uriSegments)) === $controllerMethod) {
223
            array_shift($uriSegments);
224
        }
225
226
        $controllerMethodParams = $uriSegments;
227
228
        if ($controller->hasMethod('route')) {
229
            $controller->setRequestMethod('route');
230
            $controller->setRequestMethodArgs([
231
                $controllerMethod,
232
                $controllerMethodParams,
233
            ]);
234
        } elseif ($controller->hasMethod($controllerMethod)) {
235
            $method = $controller->getMethod($controllerMethod);
236
237
            // Method doesn't need any parameters
238
            if ($method->getNumberOfParameters() == 0) {
239
                // But there is parameters requested
240
                if (count($controllerMethodParams)) {
241
                    output()->sendError(404);
242
                } else {
243
                    $controller->setRequestMethod($controllerMethod);
244
                }
245
            } else {
246
                $parameters = [];
247
248
                if (count($controllerMethodParams)) {
249
                    if (is_numeric(key($controllerMethodParams))) {
250
                        $parameters = $controllerMethodParams;
251
                    } else {
252
                        foreach ($method->getParameters() as $index => $parameter) {
253
                            if (isset($uriSegments[ $parameter->name ])) {
254
                                $parameters[ $index ] = $controllerMethodParams[ $parameter->name ];
255
                            } else {
256
                                $parameters[ $index ] = null;
257
                            }
258
                        }
259
                    }
260
                }
261
262
                $controller->setRequestMethod($controllerMethod);
263
                $controller->setRequestMethodArgs($parameters);
264
            }
265
        }
266
267
        // Set Controller
268
        services()->add($controller, 'controller');
269
    }
270
}