SpyRouter::addRoute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 7
Ratio 46.67 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 3
dl 7
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\SpyClasses;
4
5
use Closure;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Str;
8
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector;
9
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
10
11
class SpyRouter extends Router
12
{
13
    public $routePaths = [];
14
15
    /**
16
     * @var \Imanghafoori\LaravelMicroscope\SpyClasses\SpyRouteCollection
17
     */
18
    private $routesSpy = null;
19
20
    public function spyRouteConflict()
21
    {
22
        $this->routesSpy = $this->routes = new SpyRouteCollection();
23
    }
24
25
    protected function loadRoutes($routes)
26
    {
27
        // This is needed to collect the route paths to tokenize and run inspections.
28
        ! ($routes instanceof Closure) && $this->routePaths[] = $routes;
29
30
        parent::loadRoutes($routes);
31
    }
32
33
    public function updateGroupStack(array $attributes)
34
    {
35
        parent::updateGroupStack($attributes);
36
37
        $e = $this->groupStack;
38
        $newAttr = end($e);
39
40
        $i = 2;
41 View Code Duplication
        while (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            ($info = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $i + 1)[$i])
43
            &&
44
            $this->isExcluded($info)
45
        ) {
46
            $i++;
47
        }
48
        $ns = $newAttr['namespace'] ?? null;
49
        $dir = NamespaceCorrector::getRelativePathFromNamespace($ns);
50
51
        if (isset($attributes['middlewares'])) {
52
            $err = "['middlewares' => ...] key passed to Route::group(...) is not correct.";
53
            $this->routeError($info, $err, "Incorrect 'middlewares' key.");
54
        }
55
56
        if ($ns && isset($attributes['namespace']) && ! is_dir($dir) && \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $ns) !== $dir) {
57
            $err = "['namespace' => "."'".$attributes['namespace'].'\'] passed to Route::group(...) is not correct.';
58
            $this->routeError($info, $err, 'Incorrect namespace.');
59
        }
60
    }
61
62
    public function routeError($info, $err, $msg)
63
    {
64
        app(ErrorPrinter::class)->route(
65
            null,
66
            $msg,
67
            $err,
68
            $info['file'] ?? '',
69
            $info['line'] ?? 1
70
        );
71
    }
72
73
    private function isExcluded($info)
74
    {
75
        return Str::startsWith(($info['file'] ?? ''), [
76
            base_path('vendor'.DIRECTORY_SEPARATOR.'laravel'),
77
            base_path('vendor'.DIRECTORY_SEPARATOR.'imanghafoori'),
78
        ]);
79
    }
80
81
    public function addRoute($methods, $uri, $action)
82
    {
83
        $i = 2;
84 View Code Duplication
        while (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            ($info = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $i + 1)[$i])
86
            &&
87
            $this->isExcluded($info)
88
        ) {
89
            $i++;
90
        }
91
        $routeObj = $this->createRoute($methods, $uri, $action);
92
        $this->routesSpy && $this->routesSpy->addCallSiteInfo($routeObj, $info);
93
94
        return $this->routes->add($routeObj);
95
    }
96
}
97