|
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) |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.