|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Analyzers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
class Expander |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param string $type |
|
11
|
|
|
* |
|
12
|
|
|
* @return bool |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function isKeywordType($type) |
|
15
|
|
|
{ |
|
16
|
|
|
return \in_array(strtolower($type), [ |
|
17
|
|
|
'self', |
|
18
|
|
|
'static', |
|
19
|
|
|
'parent', |
|
20
|
|
|
], true); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function expendReferences($classes, $imports, $namespace) |
|
24
|
|
|
{ |
|
25
|
|
|
// Here we implode the tokens to form the full namespaced class path |
|
26
|
|
|
$results = []; |
|
27
|
|
|
$c = 0; |
|
28
|
|
|
foreach ($classes as $importeds) { |
|
29
|
|
|
$results[$c]['class'] = ''; |
|
30
|
|
|
|
|
31
|
|
|
// attach the current namespace if it does not begin with '\' |
|
32
|
|
|
if ($importeds[0][1][0] != '\\') { |
|
33
|
|
|
$results[$c]['class'] = $namespace ? $namespace.'\\' : ''; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
foreach ($importeds as $row) { |
|
37
|
|
|
if (self::isKeywordType($row[1])) { |
|
38
|
|
|
unset($results[$c]); |
|
39
|
|
|
$c--; |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// if starts with "\" or is not imported by the "use" |
|
44
|
|
|
if ($importeds[0][1][0] != '\\' && Str::contains($importeds[0][1], '\\')) { |
|
45
|
|
|
// for php 8.x |
|
46
|
|
|
$tmp = explode('\\', $importeds[0][1]); |
|
47
|
|
|
} else { |
|
48
|
|
|
$tmp = [$importeds[0][1]]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($importeds[0][1] == '\\' || ! isset(array_values($imports)[0][$tmp[0]][0])) { |
|
52
|
|
|
$results[$c]['class'] .= $row[1]; |
|
53
|
|
|
$results[$c]['line'] = $row[2]; |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// reads the import from the top |
|
58
|
|
|
$results[$c]['class'] = array_values($imports)[0][$tmp[0]][0]; |
|
59
|
|
|
|
|
60
|
|
|
// for half imported references |
|
61
|
|
|
if ($importeds[0][1][0] != '\\' && Str::contains($importeds[0][1], '\\')) { |
|
62
|
|
|
// for php 8.x |
|
63
|
|
|
$tmp = explode('\\', $importeds[0][1]); |
|
64
|
|
View Code Duplication |
for ($j = 1; $j < count($tmp); $j++) { |
|
|
|
|
|
|
65
|
|
|
$results[$c]['class'] .= '\\'.$tmp[$j]; |
|
66
|
|
|
} |
|
67
|
|
|
} else { |
|
68
|
|
|
// for php 7.x |
|
69
|
|
View Code Duplication |
for ($j = 1; $j < count($importeds); $j++) { |
|
|
|
|
|
|
70
|
|
|
$results[$c]['class'] .= $importeds[$j][1]; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$results[$c]['line'] = $row[2]; |
|
75
|
|
|
} |
|
76
|
|
|
$c++; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return [$results, $namespace]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: