Skip to content

Commit

Permalink
Blueprint: fixed compatibility with PhpGenerator 3.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 27, 2021
1 parent fe4fb56 commit 3c700a4
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/Latte/Runtime/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function addProperties(Php\ClassType $class, array $props, bool $native =
if ($native) {
$prop->setType($type);
} else {
$doctype = $printer->printType($type, false, $class->getNamespace()) ?: 'mixed';
$doctype = $this->printType($type, false, $class->getNamespace()) ?: 'mixed';
$prop->setComment("@var $doctype");
}
}
Expand All @@ -90,12 +90,48 @@ public function addFunctions(Php\ClassType $class, array $funcs): void
$printer = new Php\Printer;
foreach ($funcs as $name => $func) {
$method = (new Php\Factory)->fromCallable($func);
$type = $printer->printType($method->getReturnType(), $method->isReturnNullable(), $class->getNamespace()) ?: 'mixed';
$class->addComment("@method $type $name" . $printer->printParameters($method, $class->getNamespace()));
$type = $this->printType($method->getReturnType(), $method->isReturnNullable(), $class->getNamespace()) ?: 'mixed';
$class->addComment("@method $type $name" . $this->printParameters($method, $class->getNamespace()));
}
}


private function printType(?string $type, bool $nullable, ?Php\PhpNamespace $namespace): string
{
if ($type === null) {
return '';
}
if ($namespace) {
$type = $namespace->unresolveName($type);
}
if ($nullable && strcasecmp($type, 'mixed')) {
$type = strpos($type, '|') !== false
? $type . '|null'
: '?' . $type;
}
return $type;
}


/**
* @param Closure|GlobalFunction|Method $function
*/
public function printParameters($function, Php\PhpNamespace $namespace = null): string
{
$params = [];
$list = $function->getParameters();
foreach ($list as $param) {
$variadic = $function->isVariadic() && $param === end($list);
$params[] = ltrim($this->printType($param->getType(), $param->isNullable(), $namespace) . ' ')
. ($param->isReference() ? '&' : '')
. ($variadic ? '...' : '')
. '$' . $param->getName()
. ($param->hasDefaultValue() && !$variadic ? ' = ' . var_export($param->getDefaultValue(), true) : '');
}
return '(' . implode(', ', $params) . ')';
}


public function printCanvas(): string
{
echo "<div style='all:initial;position:fixed;overflow:auto;z-index:1000;left:0;right:0;top:0;bottom:0;color:black;background:white;padding:1em'>\n";
Expand Down

0 comments on commit 3c700a4

Please sign in to comment.