RoutelessActions   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getControllerActions() 0 25 5
A getNamespacedClassName() 0 9 1
A isLaravelController() 0 9 2
A getFullNamespace() 0 6 1
A findOrphanActions() 0 17 3
A check() 0 4 1
A checkControllerActionsForRoutes() 0 20 4
A classAtMethod() 0 6 2
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Checks;
4
5
use Illuminate\Routing\Controller;
6
use Imanghafoori\LaravelMicroscope\Analyzers\ClassMethods;
7
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector;
8
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
9
10
class RoutelessActions
11
{
12
    public static function getControllerActions($methods)
13
    {
14
        $orphanMethods = [];
15
        foreach ($methods as $method) {
16
            // we exclude non-public methods
17
            if ($method['visibility'][0] !== T_PUBLIC) {
18
                continue;
19
            }
20
21
            // we exclude static methods
22
            if ($method['is_static']) {
23
                continue;
24
            }
25
26
            $methodName = $method['name'][1];
27
            // we exclude __construct
28
            if ($methodName == '__construct') {
29
                continue;
30
            }
31
32
            $orphanMethods[] = $method;
33
        }
34
35
        return $orphanMethods;
36
    }
37
38
    public static function getNamespacedClassName($classFilePath, $psr4Path, $psr4Namespace)
39
    {
40
        $absFilePath = $classFilePath->getRealPath();
41
        $className = $classFilePath->getFilename();
42
        $relativePath = \str_replace(base_path(), '', $absFilePath);
43
        $namespace = NamespaceCorrector::calculateCorrectNamespace($relativePath, $psr4Path, $psr4Namespace);
44
45
        return $namespace.'\\'.$className;
46
    }
47
48
    public static function isLaravelController($fullNamespace)
49
    {
50
        try {
51
            return is_subclass_of($fullNamespace, Controller::class);
52
        } catch (\Throwable $r) {
53
            // it means the file does not contain a class or interface.
54
            return false;
55
        }
56
    }
57
58
    public static function getFullNamespace($classFilePath, $psr4Path, $psr4Namespace)
59
    {
60
        $fullNamespace = self::getNamespacedClassName($classFilePath, $psr4Path, $psr4Namespace);
61
62
        return \trim($fullNamespace, '.php');
63
    }
64
65
    protected function findOrphanActions($tokens, $fullNamespace)
66
    {
67
        $class = ClassMethods::read($tokens);
68
69
        $methods = self::getControllerActions($class['methods']);
70
        $routelessActions = [];
71
        foreach ($methods as $method) {
72
            $classAtMethod = self::classAtMethod($fullNamespace, $method['name'][1]);
73
74
            if (! app('router')->getRoutes()->getByAction($classAtMethod)) {
75
                $line = $method['name'][2];
76
                $routelessActions[] = [$line, $classAtMethod];
77
            }
78
        }
79
80
        return $routelessActions;
81
    }
82
83
    public static function check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace)
84
    {
85
        (new self())->checkControllerActionsForRoutes($classFilePath, $psr4Path, $psr4Namespace, $tokens, $absFilePath);
86
    }
87
88
    public function checkControllerActionsForRoutes($classFilePath, $psr4Path, $psr4Namespace, $tokens, $absFilePath)
89
    {
90
        $errorPrinter = resolve(ErrorPrinter::class);
91
        $fullNamespace = self::getFullNamespace($classFilePath, $psr4Path, $psr4Namespace);
92
93
        if (! self::isLaravelController($fullNamespace)) {
94
            return;
95
        }
96
97
        // exclude abstract class
98
        if ((new \ReflectionClass($fullNamespace))->isAbstract()) {
99
            return;
100
        }
101
102
        $actions = $this->findOrphanActions($tokens, $fullNamespace);
103
104
        foreach ($actions as $action) {
105
            $errorPrinter->routelessAction($absFilePath, $action[0], $action[1]);
106
        }
107
    }
108
109
    public static function classAtMethod($fullNamespace, $methodName)
110
    {
111
        ($methodName == '__invoke') ? ($methodName = '') : ($methodName = '@'.$methodName);
112
113
        return \trim($fullNamespace, '\\').$methodName;
114
    }
115
}
116