|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/addendum |
|
7
|
|
|
* @licence AGPL, Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
|
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
|
11
|
|
|
* @link http://maslosoft.com/addendum/ - maslosoft addendum |
|
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum\Utilities; |
|
16
|
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Builder\DocComment; |
|
18
|
|
|
use Maslosoft\Addendum\Utilities\NameNormalizer; |
|
19
|
|
|
use ReflectionMethod; |
|
20
|
|
|
use ReflectionProperty; |
|
21
|
|
|
use Reflector; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* UseResolver |
|
25
|
|
|
* |
|
26
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
27
|
|
|
*/ |
|
28
|
|
|
class UseResolver |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Resolve class alias |
|
33
|
|
|
* @param Reflector $reflection |
|
34
|
|
|
* @param string $className |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
13 |
|
public static function resolve(Reflector $reflection, $className) |
|
38
|
|
|
{ |
|
39
|
13 |
|
if ($reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod) |
|
40
|
13 |
|
{ |
|
41
|
1 |
|
$reflection = $reflection->getDeclaringClass(); |
|
42
|
1 |
|
} |
|
43
|
13 |
|
$docs = (new DocComment())->forClass($reflection); |
|
44
|
13 |
|
$use = $docs['use']; |
|
45
|
13 |
|
$ns = $docs['namespace']; |
|
46
|
13 |
|
$aliases = $docs['useAliases']; |
|
47
|
|
|
|
|
48
|
|
|
// Resolve to itself with keywords |
|
49
|
13 |
|
if ($className === 'self' || $className === 'static') |
|
50
|
13 |
|
{ |
|
51
|
1 |
|
$fqn = $ns . '\\' . $docs['className']; |
|
52
|
1 |
|
return $fqn; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// This is for same namespaced class as current class |
|
56
|
13 |
|
$aliases[$ns . '\\' . $className] = $className; |
|
57
|
|
|
|
|
58
|
13 |
|
if (in_array($className, $use)) |
|
59
|
13 |
|
{ |
|
60
|
4 |
|
return $className; |
|
61
|
|
|
} |
|
62
|
11 |
|
foreach ($use as $useClause) |
|
63
|
|
|
{ |
|
64
|
11 |
|
$patternClass = preg_quote($className); |
|
65
|
11 |
|
$pattern = "~\\\\$patternClass$~"; |
|
66
|
11 |
|
if (preg_match($pattern, $useClause)) |
|
67
|
11 |
|
{ |
|
68
|
5 |
|
return $useClause; |
|
69
|
|
|
} |
|
70
|
11 |
|
} |
|
71
|
7 |
|
foreach ($aliases as $useClause => $alias) |
|
72
|
|
|
{ |
|
73
|
7 |
|
if ($className == $alias) |
|
74
|
7 |
|
{ |
|
75
|
7 |
|
NameNormalizer::normalize($useClause, false); |
|
76
|
7 |
|
return $useClause; |
|
77
|
|
|
} |
|
78
|
1 |
|
} |
|
79
|
|
|
return $className; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|