|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Annotations\Parser\Reference; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Parser\Ast\Reference; |
|
8
|
|
|
use Doctrine\Annotations\Parser\Scope; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
use ReflectionFunction; |
|
11
|
|
|
use ReflectionFunctionAbstract; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use ReflectionProperty; |
|
14
|
|
|
use Reflector; |
|
15
|
|
|
use function assert; |
|
16
|
|
|
use function explode; |
|
17
|
|
|
use function strpos; |
|
18
|
|
|
use function strtolower; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
final class FallbackReferenceResolver implements ReferenceResolver |
|
24
|
|
|
{ |
|
25
|
19 |
|
public function resolve(Reference $reference, Scope $scope) : string |
|
26
|
|
|
{ |
|
27
|
19 |
|
$identifier = $reference->getIdentifier(); |
|
28
|
19 |
|
$imports = $scope->getImports(); |
|
29
|
|
|
|
|
30
|
19 |
|
if ($reference->isFullyQualified()) { |
|
31
|
7 |
|
return $identifier; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
12 |
|
$identifierLower = strtolower($identifier); |
|
35
|
|
|
|
|
36
|
12 |
|
if (isset($imports[$identifierLower])) { |
|
37
|
2 |
|
return $imports[$identifierLower]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
10 |
|
if (strpos($identifierLower, '\\') !== false) { |
|
41
|
|
|
$namespacePart = explode('\\', $identifierLower, 2)[0]; |
|
42
|
|
|
|
|
43
|
|
|
if (isset($imports[$namespacePart])) { |
|
44
|
|
|
return $imports[$namespacePart] . '\\' . explode('\\', $identifier, 2)[1]; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
10 |
|
$subject = $scope->getSubject(); |
|
49
|
|
|
|
|
50
|
10 |
|
if (! $subject instanceof ReflectionClass && ! $subject instanceof ReflectionFunctionAbstract) { |
|
51
|
1 |
|
return $identifier; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
9 |
|
$namespace = $this->getSubjectNamespaceName($scope->getSubject()); |
|
55
|
|
|
|
|
56
|
9 |
|
if ($namespace === '') { |
|
57
|
4 |
|
return $identifier; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
5 |
|
return $namespace . '\\' . $identifier; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
9 |
|
private function getSubjectNamespaceName(Reflector $subject) : string |
|
64
|
|
|
{ |
|
65
|
9 |
|
if ($subject instanceof ReflectionClass || $subject instanceof ReflectionFunction) { |
|
66
|
9 |
|
return $subject->getNamespaceName(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($subject instanceof ReflectionProperty || $subject instanceof ReflectionMethod) { |
|
70
|
|
|
return $subject->getDeclaringClass()->getNamespaceName(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
assert(false, 'Unsupported Reflector'); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: