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 {
132 (?:[<]span[ ]class="constant"[>])?
135 (?:\s*=\s*(?P<value>.+))?
136 (?P<desc>(?:\s*\n\s*[^\*\n#].*)*)
140 $consts = $this->splitList($pattern, $this->getSection("Constants"));
141 foreach ($consts as $const) {
142 if (!isset($const["value"]) ||
!strlen($const["value"])) {
143 $const["value"] = $this->getConstantValue($const["name"]);
145 $structs[$const["name"]] = new StructureOfConst($const);
150 private function getProperties() : array {
153 (?P<modifiers>\w+\s+)*
154 (?:\((?P<usages>(?:(?:\w+)\s*)*)\))*\s*
155 (?P<type>[\\\\\w]+)\s+
157 (?:\s*=\s*(?P<defval>.+))?
158 (?P<desc>(?:\s*\n\s*[^\*].*)*)
162 $props = $this->splitList($pattern, $this->getSection("Properties"));
163 foreach ($props as $prop) {
164 $structs[$prop["name"]] = new StructureOfVar($prop);
169 private function getFunctions() : array {
171 foreach ($this->entry
as $sub) {
172 if ($sub->isFunction()) {
173 $structs[$sub->getEntryName()] = static::of($sub);
179 private function getClasses() : array {
181 foreach ($this->entry
as $sub) {
182 if ($sub->isNsClass()) {
183 $structs[$sub->getEntryName()] = static::of($sub);
189 private function getParams() : array {
192 (?P<modifiers>\w+\s+)*
193 (?P<type>[\\\\\w_]+)\s+
194 (?P<ref>&)?(?P<name>\$[\w_]+)
195 (?:\s*=\s*(?P<defval>.+))?
196 (?P<desc>(?:\s*[^*]*\n(?!\n)\s*[^\*].*)*)
200 $params = $this->splitList($pattern, $this->getSection("Params"));
201 foreach ($params as $param) {
202 $structs[$param["name"]] = new StructureOfVar($param);
207 private function getReturns() : array {
212 (?P<desc>(?:.|\n(?!\s*\*))*)
215 $returns = $this->splitList($pattern, $this->getSection("Returns"));
217 foreach ($returns as list(, $type, $desc)) {
218 $retvals[] = [$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($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);
268 function saneTypes(array $types) {
270 foreach ($types as $type) {
271 if (strlen($s = $this->saneType($type, false))) {
278 function saneType($type, $strict = true) {
279 switch (strtolower($type)) {
300 return ($type[0] === "\\" ?
"":"\\") . $type;
306 class StructureOfRoot
extends StructureOf
{
313 $this->formatDesc(0);
315 foreach ($this->consts
as $const) {
320 printf("namespace %s;\nuse %s;\n", $this->name
, $this->name
);
321 StructureOfNs
::$last = $this->name
;
323 foreach ($this->getClasses() as $class) {
328 function getClasses() {
329 yield from
$this->classes
;
330 foreach ($this->classes
as $class) {
331 yield from
$class->getClasses();
335 class StructureOfNs
extends StructureOfRoot
{
341 print $this->formatDesc(0);
343 if (strlen($this->name
) && $this->name
!== StructureOfNs
::$last) {
344 StructureOfNs
::$last = $this->name
;
345 printf("namespace %s;\n", $this->name
);
347 foreach ($this->consts
as $const) {
354 class StructureOfClass
extends StructureOfNs
360 if ($this->ns
!== StructureOfNs
::$last) {
361 printf("namespace %s;\n", $this->ns
);
362 StructureOfNs
::$last = $this->ns
;
365 print $this->formatDesc(0);
366 printf("%s {\n", $this->name
);
368 foreach ($this->consts
as $const) {
373 foreach ($this->props
as $prop) {
374 $prop->formatAsProp(1);
378 foreach ($this->funcs
as $func) {
380 if (strncmp($this->name
, "interface", strlen("interface"))) {
391 class StructureOfFunc
extends StructureOf
{
400 function omitParamTypes() {
401 switch ($this->name
) {
415 function format(int $level) {
417 foreach ($this->params
as $param) {
418 $type = $this->saneType($param->type
, false);
419 $tags[] = "param {$type} {$param->name} {$param->desc}";
421 foreach ($this->throws
as $throws) {
422 $tags[] = "throws " . $this->saneType($throws);
424 if ($this->name
!== "__construct" && $this->returns
) {
426 if (count($this->returns
) > 1) {
427 $type = implode("|", $this->saneTypes(array_column($this->returns
, 0)));
429 foreach ($this->returns
as list($typ, $ret)) {
431 $desc .= "\n\t\t or ";
433 $desc .= $this->saneType($typ, false) . " " . $ret;
436 $type = $this->saneType($this->returns
[0][0], false);
437 $desc = $this->returns
[0][1];
439 $tags[] = "return $type $desc";
441 $this->formatDesc(1, $tags);
442 printf("\tfunction %s(", $this->name
);
444 $omit = $this->omitParamTypes();
445 foreach ($this->params
as $param) {
447 $param->formatAsParam($level, !$omit);
454 class StructureOfConst
extends StructureOf
{
459 function format(int $level) {
460 $indent = str_repeat("\t", $level);
461 $this->formatDesc($level);
462 printf("%sconst %s = ", $indent, $this->name
);
463 var_export($this->value
);
467 class StructureOfVar
extends StructureOf
{
476 function formatDefval() {
477 if (strlen($this->defval
)) {
478 if (false && defined($this->defval
)) {
480 var_export(constant($this->defval
));
481 } else if (strlen($this->defval
)) {
482 if (false !== strchr($this->defval
, "\\") && $this->defval
[0] != "\\") {
483 $this->defval
= "\\" . $this->defval
;
485 printf(" = %s", $this->defval
);
487 } elseif ($this->modifiers
) {
488 if (stristr($this->modifiers
, "optional") !== false) {
493 function formatAsProp($level) {
494 $indent = str_repeat("\t", $level);
495 $this->formatDesc($level,
496 preg_split('/\s+/', $this->modifiers
." " . $this->usages
, -1, PREG_SPLIT_NO_EMPTY
)
497 +
[-1 => "var " . $this->saneType($this->type
, false)]
499 printf("%s%s %s", $indent, $this->modifiers
, $this->name
);
500 $this->formatDefval();
503 function formatAsParam($level, $with_type = true) {
504 if ($with_type && strlen($type = $this->saneType($this->type
))) {
505 printf("%s ", $type);
507 printf("%s%s", $this->ref
, $this->name
);
508 $this->formatDefval();