1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Julius Härtl <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Julius Härtl <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace JuliusHaertl\PHPDocToRst\Builder; |
25
|
|
|
|
26
|
|
|
use JuliusHaertl\PHPDocToRst\Extension\Extension; |
27
|
|
|
use phpDocumentor\Reflection\DocBlock; |
28
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Return_; |
29
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\See; |
30
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Since; |
31
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Throws; |
32
|
|
|
use phpDocumentor\Reflection\Element; |
33
|
|
|
use phpDocumentor\Reflection\Php\Constant; |
34
|
|
|
use phpDocumentor\Reflection\Php\Property; |
35
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class to build reStructuredText file with sphinxcontrib-phpdomain syntax |
39
|
|
|
* |
40
|
|
|
* @package JuliusHaertl\PHPDocToRst\Builder |
41
|
|
|
*/ |
42
|
|
|
class PhpDomainBuilder extends RstBuilder { |
43
|
|
|
|
44
|
|
|
private $extensions; |
45
|
|
|
|
46
|
|
|
public function __construct($extensions) { |
47
|
|
|
$this->extensions = $extensions; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Constant $constant |
52
|
|
|
*/ |
53
|
|
|
protected function addConstant(Constant $constant) { |
54
|
|
|
$this->beginPhpDomain('const', $constant->getName() . ' = ' . $constant->getValue()); |
55
|
|
|
$docBlock = $constant->getDocBlock(); |
56
|
|
|
if ($docBlock) { |
57
|
|
|
foreach ($docBlock->getTags() as $tag) { |
58
|
|
|
$this->addDocblockTag( $tag->getName(), $docBlock); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$this->endPhpDomain(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Property[] $properties |
66
|
|
|
*/ |
67
|
|
|
protected function addProperties($properties) { |
68
|
|
|
if (count($properties) > 0) { |
69
|
|
|
$this->addH2('Properties'); |
70
|
|
|
foreach ($properties as $property) { |
71
|
|
|
if ($this->shouldRenderElement($property)) { |
72
|
|
|
$this->addProperty($property); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Property $property |
80
|
|
|
*/ |
81
|
|
|
private function addProperty(Property $property) { |
82
|
|
|
$this->beginPhpDomain('attr', $property->getName()); |
83
|
|
|
$this->endPhpDomain(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $type string |
88
|
|
|
* @param $fqsen string |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getLink($type, $fqsen) { |
92
|
|
|
return ':php:' . $type . ':`' . RstBuilder::escape(substr($fqsen, 1)) . '`'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $type string |
97
|
|
|
* @param $name string |
98
|
|
|
* @param $indent bool Should indent after the section started |
99
|
|
|
*/ |
100
|
|
|
public function beginPhpDomain($type, $name, $indent=true) { |
101
|
|
|
// FIXME: Add checks if it is properly ended |
102
|
|
|
$this->addLine('.. php:' . $type . ':: '. $name)->addLine(); |
103
|
|
|
if ($indent === true) { |
104
|
|
|
$this->indent(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $type |
110
|
|
|
*/ |
111
|
|
|
public function endPhpDomain($type='') { |
|
|
|
|
112
|
|
|
$this->unindent(); |
113
|
|
|
$this->addLine(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param DocBlock $docBlock |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function addDocBlockDescription($docBlock) { |
121
|
|
|
if ($docBlock === null) { |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
$this->addMultiline($docBlock->getSummary())->addLine(); |
125
|
|
|
$this->addMultiline($docBlock->getDescription())->addLine(); |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $tagName Name of the tag to parse |
131
|
|
|
* @param DocBlock $docBlock |
132
|
|
|
*/ |
133
|
|
|
protected function addDocblockTag($tagName, DocBlock $docBlock) { |
134
|
|
|
$tags = $docBlock->getTagsByName($tagName); |
135
|
|
|
switch ($tagName) { |
136
|
|
View Code Duplication |
case 'return': |
|
|
|
|
137
|
|
|
if (count($tags) === 0) continue; |
138
|
|
|
/** @var Return_ $return */ |
139
|
|
|
$return = $tags[0]; |
140
|
|
|
$this->addMultiline(':returns: ' . $return->getType() . ' ' . RstBuilder::escape($return->getDescription()), true); |
141
|
|
|
break; |
142
|
|
View Code Duplication |
case 'throws': |
|
|
|
|
143
|
|
|
if (count($tags) === 0) continue; |
144
|
|
|
/** @var Throws $tag */ |
145
|
|
|
foreach ($tags as $tag) { |
146
|
|
|
$this->addMultiline(':throws: ' . $tag->getType() . ' ' . RstBuilder::escape($tag->getDescription()), true); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
break; |
149
|
|
View Code Duplication |
case 'since': |
|
|
|
|
150
|
|
|
if (count($tags) === 0) continue; |
151
|
|
|
/** @var Since $return */ |
152
|
|
|
$return = $tags[0]; |
153
|
|
|
$this->addMultiline(':since: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()), true); |
154
|
|
|
break; |
155
|
|
View Code Duplication |
case 'deprecated': |
|
|
|
|
156
|
|
|
if (count($tags) === 0) continue; |
157
|
|
|
/** @var Deprecated $return */ |
158
|
|
|
$return = $tags[0]; |
159
|
|
|
$this->addMultiline(':deprecated: ' . $return->getVersion() . ' ' . RstBuilder::escape($return->getDescription()), true); |
160
|
|
|
break; |
161
|
|
View Code Duplication |
case 'see': |
|
|
|
|
162
|
|
|
if (count($tags) === 0) continue; |
163
|
|
|
/** @var See $return */ |
164
|
|
|
$return = $tags[0]; |
165
|
|
|
$this->addMultiline(':see: ' . $return->getReference() . ' ' . RstBuilder::escape($return->getDescription()), true); |
166
|
|
|
break; |
167
|
|
View Code Duplication |
case 'license': |
|
|
|
|
168
|
|
|
if (count($tags) === 0) continue; |
169
|
|
|
/** @var DocBlock\Tags\BaseTag $return */ |
170
|
|
|
$return = $tags[0]; |
171
|
|
|
$this->addMultiline(':license: ' . RstBuilder::escape($return->getDescription()), true); |
172
|
|
|
break; |
173
|
|
|
case 'param': |
174
|
|
|
// param handling is done by subclasses since it is more that docbook parsing |
175
|
|
|
break; |
176
|
|
|
default: |
177
|
|
|
//echo 'Tag handling not defined for: ' . $tag . PHP_EOL; |
178
|
|
|
break; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function shouldRenderElement(Element $element) { |
184
|
|
|
/** @var Extension $extension */ |
185
|
|
|
foreach ($this->extensions as $extension) { |
186
|
|
|
if ($extension->shouldRenderElement($element) === false) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.