1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* phpDocumentor |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com) |
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
9
|
|
|
* @link http://phpdoc.org |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace phpDocumentor\Descriptor\Builder\Reflector\Tags; |
13
|
|
|
|
14
|
|
|
use phpDocumentor\Compiler\Linker\Linker; |
15
|
|
|
use phpDocumentor\Descriptor\Builder\Reflector\AssemblerAbstract; |
16
|
|
|
use phpDocumentor\Descriptor\Tag\UsesDescriptor; |
17
|
|
|
use phpDocumentor\Reflection\DocBlock\Context; |
18
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag\UsesTag; |
19
|
|
|
use phpDocumentor\Reflection\DocBlock\Type\Collection; |
20
|
|
|
|
21
|
|
|
class UsesAssembler extends AssemblerAbstract |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Creates a new Descriptor from the given Reflector. |
25
|
|
|
* |
26
|
|
|
* @param UsesTag $data |
27
|
|
|
* |
28
|
|
|
* @return UsesDescriptor |
29
|
|
|
*/ |
30
|
|
|
public function create($data) |
31
|
|
|
{ |
32
|
|
|
$descriptor = new UsesDescriptor($data->getName()); |
33
|
|
|
$descriptor->setDescription($data->getDescription()); |
34
|
|
|
$reference = $data->getReference(); |
35
|
|
|
$context = $data->getDocBlock() ? $data->getDocBlock()->getContext() : null; |
36
|
|
|
|
37
|
|
|
if ($reference !== 'self' |
38
|
|
|
&& $reference !== '$this' |
39
|
|
|
&& $reference[0] !== '\\' |
40
|
|
|
) { |
41
|
|
|
// Expand FQCN part of the FQSEN |
42
|
|
|
$referenceParts = explode('::', $reference); |
43
|
|
|
|
44
|
|
|
if (count($referenceParts) > 1 || $this->referenceIsNamespaceAlias($reference, $context)) { |
|
|
|
|
45
|
|
|
$referenceParts = $this->setFirstReferencePartAsType($context, $referenceParts); |
|
|
|
|
46
|
|
|
} elseif (isset($reference[0])) { |
47
|
|
|
array_unshift($referenceParts, Linker::CONTEXT_MARKER); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$reference = implode('::', $referenceParts); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$descriptor->setReference($reference); |
54
|
|
|
|
55
|
|
|
return $descriptor; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Context $context |
60
|
|
|
* @param string[] $referenceParts |
61
|
|
|
* @return array The returned array will consist of a Collection object with the type, and strings for methods, etc. |
62
|
|
|
*/ |
63
|
|
|
private function setFirstReferencePartAsType($context, $referenceParts) |
64
|
|
|
{ |
65
|
|
|
$type = current($referenceParts); |
66
|
|
|
$type = new Collection( |
67
|
|
|
array($type), |
|
|
|
|
68
|
|
|
$context |
69
|
|
|
); |
70
|
|
|
$referenceParts[0] = $type; |
71
|
|
|
return $referenceParts; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* When you have a relative reference to a class, we need to check if this class exists in the namespace aliases |
76
|
|
|
* |
77
|
|
|
* @param string $reference |
78
|
|
|
* @param Context $context |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
private function referenceIsNamespaceAlias($reference, $context) |
82
|
|
|
{ |
83
|
|
|
/** @var \phpDocumentor\Reflection\DocBlock\Context $context*/ |
84
|
|
|
foreach ($context->getNamespaceAliases() as $alias) { |
85
|
|
|
if (substr($alias, -strlen($reference)) === $reference) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: