1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Copyright (c) 2017 Constantin Galbenu <[email protected]> * |
4
|
|
|
******************************************************************************/ |
5
|
|
|
|
6
|
|
|
namespace Gica\CodeAnalysis\Shared; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class FqnResolver |
10
|
|
|
{ |
11
|
4 |
|
public function resolveShortClassName($shortName, \ReflectionClass $contextClass) |
12
|
|
|
{ |
13
|
4 |
|
if (preg_match_all('#use\s+(?P<uses>[^;]+);#ims', file_get_contents($contextClass->getFileName()), $m)) { |
14
|
|
|
|
15
|
3 |
|
$firstComponent = $this->getFirstComponent($shortName); |
16
|
|
|
|
17
|
3 |
|
foreach ($m['uses'] as $fullUse) { |
18
|
|
|
|
19
|
3 |
|
list($use, $alias) = $this->parseUse($fullUse); |
20
|
|
|
|
21
|
3 |
|
if ($alias == $firstComponent) { |
22
|
3 |
|
$withoutFirstComponent = $this->getWithoutFirstComponent($shortName); |
23
|
|
|
|
24
|
3 |
|
if ($withoutFirstComponent) { |
25
|
1 |
|
return $use . '\\' . $withoutFirstComponent; |
26
|
|
|
} else { |
27
|
2 |
|
return $use; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
return $contextClass->getNamespaceName() . '\\' . $shortName; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
private function parseUse($fullUse) |
37
|
|
|
{ |
38
|
3 |
|
if (false !== stripos($fullUse, ' as ')) { |
39
|
2 |
|
list($use, $alias) = explode(' as ', $fullUse); |
40
|
|
|
|
41
|
2 |
|
$use = trim($use, " "); |
42
|
2 |
|
$alias = trim($alias, " "); |
43
|
|
|
} else { |
44
|
1 |
|
$alias = $this->getLastComponent($fullUse); |
45
|
1 |
|
$use = $fullUse; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return [$use, $alias]; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
private function getLastComponent($name) |
52
|
|
|
{ |
53
|
1 |
|
$parts = explode('\\', $name); |
54
|
|
|
|
55
|
1 |
|
return $parts[count($parts) - 1]; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
private function getWithoutFirstComponent($name) |
59
|
|
|
{ |
60
|
3 |
|
$parts = explode('\\', $name); |
61
|
|
|
|
62
|
3 |
|
return implode('\\', array_slice($parts, 1)); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
private function getFirstComponent($name) |
66
|
|
|
{ |
67
|
3 |
|
$parts = explode('\\', $name); |
68
|
|
|
|
69
|
3 |
|
return $parts[0]; |
70
|
|
|
} |
71
|
|
|
} |