CheckRoutes::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Str;
10
use Imanghafoori\LaravelMicroscope\BladeFiles;
11
use Imanghafoori\LaravelMicroscope\Checks\ActionsComments;
12
use Imanghafoori\LaravelMicroscope\Checks\CheckRouteCalls;
13
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
14
use Imanghafoori\LaravelMicroscope\Psr4Classes;
15
use Imanghafoori\LaravelMicroscope\Traits\LogsErrors;
16
17
class CheckRoutes extends Command
18
{
19
    use LogsErrors;
20
21
    public static $checkedRoutesNum = 0;
22
23
    public static $skippedRoutesNum = 0;
24
25
    protected $signature = 'check:routes';
26
27
    protected $description = 'Checks the validity of route definitions';
28
29
    public function handle(ErrorPrinter $errorPrinter)
30
    {
31
        event('microscope.start.command');
32
        $this->info('Checking route definitions...');
33
34
        $errorPrinter->printer = $this->output;
35
        app(Filesystem::class)->delete(app()->getCachedRoutesPath());
36
37
        $routes = app(Router::class)->getRoutes()->getRoutes();
38
        $this->checkRouteDefinitions($errorPrinter, $routes);
39
        // checks calls like this: route('admin.user')
40
        $this->getOutput()->writeln(
41
            ' - '.CheckRoutes::$checkedRoutesNum.
42
            ' Route:: definitions were checked. ('.
43
            CheckRoutes::$skippedRoutesNum.' skipped)'
44
        );
45
        $this->info('Checking route names exists...');
46
        Psr4Classes::check([CheckRouteCalls::class]);
47
        BladeFiles::check([CheckRouteCalls::class]);
48
49
        $this->getOutput()->writeln(' - '.CheckRouteCalls::$checkedRouteCallsNum.
50
            ' route(...) calls were checked. ('
51
            .CheckRouteCalls::$skippedRouteCallsNum.' skipped)');
52
53
        event('microscope.finished.checks', [$this]);
54
55
        return $errorPrinter->hasErrors() ? 1 : 0;
56
    }
57
58
    private function getRouteId($route)
59
    {
60
        if ($routeName = $route->getName()) {
61
            $msg = 'name: "'.$routeName.'"';
62
        } else {
63
            $msg = 'url: "'.$route->uri().'"';
64
        }
65
66
        return $msg;
67
    }
68
69
    private function checkRouteDefinitions($errorPrinter, $routes)
70
    {
71
        foreach ($routes as $route) {
72
            if (! is_string($ctrl = $route->getAction()['uses'])) {
73
                self::$skippedRoutesNum++;
74
                continue;
75
            }
76
77
            self::$checkedRoutesNum++;
78
            [$ctrlClass, $method] = Str::parseCallback($ctrl, '__invoke');
0 ignored issues
show
Bug introduced by
The variable $ctrlClass does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
79
80
            try {
81
                $ctrlObj = app()->make($ctrlClass);
82
            } catch (Exception $e) {
83
                $msg1 = $this->getRouteId($route);
84
                $msg2 = 'The controller can not be resolved: ('.$msg1.')';
85
                [$path, $line] = ActionsComments::getCallsiteInfo($route->methods()[0], $route);
0 ignored issues
show
Bug introduced by
The variable $path does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $line does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
86
                $errorPrinter->route($ctrlClass, $msg2, '', $path, $line);
87
88
                continue;
89
            }
90
91
            if (! method_exists($ctrlObj, $method)) {
92
                $msg2 = 'Absent method for route'.' '.$this->getRouteId($route);
93
                [$path, $line] = ActionsComments::getCallsiteInfo($route->methods()[0], $route);
94
                $errorPrinter->route($ctrl, $msg2, '', $path, $line);
95
            }
96
        }
97
    }
98
}
99