X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FStructure.php;h=70233cbbc6fe99914313184e27f1299e762f0f6c;hb=d3f229b258375f7965ecadcbf076be61c5874faa;hp=aed35897892864a16b101f319a063e927d9c7763;hpb=29d603eb211a184106cf3caa224777615bcd61a8;p=mdref%2Fmdref diff --git a/mdref/Structure.php b/mdref/Structure.php index aed3589..70233cb 100644 --- a/mdref/Structure.php +++ b/mdref/Structure.php @@ -149,6 +149,7 @@ class Structure { static $pattern = '/ \*\s+ (?P\w+\s+)* + (?:\((?P(?:(?:\w+)\s*)*)\))*\s* (?P[\\\\\w]+)\s+ (?\$\w+) (?:\s*=\s*(?P.+))? @@ -187,8 +188,8 @@ class Structure { static $pattern = '/ \*\s+ (?P\w+\s+)* - (?P[\\\\\w]+)\s+ - (?\$\w+) + (?P[\\\\\w_]+)\s+ + (?P&)?(?P\$[\w_]+) (?:\s*=\s*(?P.+))? (?P(?:\s*[^*]*\n(?!\n)\s*[^\*].*)*) /x'; @@ -204,20 +205,17 @@ class Structure { private function getReturns() : array { static $pattern = '/ \*\s+ - (?[\\\\\w]+) + (?[\\\\\w_]+) \s*,?\s* (?P(?:.|\n(?!\s*\*))*) /x'; - + $returns = $this->splitList($pattern, $this->getSection("Returns")); - $retdesc = ""; + $retvals = []; foreach ($returns as list(, $type, $desc)) { - if (strlen($retdesc)) { - $retdesc .= "\n\t or $type $desc"; - } else { - $retdesc = $desc; - } + $retvals[] = [$type, $desc]; } + return $retvals; return [implode("|", array_unique(array_column($returns, "type"))), $retdesc]; } @@ -252,11 +250,11 @@ abstract class StructureOf { // abstract function format(); - function formatDesc(int $level, array $tags = []) { + function formatDesc($level, array $tags = []) { $indent = str_repeat("\t", $level); $desc = trim($this->desc); if (false !== stristr($desc, "deprecated in")) { - $tags[] = "@deprecated"; + $tags[] = "deprecated"; } if ($tags) { $desc .= "\n\n@" . implode("\n@", $tags); @@ -264,6 +262,43 @@ abstract class StructureOf { $desc = preg_replace('/[\t ]*\n/',"\n$indent * ", $desc); printf("%s/**\n%s * %s\n%s */\n", $indent, $indent, $desc, $indent); } + + function saneTypes(array $types) { + $sane = []; + foreach ($types as $type) { + if (strlen($s = $this->saneType($type, false))) { + $sane[] = $s; + } + } + return $sane; + } + + function saneType($type, $strict = true) { + switch (strtolower($type)) { + case "object": + case "resource": + case "stream": + case "mixed": + case "true": + case "false": + case "null": + if ($strict) { + break; + } + /* fallthrough */ + case "bool": + case "int": + case "float": + case "string": + case "array": + case "callable": + return $type; + break; + default: + return ($type[0] === "\\" ? "":"\\") . $type; + break; + } + } } class StructureOfRoot extends StructureOf { @@ -319,8 +354,6 @@ class StructureOfClass extends StructureOfNs public $ns; public $props; - static $lastNs; - function format() { if ($this->ns !== StructureOfNs::$last) { printf("namespace %s;\n", $this->ns); @@ -362,23 +395,54 @@ class StructureOfFunc extends StructureOf { public $returns; public $throws; + function omitParamTypes() { + switch ($this->name) { + // ArrayAccess + case "offsetGet": + case "offsetSet": + case "offsetExists": + case "offsetUnset": + // Serializable + case "serialize": + case "unserialize": + return true; + } + return false; + } + function format(int $level) { $tags = []; foreach ($this->params as $param) { - $tags[] = "param {$param->type} {$param->name} {$param->desc}"; + $type = $this->saneType($param->type, false); + $tags[] = "param {$type} {$param->name} {$param->desc}"; } foreach ($this->throws as $throws) { - $tags[] = "throws $throws"; + $tags[] = "throws " . $this->saneType($throws); } - if ($this->name !== "__construct" && $this->returns[0]) { - $tags[] = "return {$this->returns[0]} {$this->returns[1]}"; + if ($this->name !== "__construct" && $this->returns) { + + if (count($this->returns) > 1) { + $type = implode("|", $this->saneTypes(array_column($this->returns, 0))); + $desc = ""; + foreach ($this->returns as list($typ, $ret)) { + if (strlen($desc)) { + $desc .= "\n\t\t or "; + } + $desc .= $this->saneType($typ, false) . " " . $ret; + } + } else { + $type = $this->saneType($this->returns[0][0], false); + $desc = $this->returns[0][1]; + } + $tags[] = "return $type $desc"; } $this->formatDesc(1, $tags); printf("\tfunction %s(", $this->name); $comma = ""; + $omit = $this->omitParamTypes(); foreach ($this->params as $param) { print $comma; - $param->formatAsParam($level); + $param->formatAsParam($level, !$omit); $comma = ", "; } printf(")"); @@ -403,7 +467,9 @@ class StructureOfVar extends StructureOf { public $type; public $desc; public $modifiers; + public $usages; public $defval; + public $ref; function formatDefval() { if (strlen($this->defval)) { @@ -411,6 +477,9 @@ class StructureOfVar extends StructureOf { printf(" = "); var_export(constant($this->defval)); } else if (strlen($this->defval)) { + if (false !== strchr($this->defval, "\\") && $this->defval[0] != "\\") { + $this->defval = "\\" . $this->defval; + } printf(" = %s", $this->defval); } } elseif ($this->modifiers) { @@ -419,18 +488,21 @@ class StructureOfVar extends StructureOf { } } } - function formatAsProp(int $level) { + function formatAsProp($level) { $indent = str_repeat("\t", $level); $this->formatDesc($level, - preg_split('/\s+/', $this->modifiers, -1, PREG_SPLIT_NO_EMPTY) - + [-1 => "var {$this->type}"] + preg_split('/\s+/', $this->modifiers ." " . $this->usages, -1, PREG_SPLIT_NO_EMPTY) + + [-1 => "var " . $this->saneType($this->type, false)] ); printf("%s%s %s", $indent, $this->modifiers, $this->name); $this->formatDefval(); } - function formatAsParam(int $level) { - printf("%s", $this->name); + function formatAsParam($level, $with_type = true) { + if ($with_type && strlen($type = $this->saneType($this->type))) { + printf("%s ", $type); + } + printf("%s%s", $this->ref, $this->name); $this->formatDefval(); } }