|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Analyzers; |
|
4
|
|
|
|
|
5
|
|
|
class ClassMethods |
|
6
|
|
|
{ |
|
7
|
|
|
public static function read($tokens) |
|
8
|
|
|
{ |
|
9
|
|
|
$i = 0; |
|
10
|
|
|
$class = [ |
|
11
|
|
|
'name' => null, |
|
12
|
|
|
'methods' => [], |
|
13
|
|
|
'type' => '', |
|
14
|
|
|
]; |
|
15
|
|
|
$methods = []; |
|
16
|
|
|
|
|
17
|
|
|
while (isset($tokens[$i])) { |
|
18
|
|
|
$token = $tokens[$i]; |
|
19
|
|
|
|
|
20
|
|
|
if ($token[0] == T_CLASS && $tokens[$i - 1][0] !== T_DOUBLE_COLON) { |
|
21
|
|
|
$class['name'] = $tokens[$i + 2]; |
|
22
|
|
|
$class['type'] = T_CLASS; |
|
23
|
|
|
$class['is_abstract'] = ($tokens[$i - 2][0] === T_ABSTRACT); |
|
24
|
|
|
} elseif ($token[0] == T_INTERFACE) { |
|
25
|
|
|
$class['name'] = $tokens[$i + 2]; |
|
26
|
|
|
$class['type'] = T_INTERFACE; |
|
27
|
|
|
} elseif ($token[0] == T_TRAIT) { |
|
28
|
|
|
$class['name'] = $tokens[$i + 2]; |
|
29
|
|
|
$class['type'] = T_TRAIT; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($class['name'] === null || $tokens[$i][0] != T_FUNCTION) { |
|
33
|
|
|
$i++; |
|
34
|
|
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (! \is_array($name = $tokens[$i + 2])) { |
|
38
|
|
|
$i++; |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
[$visibility, $isStatic, $isAbstract] = self::findVisibility($tokens, $i - 2); |
|
|
|
|
|
|
43
|
|
|
[, $signature, $endSignature] = Ifs::readCondition($tokens, $i + 2); |
|
|
|
|
|
|
44
|
|
|
[$char, $charIndex] = FunctionCall::forwardTo($tokens, $endSignature, [':', ';', '{']); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
[$returnType, $hasNullableReturnType, $char, $charIndex] = self::processReturnType($char, $tokens, $charIndex); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if ($char == '{') { |
|
49
|
|
|
[$body, $i] = FunctionCall::readBody($tokens, $charIndex); |
|
|
|
|
|
|
50
|
|
|
} elseif ($char == ';') { |
|
51
|
|
|
$body = []; |
|
52
|
|
|
} else { |
|
53
|
|
|
$code = Refactor::toString($tokens); |
|
54
|
|
|
self::requestIssue($code); |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$i++; |
|
59
|
|
|
$methods[] = [ |
|
60
|
|
|
'name' => $name, |
|
61
|
|
|
'visibility' => $visibility, |
|
62
|
|
|
'signature' => $signature, |
|
63
|
|
|
'body' => Refactor::toString($body), |
|
64
|
|
|
'startBodyIndex' => [$charIndex, $i], |
|
65
|
|
|
'returnType' => $returnType, |
|
66
|
|
|
'nullable_return_type' => $hasNullableReturnType, |
|
67
|
|
|
'is_static' => $isStatic, |
|
68
|
|
|
'is_abstract' => $isAbstract, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$class['methods'] = $methods; |
|
73
|
|
|
|
|
74
|
|
|
return $class; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private static function requestIssue($content) |
|
78
|
|
|
{ |
|
79
|
|
|
dump('(O_o) Well, It seems we had some problem parsing the contents of: (o_O)'); |
|
80
|
|
|
dump('Submit an issue on github: https://github.com/imanghafoori1/microscope'); |
|
81
|
|
|
dump('Send us the content and mention your php version ('.phpversion().')'); |
|
82
|
|
|
dump($content); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private static function findVisibility($tokens, $i) |
|
86
|
|
|
{ |
|
87
|
|
|
$isStatic = $tokens[$i][0] === T_STATIC && $i -= 2; |
|
88
|
|
|
$isAbstract = $tokens[$i][0] === T_ABSTRACT && $i -= 2; |
|
89
|
|
|
|
|
90
|
|
|
$hasModifier = \in_array($tokens[$i][0], [T_PUBLIC, T_PROTECTED, T_PRIVATE]); |
|
91
|
|
|
$visibility = $hasModifier ? $tokens[$i] : [T_PUBLIC, 'public']; |
|
92
|
|
|
|
|
93
|
|
|
// We have to cover both syntax: |
|
94
|
|
|
// public abstract function x() { |
|
95
|
|
|
// abstract public function x() { |
|
96
|
|
|
! $isAbstract && $isAbstract = $tokens[$i - 2][0] === T_ABSTRACT; |
|
97
|
|
|
|
|
98
|
|
|
return [$visibility, $isStatic, $isAbstract]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private static function processReturnType($char, $tokens, $charIndex) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($char != ':') { |
|
104
|
|
|
return [null, null, $char, $charIndex]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
[$returnType, $returnTypeIndex] = FunctionCall::getNextToken($tokens, $charIndex); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
// In case the return type is like this: function c() : ?string {... |
|
110
|
|
|
$hasNullableReturnType = ($returnType == '?'); |
|
111
|
|
|
|
|
112
|
|
|
if ($hasNullableReturnType) { |
|
113
|
|
|
[$returnType, $returnTypeIndex] = FunctionCall::getNextToken($tokens, $returnTypeIndex); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
[$char, $charIndex] = FunctionCall::getNextToken($tokens, $returnTypeIndex); |
|
117
|
|
|
|
|
118
|
|
|
return [$returnType, $hasNullableReturnType, $char, $charIndex]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.