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 ( 6b2166...351169 )
by Steeven
02:38
created

Router   F

Complexity

Total Complexity 86

Size/Duplication

Total Lines 499
Duplicated Lines 0 %

Importance

Changes 7
Bugs 4 Features 0
Metric Value
eloc 270
c 7
b 4
f 0
dl 0
loc 499
rs 2
wmc 86

7 Methods

Rating   Name   Duplication   Size   Complexity  
B handleSegmentsRequest() 0 27 7
F handle() 0 220 40
D parseAction() 0 65 18
B registerModule() 0 54 8
A handleAppRequest() 0 14 3
A handleExtensionRequest() 0 24 5
A getPagesControllerClassName() 0 21 5

How to fix   Complexity   

Complex Class

Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of the O2System 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\Framework\Http;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Framework\Containers\Modules\DataStructures\Module as FrameworkModuleDataStructure;
19
use O2System\Kernel\Http\Message\Uri as KernelMessageUri;
20
use O2System\Kernel\Http\Message\Uri\Segments as KernelMessageUriSegments;
21
use O2System\Kernel\Http\Router as KernelRouter;
22
use O2System\Kernel\Http\Router\Addresses as KernelAddresses;
23
use O2System\Kernel\Http\Router\DataStructures\Action as KernelActionDataStructure;
24
use O2System\Kernel\Http\Router\DataStructures\Controller as KernelControllerDataStructure;
25
use O2System\Spl\Info\SplFileInfo;
26
27
/**
28
 * Class Router
29
 *
30
 * @package O2System
31
 */
32
class Router extends KernelRouter
33
{
34
    /**
35
     * Router::handle
36
     *
37
     * @param KernelMessageUri|null $uri
38
     *
39
     * @return bool
40
     * @throws \O2System\Spl\Exceptions\RuntimeException
41
     * @throws \ReflectionException
42
     */
43
    public function handle(KernelMessageUri $uri = null)
44
    {
45
        $this->uri = is_null($uri) ? new KernelMessageUri() : $uri;
46
47
        // Handle Extension Request
48
        if ($this->uri->segments->count()) {
0 ignored issues
show
Bug introduced by
The method count() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

48
        if ($this->uri->segments->/** @scrutinizer ignore-call */ count()) {

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...
49
            $this->handleExtensionRequest();
50
        } else {
51
            $uriPath = urldecode(
52
                parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH)
53
            );
54
55
            $uriPathParts = explode('public/', $uriPath);
56
            $uriPath = end($uriPathParts);
57
58
            if ($uriPath !== '/') {
59
                $uriString = $uriPath; // tobe removed
60
                $uriSegments = array_filter(explode('/', $uriString)); // tobe removed
0 ignored issues
show
Unused Code introduced by
The assignment to $uriSegments is dead and can be removed.
Loading history...
61
62
                $this->uri = $this->uri->withSegments(new KernelMessageUriSegments(
63
                    array_filter(explode('/', $uriString)))
0 ignored issues
show
Bug introduced by
array_filter(explode('/', $uriString)) of type array is incompatible with the type null|string expected by parameter $string of O2System\Kernel\Http\Mes...Segments::__construct(). ( Ignorable by Annotation )

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

63
                    /** @scrutinizer ignore-type */ array_filter(explode('/', $uriString)))
Loading history...
64
                );
65
            }
66
67
            unset($uriPathParts, $uriPath);
68
        }
69
70
        // Load app addresses config
71
        $this->addresses = config()->loadFile('addresses', true);
0 ignored issues
show
Bug introduced by
The method loadFile() does not exist on O2System\Kernel\DataStructures\Config. ( Ignorable by Annotation )

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

71
        $this->addresses = config()->/** @scrutinizer ignore-call */ loadFile('addresses', true);

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...
72
73
        if ($this->addresses instanceof KernelAddresses) {
74
            // Domain routing
75
            if (null !== ($domain = $this->addresses->getDomain())) {
76
                if (is_array($domain)) {
77
                    $this->uri->segments->exchangeArray(
0 ignored issues
show
Bug introduced by
The method exchangeArray() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

77
                    $this->uri->segments->/** @scrutinizer ignore-call */ 
78
                                          exchangeArray(

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...
78
                        array_merge($domain, $this->uri->segments->getArrayCopy())
0 ignored issues
show
Bug introduced by
The method getArrayCopy() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

78
                        array_merge($domain, $this->uri->segments->/** @scrutinizer ignore-call */ getArrayCopy())

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...
79
                    );
80
                    $domain = $this->uri->segments->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

80
                    /** @scrutinizer ignore-call */ 
81
                    $domain = $this->uri->segments->first();

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...
81
                }
82
83
                if (false !== ($app = modules()->getApp($domain))) {
0 ignored issues
show
Bug introduced by
The method getApp() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

83
                if (false !== ($app = modules()->/** @scrutinizer ignore-call */ getApp($domain))) {

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...
84
                    $this->registerModule($app);
0 ignored issues
show
Bug introduced by
It seems like $app can also be of type true; however, parameter $module of O2System\Framework\Http\Router::registerModule() does only seem to accept O2System\Framework\Conta...s\DataStructures\Module, 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

84
                    $this->registerModule(/** @scrutinizer ignore-type */ $app);
Loading history...
85
                } elseif (false !== ($module = modules()->getModule($domain))) {
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

85
                } elseif (false !== ($module = modules()->/** @scrutinizer ignore-call */ getModule($domain))) {

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...
86
                    $this->registerModule($module);
87
                }
88
            } elseif (false !== ($subdomain = $this->uri->getSubdomain())) {
89
                if (false !== ($app = modules()->getApp($subdomain))) {
0 ignored issues
show
Bug introduced by
It seems like $subdomain can also be of type true; however, parameter $segment of O2System\Framework\Containers\Modules::getApp() does only seem to accept string, 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

89
                if (false !== ($app = modules()->getApp(/** @scrutinizer ignore-type */ $subdomain))) {
Loading history...
90
                    $this->registerModule($app);
91
                }
92
            }
93
        }
94
95
        // App and Module routing
96
        if ($numOfUriSegments = $this->uri->segments->count()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $numOfUriSegments is dead and can be removed.
Loading history...
97
            if (empty($app)) {
98
                if (false !== ($module = modules()->getModule($this->uri->segments->first()))) {
99
                    $this->registerModule($module);
100
101
                    if ($module->getType() === 'APP') {
102
                        $this->uri->segments->shift();
0 ignored issues
show
Bug introduced by
The method shift() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

102
                        $this->uri->segments->/** @scrutinizer ignore-call */ 
103
                                              shift();

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...
103
                        $this->handleAppRequest($module);
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type true; however, parameter $app of O2System\Framework\Http\Router::handleAppRequest() does only seem to accept O2System\Framework\Conta...s\DataStructures\Module, 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

103
                        $this->handleAppRequest(/** @scrutinizer ignore-type */ $module);
Loading history...
104
                    } else {
105
                        $this->handleSegmentsRequest();
106
                    }
107
                }
108
            } else {
109
                $this->handleAppRequest($app);
110
            }
111
        }
112
113
        // Try to translate from uri string
114
        if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) {
0 ignored issues
show
Bug introduced by
The method getTranslation() does not exist on O2System\Spl\DataStructures\SplArrayObject. It seems like you code against a sub-type of O2System\Spl\DataStructures\SplArrayObject such as O2System\Framework\Model...\DataObjects\Result\Row. ( Ignorable by Annotation )

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

114
        if (false !== ($action = $this->addresses->/** @scrutinizer ignore-call */ getTranslation($this->uri->segments->__toString()))) {
Loading history...
115
            if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) {
116
                output()->sendError(405);
117
            } else {
118
                // Checks if action closure is an array
119
                if (is_array($closureSegments = $action->getClosure())) {
120
                    $this->uri->segments->exchangeArray($closureSegments);
121
122
                    if (false !== ($module = modules()->getModule($this->uri->segments->first()))) {
123
                        $this->registerModule($module);
124
125
                        if ($module->getType() === 'APP') {
126
                            $this->uri->segments->shift();
127
                            $this->handleAppRequest($module);
128
                        } else {
129
                            $this->handleSegmentsRequest();
130
                        }
131
                    } else {
132
                        $this->handleSegmentsRequest();
133
                    }
134
                } else {
135
                    if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) {
136
                        $uriSegments = $parseSegments;
137
                    } else {
138
                        $uriSegments = [];
139
                    }
140
141
                    $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments));
0 ignored issues
show
Bug introduced by
$uriSegments of type array|true is incompatible with the type null|string expected by parameter $string of O2System\Kernel\Http\Mes...Segments::__construct(). ( Ignorable by Annotation )

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

141
                    $this->uri = $this->uri->withSegments(new KernelMessageUriSegments(/** @scrutinizer ignore-type */ $uriSegments));
Loading history...
142
                    $uriString = $this->uri->segments->__toString();
0 ignored issues
show
Unused Code introduced by
The assignment to $uriString is dead and can be removed.
Loading history...
143
144
                    $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\Framework\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

144
                    $this->parseAction(/** @scrutinizer ignore-type */ $action, $uriSegments);
Loading history...
Bug introduced by
It seems like $uriSegments can also be of type true; however, parameter $uriSegments of O2System\Framework\Http\Router::parseAction() does only seem to accept array, 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

144
                    $this->parseAction($action, /** @scrutinizer ignore-type */ $uriSegments);
Loading history...
145
                    if ( ! empty(services()->has('controller'))) {
146
                        return true;
147
                    }
148
                }
149
            }
150
        }
151
152
        // Try to get route from controller & page
153
        if ($numOfUriSegments = $this->uri->segments->count()) {
154
            $uriSegments = $this->uri->segments->getArrayCopy();
155
            $uriString = $this->uri->segments->__toString();
156
157
            for ($i = 0; $i <= $numOfUriSegments; $i++) {
158
                $uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i));
159
                $modules = modules()->getArrayCopy();
0 ignored issues
show
Bug introduced by
The method getArrayCopy() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

159
                $modules = modules()->/** @scrutinizer ignore-call */ getArrayCopy();

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...
160
161
                foreach ($modules as $module) {
162
                    $controllerNamespace = $module->getNamespace() . 'Controllers\\';
163
164
                    if ($module->getNamespace() === 'O2System\Framework\\') {
165
                        $controllerNamespace = 'O2System\Framework\Http\Controllers\\';
166
                    }
167
168
                    /**
169
                     * Try to find requested controller
170
                     */
171
                    if (class_exists($controllerClassName = $controllerNamespace . implode('\\',
172
                            array_map('studlycase', $uriRoutedSegments)))) {
173
174
                        if ($controllerClassName::$inherited) {
0 ignored issues
show
Bug introduced by
The property inherited does not exist on string.
Loading history...
175
                            $uriSegments = array_diff($uriSegments, $uriRoutedSegments);
176
                            $this->setController(new KernelControllerDataStructure($controllerClassName),
177
                                $uriSegments);
178
179
                            break;
180
                        } else {
181
                            $uriSegments = array_diff($uriSegments, $uriRoutedSegments);
182
                            $this->setController(new KernelControllerDataStructure($controllerClassName),
183
                                $uriSegments);
184
                        }
185
                    }
186
187
                    /**
188
                     * Try to find requested page
189
                     */
190
                    if (false !== ($pagesDir = $module->getResourcesDir('pages'))) {
191
                        if ($controllerClassName = $this->getPagesControllerClassName()) {
192
193
                            /**
194
                             * Try to find from database
195
                             */
196
                            $modelClassName = str_replace('Controllers', 'Models', $controllerClassName);
197
198
                            if (class_exists($modelClassName)) {
199
                                models()->load($modelClassName, 'controller');
0 ignored issues
show
Bug introduced by
The method load() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

199
                                models()->/** @scrutinizer ignore-call */ load($modelClassName, 'controller');

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...
Bug introduced by
The method load() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

199
                                models()->/** @scrutinizer ignore-call */ load($modelClassName, 'controller');
Loading history...
200
201
                                if (false !== ($page = models('controller')->find($uriString, 'segments'))) {
0 ignored issues
show
Bug introduced by
The method find() does not exist on O2System\Framework\Containers\Models. ( Ignorable by Annotation )

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

201
                                if (false !== ($page = models('controller')->/** @scrutinizer ignore-call */ find($uriString, 'segments'))) {

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...
202
                                    if (isset($page->content)) {
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist on O2System\Framework\Models\Sql\DataObjects\Result. Since you implemented __get, consider adding a @property annotation.
Loading history...
203
                                        presenter()->partials->offsetSet('content', $page->content);
204
205
                                        $this->setController(
206
                                            (new KernelControllerDataStructure($controllerClassName))
207
                                                ->setRequestMethod('index')
208
                                        );
209
210
                                        return true;
211
                                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
212
                                    }
213
                                }
214
                            }
215
216
                            /**
217
                             * Try to find from page file
218
                             */
219
220
                            foreach (['.phtml', '.vue'] as $pageExtension) {
221
                                $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR,
222
                                        array_map('dash', $uriRoutedSegments)) . $pageExtension;
223
224
                                if (is_file($pageFilePath)) {
225
                                    presenter()->page->setFile($pageFilePath);
226
                                    break;
227
                                } else {
228
                                    $pageFilePath = str_replace($pageExtension,
229
                                        DIRECTORY_SEPARATOR . 'index' . $pageExtension, $pageFilePath);
230
                                    if (is_file($pageFilePath)) {
231
                                        presenter()->page->setFile($pageFilePath);
232
                                        break;
233
                                    }
234
                                }
235
                            }
236
237
                            if (presenter()->page->file instanceof SplFileInfo) {
238
                                $this->setController(
239
                                    (new KernelControllerDataStructure($controllerClassName))
240
                                        ->setRequestMethod('index')
241
                                );
242
243
                                return true;
244
                                break;
245
                            }
246
                        }
247
                    }
248
                }
249
250
                // break the loop if the controller has been set
251
                if (services()->has('controller')) {
252
                    return true;
253
                    break;
254
                }
255
            }
256
        }
257
258
        if (class_exists($controllerClassName = modules()->top()->getDefaultControllerClassName())) {
0 ignored issues
show
Bug introduced by
The method top() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

258
        if (class_exists($controllerClassName = modules()->/** @scrutinizer ignore-call */ top()->getDefaultControllerClassName())) {

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...
259
            $this->setController(new KernelControllerDataStructure($controllerClassName),
260
                $this->uri->segments->getArrayCopy());
261
262
            return true;
263
        }
264
265
        // Let's the framework do the rest when there is no controller found
266
        // the framework will redirect to PAGE 404
267
    }
268
269
    // ------------------------------------------------------------------------
270
271
    protected function handleExtensionRequest()
272
    {
273
        $lastSegment = $this->uri->segments->last();
0 ignored issues
show
Bug introduced by
The method last() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

273
        /** @scrutinizer ignore-call */ 
274
        $lastSegment = $this->uri->segments->last();

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...
274
275
        if (strpos($lastSegment, '.json') !== false) {
276
            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

276
            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...
277
            $lastSegment = str_replace('.json', '', $lastSegment);
278
            $this->uri->segments->pop();
0 ignored issues
show
Bug introduced by
The method pop() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

278
            $this->uri->segments->/** @scrutinizer ignore-call */ 
279
                                  pop();

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...
279
            $this->uri->segments->push($lastSegment);
0 ignored issues
show
Bug introduced by
The method push() does not exist on O2System\Kernel\Http\Message\Uri\Segments. ( Ignorable by Annotation )

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

279
            $this->uri->segments->/** @scrutinizer ignore-call */ 
280
                                  push($lastSegment);

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...
280
        } elseif (strpos($lastSegment, '.xml') !== false) {
281
            output()->setContentType('application/xml');
282
            $lastSegment = str_replace('.xml', '', $lastSegment);
283
            $this->uri->segments->pop();
284
            $this->uri->segments->push($lastSegment);
285
        } elseif (strpos($lastSegment, '.js') !== false) {
286
            output()->setContentType('application/x-javascript');
287
            $lastSegment = str_replace('.js', '', $lastSegment);
288
            $this->uri->segments->pop();
289
            $this->uri->segments->push($lastSegment);
290
        } elseif (strpos($lastSegment, '.css') !== false) {
291
            output()->setContentType('text/css');
292
            $lastSegment = str_replace('.css', '', $lastSegment);
293
            $this->uri->segments->pop();
294
            $this->uri->segments->push($lastSegment);
295
        }
296
    }
297
298
    /**
299
     * Router::handleAppRequest
300
     *
301
     * @param \O2System\Framework\Containers\Modules\DataStructures\Module $app
302
     */
303
    public function handleAppRequest(FrameworkModuleDataStructure $app)
304
    {
305
        // Find App module
306
        foreach(['modules', 'plugins'] as $additionalSegment) {
307
            if (false !== ($module = modules()->getModule([
308
                    $app->getParameter(),
309
                    $additionalSegment,
310
                    $this->uri->segments->first(),
311
                ]))) {
312
                $this->uri->segments->shift();
313
314
                $this->registerModule($module);
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type true; however, parameter $module of O2System\Framework\Http\Router::registerModule() does only seem to accept O2System\Framework\Conta...s\DataStructures\Module, 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

314
                $this->registerModule(/** @scrutinizer ignore-type */ $module);
Loading history...
315
                $this->handleSegmentsRequest();
316
                break;
317
            }
318
        }
319
    }
320
321
    // ------------------------------------------------------------------------
322
323
    /**
324
     * Router::handleModuleRequest
325
     */
326
    public function handleSegmentsRequest()
327
    {
328
        $module = modules()->getActiveModule();
0 ignored issues
show
Bug introduced by
The method getActiveModule() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

328
        $module = modules()->/** @scrutinizer ignore-call */ getActiveModule();

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...
329
330
        if ($numOfUriSegments = $this->uri->segments->count()) {
331
            $uriSegments = $this->uri->segments->getArrayCopy();
332
333
            for ($i = 0; $i <= $numOfUriSegments; $i++) {
334
                $uriRoutedSegments = array_diff($uriSegments,
335
                    array_slice($uriSegments, ($numOfUriSegments - $i)));
336
337
                if(count($uriRoutedSegments)) {
338
                    if($module instanceof FrameworkModuleDataStructure) {
339
                        $moduleSegments = $module->getSegments();
340
341
                        if(count($moduleSegments)) {
342
                            $uriRoutedSegments = array_merge($moduleSegments, $uriRoutedSegments);
343
                        }
344
                    }
345
346
                    if (false !== ($module = modules()->getModule($uriRoutedSegments))) {
347
                        $uriSegments = array_diff($uriSegments, $uriRoutedSegments);
348
                        $this->uri->segments->exchangeArray($uriSegments);
349
350
                        $this->registerModule($module);
0 ignored issues
show
Bug introduced by
It seems like $module can also be of type true; however, parameter $module of O2System\Framework\Http\Router::registerModule() does only seem to accept O2System\Framework\Conta...s\DataStructures\Module, 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

350
                        $this->registerModule(/** @scrutinizer ignore-type */ $module);
Loading history...
351
                        $this->handleSegmentsRequest();
352
                        break;
353
                    }
354
                }
355
            }
356
        }
357
    }
358
359
    // ------------------------------------------------------------------------
360
361
    /**
362
     * Router::getPagesControllerClassName
363
     *
364
     * @return bool|string
365
     */
366
    final protected function getPagesControllerClassName()
367
    {
368
        $modules = modules()->getArrayCopy();
369
370
        foreach ($modules as $module) {
371
            $controllerClassName = $module->getNamespace() . 'Controllers\Pages';
372
            if ($module->getNamespace() === 'O2System\Framework\\') {
373
                $controllerClassName = 'O2System\Framework\Http\Controllers\Pages';
374
            }
375
376
            if (class_exists($controllerClassName)) {
377
                return $controllerClassName;
378
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
379
            }
380
        }
381
382
        if (class_exists('O2System\Framework\Http\Controllers\Pages')) {
383
            return 'O2System\Framework\Http\Controllers\Pages';
384
        }
385
386
        return false;
387
    }
388
389
    // ------------------------------------------------------------------------
390
391
    /**
392
     * Router::registerModule
393
     *
394
     * @param FrameworkModuleDataStructure $module
395
     */
396
    final public function registerModule(FrameworkModuleDataStructure $module)
397
    {
398
        // Push Subdomain App Module
399
        modules()->push($module);
0 ignored issues
show
Bug introduced by
The method push() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

399
        modules()->/** @scrutinizer ignore-call */ push($module);

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...
400
401
        // Add Config FilePath
402
        config()->addFilePath($module->getRealPath());
0 ignored issues
show
Bug introduced by
The method addFilePath() does not exist on O2System\Kernel\DataStructures\Config. ( Ignorable by Annotation )

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

402
        config()->/** @scrutinizer ignore-call */ addFilePath($module->getRealPath());

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...
403
404
        // Reload Config
405
        config()->reload();
0 ignored issues
show
Bug introduced by
The method reload() does not exist on O2System\Kernel\DataStructures\Config. ( Ignorable by Annotation )

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

405
        config()->/** @scrutinizer ignore-call */ reload();

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...
406
407
        // Load modular addresses config
408
        if (false !== ($configDir = $module->getDir('config', true))) {
409
            unset($addresses);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $addresses seems to be never defined.
Loading history...
410
411
            $reconfig = false;
412
            if (is_file(
413
                $filePath = $configDir . ucfirst(
414
                        strtolower(ENVIRONMENT)
0 ignored issues
show
Bug introduced by
The constant O2System\Framework\Http\ENVIRONMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
415
                    ) . DIRECTORY_SEPARATOR . 'Addresses.php'
416
            )) {
417
                require($filePath);
418
                $reconfig = true;
419
            } elseif (is_file(
420
                $filePath = $configDir . 'Addresses.php'
421
            )) {
422
                require($filePath);
423
                $reconfig = true;
424
            }
425
426
            if ( ! $reconfig) {
427
                $controllerNamespace = $module->getNamespace() . 'Controllers\\';
428
                $controllerClassName = $controllerNamespace . studlycase($module->getParameter());
429
430
                if (class_exists($controllerClassName)) {
431
                    $this->addresses->any(
432
                        '/',
433
                        function () use ($controllerClassName) {
434
                            return new $controllerClassName();
435
                        }
436
                    );
437
                }
438
            } elseif (isset($addresses)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $addresses seems to never exist and therefore isset should always be false.
Loading history...
439
                $this->addresses = $addresses;
440
            }
441
        } else {
442
            $controllerNamespace = $module->getNamespace() . 'Controllers\\';
443
            $controllerClassName = $controllerNamespace . studlycase($module->getParameter());
444
445
            if (class_exists($controllerClassName)) {
446
                $this->addresses->any(
447
                    '/',
448
                    function () use ($controllerClassName) {
449
                        return new $controllerClassName();
450
                    }
451
                );
452
            }
453
        }
454
    }
455
456
    // ------------------------------------------------------------------------
457
458
    /**
459
     * Router::parseAction
460
     *
461
     * @param KernelActionDataStructure $action
462
     * @param array                     $uriSegments
463
     *
464
     * @throws \ReflectionException
465
     */
466
    protected function parseAction(KernelActionDataStructure $action, array $uriSegments = [])
467
    {
468
        ob_start();
469
        $closure = $action->getClosure();
470
        if (empty($closure)) {
471
            $closure = ob_get_contents();
472
        }
473
        ob_end_clean();
474
475
        if ($closure instanceof Controller) {
476
            $uriSegments = empty($uriSegments)
477
                ? $action->getClosureParameters()
478
                : $uriSegments;
479
            $this->setController(
480
                (new KernelControllerDataStructure($closure))
0 ignored issues
show
Bug introduced by
$closure of type O2System\Framework\Http\Controller is incompatible with the type string expected by parameter $filePath of O2System\Kernel\Http\Rou...ntroller::__construct(). ( Ignorable by Annotation )

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

480
                (new KernelControllerDataStructure(/** @scrutinizer ignore-type */ $closure))
Loading history...
481
                    ->setRequestMethod('index'),
482
                $uriSegments
483
            );
484
        } elseif ($closure instanceof KernelControllerDataStructure) {
485
            $this->setController($closure, $action->getClosureParameters());
486
        } elseif (is_array($closure)) {
487
            $this->uri = (new KernelMessageUri())
488
                ->withSegments(new KernelMessageUriSegments(''))
489
                ->withQuery('');
490
            $this->handle($this->uri->addSegments($closure));
491
        } else {
492
            if (class_exists($closure)) {
493
                $this->setController(
494
                    (new KernelControllerDataStructure($closure))
495
                        ->setRequestMethod('index'),
496
                    $uriSegments
497
                );
498
            } elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) {
499
                $this->setController(
500
                    (new KernelControllerDataStructure($matches[ 1 ]))
501
                        ->setRequestMethod($matches[ 3 ]),
502
                    $uriSegments
503
                );
504
            } elseif (presenter()->theme->use === true) {
0 ignored issues
show
Bug introduced by
The property use does not seem to exist on O2System\Framework\Conta...Structures\Module\Theme.
Loading history...
505
                if ( ! presenter()->partials->offsetExists('content') && $closure !== '') {
506
                    presenter()->partials->offsetSet('content', $closure);
507
                }
508
509
                if (presenter()->partials->offsetExists('content')) {
510
                    profiler()->watch('VIEW_SERVICE_RENDER');
511
                    view()->render();
512
                    exit(EXIT_SUCCESS);
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...
513
                } else {
514
                    output()->sendError(204);
515
                    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...
516
                }
517
            } elseif (is_string($closure) && $closure !== '') {
518
                if (is_json($closure)) {
519
                    output()->setContentType('application/json');
520
                    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

520
                    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...
521
                } else {
522
                    output()->send($closure);
523
                }
524
            } elseif (is_array($closure) || is_object($closure)) {
525
                output()->send($closure);
526
            } elseif (is_numeric($closure)) {
527
                output()->sendError($closure);
0 ignored issues
show
Bug introduced by
$closure of type string is incompatible with the type integer expected by parameter $code of O2System\Kernel\Http\Output::sendError(). ( Ignorable by Annotation )

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

527
                output()->sendError(/** @scrutinizer ignore-type */ $closure);
Loading history...
Bug introduced by
$closure of type string is incompatible with the type integer expected by parameter $code of O2System\Kernel\Cli\Output::sendError(). ( Ignorable by Annotation )

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

527
                output()->sendError(/** @scrutinizer ignore-type */ $closure);
Loading history...
528
            } else {
529
                output()->sendError(204);
530
                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...
531
            }
532
        }
533
    }
534
}