|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Checks\PSR12; |
|
4
|
|
|
|
|
5
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor; |
|
6
|
|
|
|
|
7
|
|
|
class CurlyBraces |
|
8
|
|
|
{ |
|
9
|
|
|
public static $command; |
|
10
|
|
|
|
|
11
|
|
|
public static function check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace) |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
self::removeGenericDocBlocks($tokens, $classFilePath->getRealpath()); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
private static function removeGenericDocBlocks($tokens, $classFilePath) |
|
17
|
|
|
{ |
|
18
|
|
|
$level = 0; |
|
19
|
|
|
$isInSideClass = false; |
|
20
|
|
|
$ct = \count($tokens); |
|
21
|
|
|
$i = 0; |
|
22
|
|
|
while ($i < $ct - 1) { |
|
23
|
|
|
$i++; |
|
24
|
|
|
$token = $tokens[$i]; |
|
25
|
|
|
\in_array($token[0], [T_CURLY_OPEN, '{']) && $level++; |
|
26
|
|
|
($token[0] == '}') && $level--; |
|
27
|
|
|
if ($level == 0) { |
|
28
|
|
|
if (\in_array($token[0], [T_CLASS, T_TRAIT, T_INTERFACE])) { |
|
29
|
|
|
if ($tokens[$i - 1] != T_DOUBLE_COLON) { |
|
30
|
|
|
$isInSideClass = true; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
self::openCurly($token, $level, $tokens, $i, $classFilePath); |
|
35
|
|
|
|
|
36
|
|
|
[$tokens, $i] = self::writePublic($level, $token, $isInSideClass, $i, $tokens, $classFilePath); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private static function openCurly($token, $level, $tokens, $i, $classFilePath) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($token == '{' && ! \in_array($tokens[$i - 1][0], [T_DOUBLE_COLON, T_OBJECT_OPERATOR])) { |
|
43
|
|
|
$sp = str_repeat(' ', $level); |
|
44
|
|
|
if ($tokens[$i + 1][0] == T_WHITESPACE) { |
|
45
|
|
|
if ($tokens[$i + 1][1] != PHP_EOL.$sp && $tokens[$i + 1][1] != "\n".$sp) { |
|
46
|
|
|
$tokens[$i + 1][1] = PHP_EOL.$sp; |
|
47
|
|
|
Refactor::saveTokens($classFilePath, $tokens); |
|
48
|
|
|
} else { |
|
49
|
|
|
/// |
|
50
|
|
|
} |
|
51
|
|
|
} else { |
|
52
|
|
|
array_splice($tokens, $i + 1, 0, [[T_WHITESPACE, PHP_EOL.$sp]]); |
|
53
|
|
|
Refactor::saveTokens($classFilePath, $tokens); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private static function writePublic($level, $token, $isInClass, $i, $tokens, $absolutePath) |
|
59
|
|
|
{ |
|
60
|
|
|
if (($level != 1) || ($token[0] != T_FUNCTION) || ! $isInClass) { |
|
61
|
|
|
return [$tokens, $i]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$t = $i; |
|
65
|
|
|
if (\in_array($tokens[$t - 2][0], [T_STATIC])) { |
|
66
|
|
|
$t = $t - 2; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (! \in_array($tokens[$t - 2][0], [T_PUBLIC, T_PROTECTED, T_PRIVATE])) { |
|
70
|
|
|
array_splice($tokens, $t, 0, [[T_WHITESPACE, ' ']]); |
|
71
|
|
|
array_splice($tokens, $t, 0, [[T_PUBLIC, 'public']]); |
|
72
|
|
|
$i++; |
|
73
|
|
|
$i++; |
|
74
|
|
|
Refactor::saveTokens($absolutePath, $tokens); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return [$tokens, $i]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.