1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Scabbia2 Scanners Component |
4
|
|
|
* https://github.com/eserozvataf/scabbia2 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/eserozvataf/scabbia2-scanners for the canonical source repository |
10
|
|
|
* @copyright 2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/) |
11
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Scabbia\Scanners; |
15
|
|
|
|
16
|
|
|
use Scabbia\Helpers\FileSystem; |
17
|
|
|
// use Scabbia\Scanners\ScannerInterface; |
18
|
|
|
use LogicException; |
19
|
|
|
use ReflectionClass; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Scanners registry |
23
|
|
|
* |
24
|
|
|
* @package Scabbia\Scanners |
25
|
|
|
* @author Eser Ozvataf <[email protected]> |
26
|
|
|
* @since 2.0.0 |
27
|
|
|
*/ |
28
|
|
|
class Scanners |
29
|
|
|
{ |
30
|
|
|
/** @type array scanner classes */ |
31
|
|
|
public $scanners = []; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Registers a scanner |
36
|
|
|
* |
37
|
|
|
* @param array|string $uScanners scanner instances |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function register(...$uScanners) |
42
|
|
|
{ |
43
|
|
|
foreach ($uScanners as $tScanner) { |
44
|
|
|
$this->scanners[] = $tScanner; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Scans all files in folders |
50
|
|
|
* |
51
|
|
|
* @param array|string $uFolders folders |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function processFolder(...$uFolders) |
56
|
|
|
{ |
57
|
|
|
foreach ($uFolders as $tFolder) { |
58
|
|
|
FileSystem::getFilesWalk( |
59
|
|
|
$tFolder, |
|
|
|
|
60
|
|
|
"*.php", |
61
|
|
|
true, |
62
|
|
|
[$this, "processFile"] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Scans a file |
69
|
|
|
* |
70
|
|
|
* @param string $uFile file path |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function processFile($uFile) |
75
|
|
|
{ |
76
|
|
|
$tFileContents = FileSystem::read($uFile); |
77
|
|
|
|
78
|
|
|
foreach ($this->scanners as $tScanner) { |
79
|
|
|
$tScanner->processFile($uFile, $tFileContents); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$tTokenStream = TokenStream::fromString($tFileContents); |
|
|
|
|
83
|
|
|
$this->processTokenStream($tTokenStream); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Scans a token stream |
88
|
|
|
* |
89
|
|
|
* @param TokenStream $uTokenStream extracted tokens wrapped with tokenstream |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function processTokenStream(TokenStream $uTokenStream) |
94
|
|
|
{ |
95
|
|
|
foreach ($this->scanners as $tScanner) { |
96
|
|
|
$tScanner->processTokenStream($uTokenStream); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$tBuffer = ""; |
100
|
|
|
|
101
|
|
|
$tUses = []; |
102
|
|
|
$tLastNamespace = null; |
103
|
|
|
$tLastClass = null; |
104
|
|
|
$tLastClassDerivedFrom = null; |
105
|
|
|
$tExpectation = 0; // 1=namespace, 2=class |
|
|
|
|
106
|
|
|
|
107
|
|
|
foreach ($uTokenStream as $tToken) { |
108
|
|
|
if ($tToken[0] === T_WHITESPACE) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($tExpectation === 0) { |
113
|
|
|
if ($tToken[0] === T_NAMESPACE) { |
114
|
|
|
$tBuffer = ""; |
115
|
|
|
$tExpectation = 1; |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($tToken[0] === T_CLASS) { |
120
|
|
|
$tExpectation = 2; |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($tToken[0] === T_USE) { |
125
|
|
|
$tBuffer = ""; |
126
|
|
|
$tExpectation = 5; |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
View Code Duplication |
} elseif ($tExpectation === 1) { |
|
|
|
|
130
|
|
|
if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) { |
131
|
|
|
$tBuffer .= $tToken[1]; |
132
|
|
|
} else { |
133
|
|
|
$tLastNamespace = $tBuffer; |
134
|
|
|
$tExpectation = 0; |
135
|
|
|
} |
136
|
|
|
} elseif ($tExpectation === 2) { |
137
|
|
|
$tLastClass = "{$tLastNamespace}\\{$tToken[1]}"; |
138
|
|
|
$tExpectation = 3; |
139
|
|
|
} elseif ($tExpectation === 3) { |
140
|
|
|
if ($tToken[0] === T_EXTENDS) { |
141
|
|
|
$tBuffer = ""; |
142
|
|
|
$tExpectation = 4; |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$tSkip = false; |
147
|
|
|
if ($tLastClassDerivedFrom !== null && !class_exists($tLastClassDerivedFrom)) { |
148
|
|
|
$tSkip = true; |
|
|
|
|
149
|
|
|
throw new LogicException(sprintf( |
150
|
|
|
"\"%s\" derived from \"%s\", but it could not be found.\n", |
151
|
|
|
$tLastClass, |
152
|
|
|
$tLastClassDerivedFrom |
153
|
|
|
)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!$tSkip && !isset($this->result[$tLastClass])) { |
|
|
|
|
157
|
|
|
$this->processClass($tLastClass); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$tExpectation = 0; |
161
|
|
|
} elseif ($tExpectation === 4) { |
162
|
|
|
if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) { |
163
|
|
|
$tBuffer .= $tToken[1]; |
164
|
|
|
} else { |
165
|
|
|
$tFound = false; |
166
|
|
|
|
167
|
|
|
foreach ($tUses as $tUse) { |
168
|
|
|
$tLength = strlen($tBuffer); |
169
|
|
|
if (strlen($tUse) >= $tLength && substr($tUse, -$tLength) === $tBuffer) { |
170
|
|
|
$tLastClassDerivedFrom = $tUse; |
171
|
|
|
$tFound = true; |
172
|
|
|
break; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (!$tFound) { |
177
|
|
|
if (strpos($tBuffer, "\\") !== false) { |
178
|
|
|
$tLastClassDerivedFrom = $tBuffer; |
179
|
|
|
} else { |
180
|
|
|
$tLastClassDerivedFrom = "{$tLastNamespace}\\{$tBuffer}"; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$tExpectation = 3; |
185
|
|
|
} |
186
|
|
View Code Duplication |
} elseif ($tExpectation === 5) { |
|
|
|
|
187
|
|
|
if ($tToken[0] === T_STRING || $tToken[0] === T_NS_SEPARATOR) { |
188
|
|
|
$tBuffer .= $tToken[1]; |
189
|
|
|
} else { |
190
|
|
|
$tUses[] = $tBuffer; |
191
|
|
|
$tExpectation = 0; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Processes classes using reflection |
199
|
|
|
* |
200
|
|
|
* @param string $uClass class name |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public function processClass($uClass) |
205
|
|
|
{ |
206
|
|
|
$tReflection = new ReflectionClass($uClass); |
207
|
|
|
|
208
|
|
|
foreach ($this->scanners as $tScanner) { |
209
|
|
|
$tScanner->processClass($uClass, $tReflection); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.