ActionsComments::getMsg()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 24
nop 2
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Checks;
4
5
use Imanghafoori\LaravelMicroscope\Analyzers\ClassMethods;
6
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor;
7
8
class ActionsComments
9
{
10
    public static $command;
11
12
    public static function check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace)
0 ignored issues
show
Unused Code introduced by
The parameter $absFilePath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
    {
14
        self::checkControllerActionsForRoutes($classFilePath, $psr4Path, $psr4Namespace, $tokens);
15
    }
16
17
    public static function checkControllerActionsForRoutes($classFilePath, $psr4Path, $psr4Namespace, $tokens)
18
    {
19
//        $errorPrinter = resolve(ErrorPrinter::class);
20
        $fullNamespace = RoutelessActions::getFullNamespace($classFilePath, $psr4Path, $psr4Namespace);
21
22
        if (RoutelessActions::isLaravelController($fullNamespace)) {
23
            $actions = self::checkActions($tokens, $fullNamespace, $classFilePath);
0 ignored issues
show
Unused Code introduced by
$actions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
24
25
            /*foreach ($actions as $action) {
26
                $errorPrinter->routelessAction($absFilePath, $action[0], $action[1]);
27
            }*/
28
        }
29
    }
30
31
    protected static function checkActions($tokens, $fullNamespace, $path)
32
    {
33
        $class = ClassMethods::read($tokens);
34
35
        $methods = RoutelessActions::getControllerActions($class['methods']);
36
        $routelessActions = [];
37
        $shouldSave = false;
38
39
        foreach ($methods as $method) {
40
            $classAtMethod = RoutelessActions::classAtMethod($fullNamespace, $method['name'][1]);
41
42
            if (! ($route = app('router')->getRoutes()->getByAction($classAtMethod))) {
43
                continue;
44
            }
45
46
            /**
47
             * @var $route \Illuminate\Routing\Route
48
             */
49
            $methods = $route->methods();
50
            ($methods == ['GET', 'HEAD']) && $methods = ['GET'];
51
            $msg = self::getMsg($methods, $route);
52
53
            $commentIndex = $method['startBodyIndex'][0] + 1;
54
55
            if (T_DOC_COMMENT !== $tokens[$commentIndex + 1][0]) {
56
                $shouldSave = true;
57
                $tokens[$commentIndex][1] = "\n        ".$msg.$tokens[$commentIndex][1];
58
            } elseif ($msg !== $tokens[$commentIndex + 1][1]) {
59
                // if the docblock is there, but needs update...
60
                $shouldSave = true;
61
                $tokens[$commentIndex + 1][1] = $msg;
62
            }
63
64
            $line = $method['name'][2];
65
            $routelessActions[] = [$line, $classAtMethod];
66
        }
67
68
        $question = 'Do you want to add route definition to: '.$fullNamespace;
69
        if ($shouldSave && (self::$command)->confirm($question, true)) {
70
            Refactor::saveTokens($path->getRealpath(), $tokens);
71
        }
72
73
        return $routelessActions;
74
    }
75
76
    protected static function getMsg($methods, $route)
77
    {
78
        $msg = '/**'."\n";
79
        $prefix = '         * ';
80
81
        [$file, $line] = self::getCallsiteInfo($methods[0], $route);
0 ignored issues
show
Bug introduced by
The variable $file 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...
82
83
        if ($file) {
84
            $msg .= $prefix.'@at('.$file.':'.$line.')';
85
        }
86
87
        $nameBlock = $prefix."@name('".($route->getName() ?: '')."')";
88
        $msg .= "\n".$prefix;
89
        if (\count($methods) > 1) {
90
            $msg .= '@methods('.\implode(', ', $methods).')'."\n".$prefix.'@uri(\'/'.$route->uri().'\')'."\n".$nameBlock;
91
        } else {
92
            $msg .= '@'.strtolower(\implode('', $methods)).'(\'/'.$route->uri().'\')'."\n".$nameBlock;
93
        }
94
95
        $middlewares = $route->gatherMiddleware();
96
97
        foreach ($middlewares as $i => $m) {
98
            if (! is_string($m)) {
99
                $middlewares[$i] = 'Closure';
100
            }
101
        }
102
        $msg .= "\n".$prefix.'@middlewares('.\implode(', ', $middlewares).')';
103
104
        $msg .= "\n         */";
105
106
        return $msg;
107
    }
108
109
    public static function getCallsiteInfo($methods, $route)
110
    {
111
        $callsite = app('router')->getRoutes()->routesInfo[$methods][$route->uri()] ?? [];
112
        $file = $callsite[0]['file'] ?? '';
113
        $line = $callsite[0]['line'] ?? '';
114
        $file = \trim(str_replace(base_path(), '', $file), '\\/');
115
116
        return [$file, $line];
117
    }
118
}
119