#!/usr/bin/env php
<?php
-function m($m) {
+function m($m, $c = null) {
$n = "";
+ if ($c && $c->isInterface()) {
+ $m ^= ReflectionMethod::IS_ABSTRACT;
+ }
foreach (Reflection::getModifierNames($m) as $mn) {
$n .= $mn . " ";
}
return $n;
}
-function t($p) {
- if ($c = $p->getClass()) return "\\" . $c->getName() . " ";
- if ($p->isArray()) return "array ";
+function t(ReflectionParameter $p) {
+ if ($c = $p->getClass()) return ($p->allowsNull()?"?":"") . "\\" . $c->getName() . " ";
+ if ($p->isArray()) return ($p->allowsNull() ? "?":"") . "array ";
+ if ($p->hasType()) {
+ return ($p->allowsNull() ? "?" : "") . ($p->getType()->isBuiltin() ? "" : "\\") . $p->getType()->getName() . " ";
+ }
}
function c($n, $c) {
$_=$c;
}
$ps[] = $p1;
}
- fprintf($out, "%s) {\n\t}\n", implode(", ", $ps));
+ fprintf($out, "%s)", implode(", ", $ps));
+ if ($f->hasReturnType()) {
+ fprintf($out, " : %s%s", $f->getReturnType()->allowsNull() ? "?":"", $f->getReturnType()->getName());
+ }
+ fprintf($out, " {\n\t}\n");
}
}
//
fprintf($out, "extends \\%s ", $p->getName());
}
if ($i = $c->getInterfaceNames()) {
- fprintf($out, "implements \\%s ",
- implode(", \\", array_filter($i, function($v) {
+ fprintf($out, "%s \\%s ",
+ $c->isInterface() ? "extends" : "implements",
+ implode(", \\", array_filter($i, function($v) use($c, $i) {
+ foreach ($i as $ii) {
+ if ($v != $ii && (new ReflectionClass($ii))->implementsInterface("\\".$v)) {
+ return false;
+ }
+ }
return $v != "Traversable";
}))
foreach ($c->getMethods() as $m) {
if ($m->getDeclaringClass()->getName() == $c->getName()) {
- fprintf($out, "\t\t%sfunction %s(", m($m->getModifiers()),
+ fprintf($out, "\t\t%sfunction %s(", m($m->getModifiers(), $c),
$m->getName());
$ps = array();
foreach ($m->getParameters() as $p) {
$ps[] = $p1;
}
fprintf($out, "%s)", implode(", ", $ps));
+ if ($m->hasReturnType()) {
+ fprintf($out, " : %s%s%s", $m->getReturnType()->allowsNull() ? "?":"",
+ $m->getReturnType()->isBuiltin() ? "" : "\\", //0 === strpos($m->getReturnType()->getName(), $c->getNamespaceName()) ? "\\":"",
+ $m->getReturnType()->getName());
+ }
if ($m->isAbstract()) {
fprintf($out, ";\n\n");
} else {