1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Analyzers; |
4
|
|
|
|
5
|
|
|
class TokenManager |
6
|
|
|
{ |
7
|
|
|
public static function removeTokens($tokens, $from, $to, $at) |
8
|
|
|
{ |
9
|
|
|
$refactoredTokens = []; |
10
|
|
|
foreach ($tokens as $i => $oldToken) { |
11
|
|
|
if ($i > $from && $i <= $to) { |
12
|
|
|
continue; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
if ($i == $at) { |
16
|
|
|
continue; |
17
|
|
|
} |
18
|
|
|
$refactoredTokens[] = $oldToken; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
return $refactoredTokens; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function getNextToken($tokens, $i) |
25
|
|
|
{ |
26
|
|
|
$i++; |
27
|
|
|
$token = $tokens[$i] ?? '_'; |
28
|
|
|
while ($token[0] == T_WHITESPACE || $token[0] == T_COMMENT) { |
29
|
|
|
$i++; |
30
|
|
|
$token = $tokens[$i] ?? [null, null]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return [$token, $i]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getPrevToken($tokens, $i) |
37
|
|
|
{ |
38
|
|
|
$i--; |
39
|
|
|
$token = $tokens[$i]; |
40
|
|
|
while ($token[0] == T_WHITESPACE || $token[0] == T_COMMENT) { |
41
|
|
|
$i--; |
42
|
|
|
$token = $tokens[$i]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return [$token, $i]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function forwardTo($tokens, $i, $tokenType) |
49
|
|
|
{ |
50
|
|
|
$i++; |
51
|
|
|
$nextToken = $tokens[$i] ?? '_'; |
52
|
|
|
while (! \in_array($nextToken[0], $tokenType)) { |
53
|
|
|
$i++; |
54
|
|
|
$nextToken = $tokens[$i] ?? [null, null]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return [$nextToken, $i]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function readBodyBack(&$tokens, $i) |
61
|
|
|
{ |
62
|
|
|
$body = []; |
63
|
|
|
$level = 0; |
64
|
|
|
while (true) { |
65
|
|
|
[$token, $i] = self::getPrevToken($tokens, $i); |
|
|
|
|
66
|
|
|
|
67
|
|
|
if (\in_array($token[0], [']', ')', '}'])) { |
|
|
|
|
68
|
|
|
$level--; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$isOpening = \in_array($token[0], ['[', '(', '{', T_CURLY_OPEN]); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($level == 0 && $isOpening) { |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($isOpening) { |
78
|
|
|
$level++; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$body[] = $token; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [$body, $i]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function readBody(&$tokens, $i, $until = '}') |
88
|
|
|
{ |
89
|
|
|
$body = []; |
90
|
|
|
$level = 0; |
91
|
|
|
while (true) { |
92
|
|
|
$i++; |
93
|
|
|
$nextToken = $tokens[$i] ?? '_'; |
94
|
|
|
|
95
|
|
|
if ($nextToken == '_') { |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($level == 0 && $nextToken[0] == $until) { |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (\in_array($nextToken[0], ['[', '(', '{', T_CURLY_OPEN])) { |
104
|
|
|
$level++; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (\in_array($nextToken[0], [']', ')', '}'])) { |
108
|
|
|
$level--; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$body[] = $nextToken; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return [$body, $i]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public static function readBackUntil(&$tokens, $i, $chars = ['}']) |
118
|
|
|
{ |
119
|
|
|
$orphanBlock = []; |
120
|
|
|
while (true) { |
121
|
|
|
[$token, $i] = self::getPrevToken($tokens, $i); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$depth = 0; |
124
|
|
|
if (\in_array($token[0], $chars)) { |
|
|
|
|
125
|
|
|
[$ifBody, $openIfIndex] = self::readBodyBack($tokens, $i); |
|
|
|
|
126
|
|
|
[, $closeParenIndex] = self::getPrevToken($tokens, $openIfIndex); |
|
|
|
|
127
|
|
|
[$condition, $openParenIndex] = self::readBodyBack($tokens, $closeParenIndex); |
|
|
|
|
128
|
|
|
[$ownerOfClosing] = self::getPrevToken($tokens, $openParenIndex); |
|
|
|
|
129
|
|
|
|
130
|
|
|
if ($ownerOfClosing[0] == T_IF) { |
131
|
|
|
break; |
132
|
|
|
} else { |
133
|
|
|
return [null, null]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($token[0] == '{') { |
|
|
|
|
138
|
|
|
$depth--; |
139
|
|
|
|
140
|
|
|
if ($depth === -1) { |
141
|
|
|
return [null, null]; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$orphanBlock[] = $token; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return [[$ifBody, [$openIfIndex, $i]], [$condition, [$openParenIndex, $closeParenIndex]], $orphanBlock, $i]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.