6 * Structure of an entry
9 const OF_OTHER
= "other";
10 const OF_NAMESPACE
= "ns";
11 const OF_CLASS
= "class";
12 const OF_FUNC
= "func";
18 function __construct(Entry
$entry) {
19 $this->entry
= $entry;
21 if ($entry->isRoot() ||
$entry->isNsClass()) {
22 if ($entry->isRoot()) {
23 $this->type
= self
::OF_NAMESPACE
;
24 $this->getStructureOfRoot();
25 } elseif (!strncmp($entry->getTitle(), "namespace", strlen("namespace"))) {
26 $this->type
= self
::OF_NAMESPACE
;
27 $this->getStructureOfNs();
29 $this->type
= self
::OF_CLASS
;
30 $this->getStructureOfClass();
32 } elseif ($entry->isFunction()) {
33 $this->type
= self
::OF_FUNC
;
34 $this->getStructureOfFunc();
36 $this->type
= self
::OF_OTHER
;
40 static function of(Entry
$entry) : StructureOf
{
41 return (new static($entry))->getStruct();
44 function getStruct() : StructureOf
{
49 $this->struct
->format();
52 private function getStructureOfFunc() : StructureOfFunc
{
53 return $this->struct
= new StructureOfFunc([
54 "ns" => $this->entry
->getParent()->getNsName(),
55 "name" => $this->entry
->getEntryName(),
56 "desc" => $this->entry
->getFullDescription(),
57 "returns" => $this->getReturns(),
58 "params" => $this->getParams(),
59 "throws" => $this->getThrows()
63 private function getStructureOfClass() : StructureOfClass
{
64 return $this->struct
= new StructureOfClass([
65 "ns" => $this->entry
->getParent()->getNsName(),
66 "name" => $this->prepareClassName(),
67 "desc" => $this->entry
->getFullDescription(),
68 "consts" => $this->getConstants(),
69 "props" => $this->getProperties(),
70 "funcs" => $this->getFunctions(),
71 "classes" => $this->getClasses(),
75 private function getStructureOfNs() : StructureOfNs
{
76 return $this->struct
= new StructureOfNs([
77 "name" => $this->entry
->getNsName(),
78 "desc" => $this->entry
->getFullDescription(),
79 "consts" => $this->getConstants(),
80 "classes" => $this->getClasses(),
84 private function getStructureOfRoot() : StructureOfRoot
{
85 return $this->struct
= new StructureOfRoot([
86 "name" => $this->entry
->getName(),
87 "desc" => $this->entry
->getFile()->readIntro(),
88 "consts" => $this->getConstants(),
89 "classes" => $this->getClasses()
93 private function getSection(string $section) : string {
94 return $this->entry
->getFile()->readSection($section);
97 private function prepareClassName() {
98 return preg_replace_callback_array([
99 '/(?P<type>class|interface|trait)\s+([\\\\\w]+\\\)?(?P<name>\w+)\s*/' => function($match) {
100 return $match["type"] . " " . $match["name"] . " ";
102 '/(?P<op>extends|implements)\s+(?P<names>[\\w]+(?:(?:,\s*[\\\\\w]+)*))/' => function ($match) {
103 return $match["op"] . " " . preg_replace('/\b(?<!\\\)(\w)/', '\\\\\\1', $match["names"]);
105 ], $this->entry
->getTitle());
108 private function splitList(string $pattern, string $text) : array {
110 if (strlen($text) && !preg_match("/^None/", $text)) {
111 if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER
)) {
118 private function getConstantValue(string $name) {
119 $ns = $this->entry
->getNsName();
120 if (defined("\\$ns::$name")) {
121 return constant("\\$ns::$name");
123 if (defined("\\$ns\\$name")) {
124 return constant("\\$ns\\$name");
129 private function getConstants() : array {
133 (?:\s*=\s*(?P<value>.+))?
134 (?P<desc>(?:\s*\n\s*[^\*\n#].*)*)
138 $consts = $this->splitList($pattern, $this->getSection("Constants"));
139 foreach ($consts as $const) {
140 if (!isset($const["value"]) ||
!strlen($const["value"])) {
141 $const["value"] = $this->getConstantValue($const["name"]);
143 $structs[$const["name"]] = new StructureOfConst($const);
148 private function getProperties() : array {
151 (?P<modifiers>\w+\s+)*
152 (?P<type>[\\\\\w]+)\s+
154 (?:\s*=\s*(?P<defval>.+))?
155 (?P<desc>(?:\s*\n\s*[^\*].*)*)
159 $props = $this->splitList($pattern, $this->getSection("Properties"));
160 foreach ($props as $prop) {
161 $structs[$prop["name"]] = new StructureOfVar($prop);
166 private function getFunctions() : array {
168 foreach ($this->entry
as $sub) {
169 if ($sub->isFunction()) {
170 $structs[$sub->getEntryName()] = static::of($sub);
176 private function getClasses() : array {
178 foreach ($this->entry
as $sub) {
179 if ($sub->isNsClass()) {
180 $structs[$sub->getEntryName()] = static::of($sub);
186 private function getParams() : array {
189 (?P<modifiers>\w+\s+)*
190 (?P<type>[\\\\\w]+)\s+
192 (?:\s*=\s*(?P<defval>.+))?
193 (?P<desc>(?:\s*[^*]*\n(?!\n)\s*[^\*].*)*)
197 $params = $this->splitList($pattern, $this->getSection("Params"));
198 foreach ($params as $param) {
199 $structs[$param["name"]] = new StructureOfVar($param);
204 private function getReturns() : array {
209 (?P<desc>(?:.|\n(?!\s*\*))*)
212 $returns = $this->splitList($pattern, $this->getSection("Returns"));
214 foreach ($returns as list(, $type, $desc)) {
215 if (strlen($retdesc)) {
216 $retdesc .= "\n\t or $type $desc";
221 return [implode("|", array_unique(array_column($returns, "type"))), $retdesc];
224 private function getThrows() : array {
227 (?P<exception>[\\\\\w]+)\s*
230 $throws = $this->splitList($pattern, $this->getSection("Throws"));
231 return array_column($throws, "exception");
235 abstract class StructureOf
{
236 function __construct(array $props = []) {
237 foreach ($props as $key => $val) {
241 if (!property_exists(static::class, $key)) {
242 throw new \
UnexpectedValueException(
243 sprintf("Property %s::\$%s does not exist", static::class, $key)
246 if ($key === "desc" ||
$key === "modifiers" ||
$key === "defval") {
253 // abstract function format();
255 function formatDesc(int $level, array $tags = []) {
256 $indent = str_repeat("\t", $level);
257 $desc = trim($this->desc
);
258 if (false !== stristr($desc, "deprecated in")) {
259 $tags[] = "@deprecated";
262 $desc .= "\n\n@" . implode("\n@", $tags);
264 $desc = preg_replace('/[\t ]*\n/',"\n$indent * ", $desc);
265 printf("%s/**\n%s * %s\n%s */\n", $indent, $indent, $desc, $indent);
269 class StructureOfRoot
extends StructureOf
{
276 $this->formatDesc(0);
278 foreach ($this->consts
as $const) {
283 printf("namespace %s;\nuse %s;\n", $this->name
, $this->name
);
284 StructureOfNs
::$last = $this->name
;
286 foreach ($this->getClasses() as $class) {
291 function getClasses() {
292 yield from
$this->classes
;
293 foreach ($this->classes
as $class) {
294 yield from
$class->getClasses();
298 class StructureOfNs
extends StructureOfRoot
{
304 print $this->formatDesc(0);
306 if (strlen($this->name
) && $this->name
!== StructureOfNs
::$last) {
307 StructureOfNs
::$last = $this->name
;
308 printf("namespace %s;\n", $this->name
);
310 foreach ($this->consts
as $const) {
317 class StructureOfClass
extends StructureOfNs
325 if ($this->ns
!== StructureOfNs
::$last) {
326 printf("namespace %s;\n", $this->ns
);
327 StructureOfNs
::$last = $this->ns
;
330 print $this->formatDesc(0);
331 printf("%s {\n", $this->name
);
333 foreach ($this->consts
as $const) {
338 foreach ($this->props
as $prop) {
339 $prop->formatAsProp(1);
343 foreach ($this->funcs
as $func) {
345 if (strncmp($this->name
, "interface", strlen("interface"))) {
356 class StructureOfFunc
extends StructureOf
{
365 function format(int $level) {
367 foreach ($this->params
as $param) {
368 $tags[] = "param {$param->type} {$param->name} {$param->desc}";
370 foreach ($this->throws
as $throws) {
371 $tags[] = "throws $throws";
373 if ($this->name
!== "__construct" && $this->returns
[0]) {
374 $tags[] = "return {$this->returns[0]} {$this->returns[1]}";
376 $this->formatDesc(1, $tags);
377 printf("\tfunction %s(", $this->name
);
379 foreach ($this->params
as $param) {
381 $param->formatAsParam($level);
388 class StructureOfConst
extends StructureOf
{
393 function format(int $level) {
394 $indent = str_repeat("\t", $level);
395 $this->formatDesc($level);
396 printf("%sconst %s = ", $indent, $this->name
);
397 var_export($this->value
);
401 class StructureOfVar
extends StructureOf
{
408 function formatDefval() {
409 if (strlen($this->defval
)) {
410 if (false && defined($this->defval
)) {
412 var_export(constant($this->defval
));
413 } else if (strlen($this->defval
)) {
414 printf(" = %s", $this->defval
);
416 } elseif ($this->modifiers
) {
417 if (stristr($this->modifiers
, "optional") !== false) {
422 function formatAsProp(int $level) {
423 $indent = str_repeat("\t", $level);
424 $this->formatDesc($level,
425 preg_split('/\s+/', $this->modifiers
, -1, PREG_SPLIT_NO_EMPTY
)
426 +
[-1 => "var {$this->type}"]
428 printf("%s%s %s", $indent, $this->modifiers
, $this->name
);
429 $this->formatDefval();
432 function formatAsParam(int $level) {
433 printf("%s", $this->name
);
434 $this->formatDefval();