1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wingu\OctopusCore\Reflection; |
4
|
|
|
|
5
|
|
|
use Wingu\OctopusCore\Reflection\Annotation\AnnotationsCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Reflection on a document block. |
9
|
|
|
*/ |
10
|
|
|
class ReflectionDocComment |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The original document block that was parsed. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $originalDocBlock = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The parsed short description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $shortDescription; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The parsed long description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $longDescription; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $comment The original document block comment. |
38
|
|
|
* @param string $trimLinePattern Pattern for trim() function applied to each line. Usefull to leave spaces or tabs. The default is the same as calling trim() without the argument. |
39
|
|
|
*/ |
40
|
186 |
|
public function __construct($comment, $trimLinePattern = " \t\n\r\0\x0B") |
41
|
|
|
{ |
42
|
186 |
|
$this->originalDocBlock = trim((string)$comment); |
43
|
|
|
|
44
|
186 |
|
$comment = preg_replace('#^\s*\*\s?#ms', '', trim($this->originalDocBlock, '/*')); |
45
|
186 |
|
$comment = preg_split('#^\s*(?=@[_a-zA-Z\x7F-\xFF][_a-zA-Z0-9\x7F-\xFF-]*)#m', $comment, 2); |
46
|
|
|
|
47
|
186 |
|
if (isset($comment[0]) === true) { |
48
|
186 |
|
$description = $comment[0]; |
49
|
186 |
|
$description = preg_split("/\n|\n\r/", $description); |
50
|
186 |
|
array_walk($description, function (& $value, $key, $trimLinePattern) { |
51
|
186 |
|
$value = trim($value, $trimLinePattern); |
52
|
186 |
|
}, $trimLinePattern); |
53
|
|
|
|
54
|
186 |
|
foreach ($description as $key => $descLine) { |
55
|
186 |
|
if ($descLine !== '') { |
56
|
111 |
|
$this->shortDescription = $descLine; |
57
|
111 |
|
unset($description[$key]); |
58
|
111 |
|
$this->longDescription = trim(implode("\n", $description)); |
59
|
111 |
|
$this->longDescription = $this->longDescription !== '' ? $this->longDescription : null; |
60
|
111 |
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
75 |
|
unset($description[$key]); |
64
|
62 |
|
} |
65
|
62 |
|
} |
66
|
186 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the full description. |
70
|
|
|
* The long description (if present) will be concatenated to the short description with an empty line between. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
33 |
|
public function getFullDescription() |
75
|
|
|
{ |
76
|
33 |
|
if ($this->longDescription !== null) { |
77
|
15 |
|
return $this->shortDescription . "\n\n" . $this->longDescription; |
78
|
|
|
} else { |
79
|
18 |
|
return $this->shortDescription; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the short description. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
27 |
|
public function getShortDescription() |
89
|
|
|
{ |
90
|
27 |
|
return $this->shortDescription; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the long description. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
33 |
|
public function getLongDescription() |
99
|
|
|
{ |
100
|
33 |
|
return $this->longDescription; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the annotations collection. |
105
|
|
|
* |
106
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\AnnotationsCollection |
107
|
|
|
*/ |
108
|
24 |
|
public function getAnnotationsCollection() |
109
|
|
|
{ |
110
|
24 |
|
return new AnnotationsCollection($this->originalDocBlock); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the original doc block. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
24 |
|
public function getOriginalDocBlock() |
119
|
|
|
{ |
120
|
24 |
|
return $this->originalDocBlock; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check if the documentation comment is empty or not. |
125
|
|
|
* |
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
15 |
|
public function isEmpty() |
129
|
|
|
{ |
130
|
15 |
|
return $this->originalDocBlock === ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Magic method to print out the document. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
24 |
|
public function __toString() |
139
|
|
|
{ |
140
|
24 |
|
return $this->originalDocBlock; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|