Route::run()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 10
c 2
b 1
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 5
nop 0
1
<?php
2
3
namespace App\Ship;
4
5
use Rudra\Container\Facades\Rudra;
6
use Rudra\Exceptions\RouterException;
7
use Rudra\Router\RouterFacade as Router;
8
9
class Route
10
{
11
    /**
12
     * @throws RouterException
13
     */
14
    public function run()
15
    {
16
        if (config('environment') === 'development') {
17
            $timeCollector = Rudra::get("debugbar")['time'];
18
19
            if ($timeCollector->hasStartedMeasure('index')) {
20
                $timeCollector->stopMeasure('index');
21
            }
22
23
            if (!$timeCollector->hasStartedMeasure('routing')) {
24
                $timeCollector->startMeasure('routing');
25
            }
26
        }
27
28
        $this->collect(config('containers'));
29
        throw new RouterException("404");
30
31
        if (config('environment') !== 'test') {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
32
            exit();
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...
33
        }
34
    }
35
36
    protected function collect(array $namespaces): void
37
    {
38
        foreach ($namespaces as $container => $item) {
39
            $routes     = $this->getRoutes($container);
40
            $flatRoutes = array_merge(...$routes);
41
42
            foreach ($flatRoutes as $route) {
43
                Router::set($route);
44
            }
45
        }
46
    }
47
48
    protected function getRoutes(string $container): array
49
    {
50
        $cacheDir  = "../app/cache";
51
        $cacheTime = config('cache.time', 'routes');
52
        $routesDir = $cacheDir . "/routes";
53
        $cacheFile = $routesDir . "/routes_" . $container . ".php";
54
        $cacheLifetime = strtotime($cacheTime) - time();
55
56
        if (!is_dir($cacheDir)) {
57
            mkdir($cacheDir, 0755, true);
58
        }
59
60
        if (!is_dir($routesDir)) {
61
            mkdir($routesDir, 0755, true);
62
        }
63
64
        if (file_exists($cacheFile)) {
65
            $cacheTime   = filemtime($cacheFile);
66
            $currentTime = time();
67
68
            if ($currentTime - $cacheTime < $cacheLifetime) {
69
                return include $cacheFile;
70
            }
71
        }
72
73
        $path = "../app/Containers/" . ucfirst($container) . "/routes";
74
75
        if (!file_exists($path . ".php")) {
76
            throw new \RuntimeException("Routes file not found for container: " . $container);
77
        }
78
79
        // Генерация маршрутов и сохранение их в кеш
80
        $routes = Router::annotationCollector(require_once $path . ".php", true, Rudra::config()->get("attributes"));
0 ignored issues
show
Unused Code introduced by
The call to Rudra\Router\RouterFacade::annotationCollector() has too many arguments starting with true. ( 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
        $routes = Router::annotationCollector(require_once $path . ".php", true, Rudra::config()->get("attributes"));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
81
        file_put_contents($cacheFile, "<?php\nreturn " . var_export($routes, true) . ";");
82
83
        return $routes;
84
    }
85
}
86