move formatting from JS to PHP
[mdref/mdref] / mdref / Generator / Func.php
1 <?php
2
3 namespace mdref\Generator;
4
5 use mdref\Generator;
6 use phpDocumentor\Reflection\{DocBlock, DocBlock\Tags, DocBlockFactory};
7
8 class Func extends Scrap {
9 public function __toString() : string {
10 return parent::toString(__FILE__, __COMPILER_HALT_OFFSET__);
11 }
12 }
13
14 /** @var $gen Generator */
15 /** @var $ref \ReflectionFunctionAbstract */
16 /** @var $doc DocBlock */
17 /** @var $patch callable as function(string, \Reflector) */
18
19 __HALT_COMPILER();
20 # <?php
21 if ($ref instanceof \ReflectionMethod) :
22 if ($ref->isFinal()) :
23 ?>final <?php
24 endif;
25 if ($ref->isStatic()) :
26 ?>static <?php
27 endif;
28 endif;
29 ?><?= $ref->hasReturnType() ? $ref->getReturnType() : "void"
30 ?> <?php
31 if ($ref instanceof \ReflectionMethod) :
32 ?><?=$ref->getDeclaringClass()->getName()
33 ?>::<?php
34 endif;
35 ?><?= $ref->getName() ?>
36 (<?php
37 $optional = 0;
38 foreach ($ref->getParameters() as $i => $param) :
39 if ($param->isOptional()) : $optional++
40 ?>[<?php
41 endif;
42 $patch(Arg::class, $param);
43 if ($i < $ref->getNumberOfParameters()-1):
44 ?>, <?php
45 endif;
46 endforeach;
47 echo str_repeat("]", $optional);
48 ?>
49 )
50
51 <?= $doc?->getSummary() ?>
52
53
54 <?= $doc?->getDescription()?->getBodyTemplate() ?>
55
56
57 <?php $patch(SeeAlso::class, $ref) ?>
58
59
60
61 ## Params:
62
63 <?php
64 if (!($params = $ref->getParameters())) :
65 ?>None.<?php
66 else :
67 foreach ($params as $i => $param) :
68 $patch(Param::class, $param);
69 endforeach;
70 endif;
71
72 if (($tags = $doc?->getTagsWithTypeByName("return")) || ($ref->hasReturnType() && $ref->hasReturnType() != "void")) :
73 ?>
74
75
76 ## Returns:
77
78 <?php
79 if ($tags) :
80 foreach ($tags as $tag) :
81 ?>* <?= $tag->getType()
82 ?>, <?= $tag->getDescription()
83 ?><?="\n"
84 ?><?php
85 endforeach;
86 else :
87 ?>* <?= $ref->getReturnType()
88 ?><?php
89 endif;
90 endif;
91 ?>
92
93
94 <?php
95 if (($tags = $doc?->getTagsWithTypeByName("throws"))) :
96 ?>
97
98 ## Throws:
99
100 <?php
101 foreach ($tags as $tag) :
102 ?>* <?= $tag->getType()
103 ?><?php
104 if ($tag->getDescription()?->getBodyTemplate()) :
105 ?>, <?= $tag->getDescription()
106 ?><?php
107 endif;
108 ?><?="\n"
109 ?><?php
110 endforeach;
111 endif;
112 ?>
113
114
115 <?php