1 | <?php |
||
9 | class LaravelEnvScanner |
||
10 | { |
||
11 | /** |
||
12 | * The results of performed scan |
||
13 | * |
||
14 | * @var array |
||
15 | */ |
||
16 | public $results = [ |
||
17 | 'locations' => 0, |
||
18 | 'defined' => 0, |
||
19 | 'undefined' => 0, |
||
20 | 'depending_on_default' => 0, |
||
21 | 'columns' => [], |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Stores processed file and var names |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $processed = [ |
||
30 | 'variables' => [], |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * Stores undefined var names |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | public $undefined = []; |
||
39 | |||
40 | /** |
||
41 | * Stores warnings for vars not passing validation |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | public $warnings = []; |
||
46 | |||
47 | /** |
||
48 | * Current file being processed |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $file; |
||
53 | |||
54 | /** |
||
55 | * Current location a found invocation |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $location; |
||
60 | |||
61 | /** |
||
62 | * Current invocation being processed |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $invocation; |
||
67 | |||
68 | /** |
||
69 | * Current parameters being processed |
||
70 | * |
||
71 | * @var object |
||
72 | */ |
||
73 | private $parameters; |
||
74 | |||
75 | /** |
||
76 | * Root directory to start recursive search for env()'s from |
||
77 | * Defaults to config_path() |
||
78 | * |
||
79 | * @var string $dir |
||
80 | */ |
||
81 | public $dir; |
||
82 | |||
83 | public function __construct(string $dir = null) |
||
87 | |||
88 | /** |
||
89 | * Run the scan |
||
90 | * |
||
91 | * @return mixed |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | public function scan() |
||
115 | |||
116 | /** |
||
117 | * Search for possible matches and make something usable out of it |
||
118 | * |
||
119 | * @param array $lines |
||
120 | * @param string $line |
||
121 | * @param int $index |
||
122 | * @return bool |
||
123 | */ |
||
124 | private function setInvocationDetails(array $lines, string $line, int $index): bool |
||
148 | |||
149 | /** |
||
150 | * Search for single and multi-lined env and getenv invocations |
||
151 | * |
||
152 | * @param array $lines |
||
153 | * @param string $line |
||
154 | * @param int $number |
||
155 | * @return mixed |
||
156 | */ |
||
157 | private function search(array $lines, string $line, int $number) |
||
173 | |||
174 | /** |
||
175 | * For multi-line invocation f.e. |
||
176 | * env( |
||
177 | * 'MULTI', |
||
178 | * 'lined' |
||
179 | * ); |
||
180 | * |
||
181 | * @param array $lines |
||
182 | * @param int $number |
||
183 | * @return mixed |
||
184 | */ |
||
185 | private function searchMultiLine(array $lines, int $number) |
||
200 | |||
201 | /** |
||
202 | * Set invocation based on first index in preg_match_all result |
||
203 | * |
||
204 | * @param array $matches |
||
205 | */ |
||
206 | private function setInvocation(array $matches) |
||
212 | |||
213 | /** |
||
214 | * Sets parameters based on comma exploding |
||
215 | * 1 of last indexes in preg_match_all result |
||
216 | * |
||
217 | * @param array $matches |
||
218 | */ |
||
219 | private function setParameters(array $matches) |
||
229 | |||
230 | /** |
||
231 | * Sets location as filename + linenumber |
||
232 | * |
||
233 | * @param int $linenumber |
||
234 | */ |
||
235 | private function setLocation(int $linenumber) |
||
239 | |||
240 | /** |
||
241 | * Only warn about risky and unreadable invocations |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | private function needsWarning(): bool |
||
259 | |||
260 | /** |
||
261 | * @return bool |
||
262 | */ |
||
263 | private function alreadyProcessed(): bool |
||
267 | |||
268 | /** |
||
269 | * Store result and optional runtime output |
||
270 | */ |
||
271 | private function storeResult() |
||
299 | |||
300 | private function getFiles(): array |
||
323 | } |
||
324 |
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.