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 https://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
|
21 |
|
public static function resolve(Reflector $reflection, $className) |
38
|
|
|
{ |
39
|
21 |
|
if ($reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod) |
40
|
|
|
{ |
41
|
1 |
|
$reflection = $reflection->getDeclaringClass(); |
42
|
|
|
} |
43
|
21 |
|
$docs = (new DocComment())->forClass($reflection); |
44
|
21 |
|
$use = $docs['use']; |
45
|
21 |
|
$ns = $docs['namespace']; |
46
|
21 |
|
$aliases = $docs['useAliases']; |
47
|
|
|
|
48
|
|
|
// Resolve to itself with keywords |
49
|
21 |
|
if ($className === 'self' || $className === 'static') |
50
|
|
|
{ |
51
|
4 |
|
$fqn = $ns . '\\' . $docs['className']; |
52
|
4 |
|
return $fqn; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// This is for same namespaced class as current class |
56
|
21 |
|
if (strpos($className, '\\') === false) |
57
|
|
|
{ |
58
|
10 |
|
$aliases[$ns . '\\' . $className] = $className; |
59
|
|
|
} |
60
|
|
|
|
61
|
21 |
|
if (in_array($className, $use)) |
62
|
|
|
{ |
63
|
4 |
|
return $className; |
64
|
|
|
} |
65
|
19 |
|
foreach ($use as $useClause) |
66
|
|
|
{ |
67
|
19 |
|
$patternClass = preg_quote($className); |
68
|
19 |
|
$pattern = "~\\\\$patternClass$~"; |
69
|
19 |
|
if (preg_match($pattern, $useClause)) |
70
|
|
|
{ |
71
|
19 |
|
return $useClause; |
72
|
|
|
} |
73
|
|
|
} |
74
|
16 |
|
foreach ($aliases as $useClause => $alias) |
75
|
|
|
{ |
76
|
7 |
|
if ($className == $alias) |
77
|
|
|
{ |
78
|
7 |
|
NameNormalizer::normalize($useClause, false); |
79
|
7 |
|
return $useClause; |
80
|
|
|
} |
81
|
|
|
} |
82
|
12 |
|
return $className; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|