Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 9, 2018
1 parent d346ca0 commit 1527345
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
20 changes: 9 additions & 11 deletions src/PhpGenerator/ClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ public function __construct(string $name = null, PhpNamespace $namespace = null)

public function __toString(): string
{
$resolver = $this->namespace ? [$this->namespace, 'unresolveName'] : function ($s) { return $s; };

$traits = [];
foreach ($this->traits as $trait => $resolutions) {
$traits[] = 'use ' . ($this->namespace ? $this->namespace->unresolveName($trait) : $trait)
$traits[] = 'use ' . $resolver($trait)
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
}

Expand All @@ -103,23 +105,19 @@ public function __toString(): string
. ';';
}

$mapper = function (array $arr) {
return $this->namespace ? array_map([$this->namespace, 'unresolveName'], $arr) : $arr;
};

return Strings::normalize(
Helpers::formatDocComment($this->comment . "\n")
. ($this->abstract ? 'abstract ' : '')
. ($this->final ? 'final ' : '')
. ($this->name ? "$this->type $this->name " : '')
. ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '')
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
. ($this->extends ? 'extends ' . implode(', ', array_map($resolver, (array) $this->extends)) . ' ' : '')
. ($this->implements ? 'implements ' . implode(', ', array_map($resolver, $this->implements)) . ' ' : '')
. ($this->name ? "\n" : '') . "{\n"
. Strings::indent(
($this->traits ? implode("\n", $traits) . "\n\n" : '')
. ($this->consts ? implode("\n", $consts) . "\n\n" : '')
. ($this->properties ? implode("\n\n", $properties) . "\n\n\n" : '')
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''), 1)
($traits ? implode("\n", $traits) . "\n\n" : '')
. ($consts ? implode("\n", $consts) . "\n\n" : '')
. ($properties ? implode("\n\n", $properties) . "\n\n\n" : '')
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''))
. '}'
) . ($this->name ? "\n" : '');
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpGenerator/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __toString(): string
return 'function '
. ($this->returnReference ? '&' : '')
. $this->parametersToString()
. ($this->uses ? " use ($useStr)" : '')
. ($uses ? " use ($useStr)" : '')
. $this->returnTypeToString()
. " {\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}';
. " {\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n")) . '}';
}


Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/GlobalFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public function __toString(): string
. $this->name
. $this->parametersToString()
. $this->returnTypeToString()
. "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}';
. "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n")) . '}';
}
}
2 changes: 1 addition & 1 deletion src/PhpGenerator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static function formatArgs(string $statement, array $args): string
$items[] = self::dump($tmp);
}
$res .= strlen($tmp = implode(', ', $items)) > self::WRAP_LENGTH && count($items) > 1
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items), 1) . "\n"
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items)) . "\n"
: $tmp;

} else { // $ -> ::
Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __toString(): string
? ';'
: (strpos($params, "\n") === false ? "\n" : ' ')
. "{\n"
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1)
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"))
. '}');
}

Expand Down
22 changes: 11 additions & 11 deletions src/PhpGenerator/PhpNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function addUse(string $name, string $alias = null, string &$aliasOut = n

$aliasOut = $alias;
$this->uses[$alias] = $name;
asort($this->uses);
return $this;
}

Expand All @@ -127,9 +128,9 @@ public function unresolveName(string $name): string
$name = ltrim($name, '\\');
$res = null;
$lower = strtolower($name);
foreach ($this->uses as $alias => $for) {
if (Strings::startsWith($lower . '\\', strtolower($for) . '\\')) {
$short = $alias . substr($name, strlen($for));
foreach ($this->uses as $alias => $original) {
if (Strings::startsWith($lower . '\\', strtolower($original) . '\\')) {
$short = $alias . substr($name, strlen($original));
if (!isset($res) || strlen($res) > strlen($short)) {
$res = $short;
}
Expand Down Expand Up @@ -178,15 +179,14 @@ public function getClasses(): array
public function __toString(): string
{
$uses = [];
asort($this->uses);
foreach ($this->uses as $alias => $name) {
$useNamespace = Helpers::extractNamespace($name);
foreach ($this->uses as $alias => $original) {
$useNamespace = Helpers::extractNamespace($original);

if ($this->name !== $useNamespace) {
if ($alias === $name || substr($name, -(strlen($alias) + 1)) === '\\' . $alias) {
$uses[] = "use {$name};";
if ($alias === $original || substr($original, -(strlen($alias) + 1)) === '\\' . $alias) {
$uses[] = "use $original;";
} else {
$uses[] = "use {$name} as {$alias};";
$uses[] = "use $original as $alias;";
}
}
}
Expand All @@ -195,12 +195,12 @@ public function __toString(): string
. implode("\n", $this->classes);

if ($this->bracketedSyntax) {
return 'namespace' . ($this->name ? ' ' . $this->name : '') . " {\n\n"
return 'namespace' . ($this->name ? " $this->name" : '') . " {\n\n"
. Strings::indent($body)
. "\n}\n";

} else {
return ($this->name ? "namespace {$this->name};\n\n" : '')
return ($this->name ? "namespace $this->name;\n\n" : '')
. $body;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/expected/ClassType.from.expect
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Class2 extends Class1 implements Interface2
* Func3
* @return Class1
*/
private function &func3(array $a%a?%, Class2 $b = null, Unknown $c, \Xyz\Unknown $d, callable $e, $f = Abc\Unknown::ABC, $g)
private function &func3(array $a = [], Class2 $b = null, Unknown $c, \Xyz\Unknown $d, callable $e, $f = Abc\Unknown::ABC, $g)
{
}

Expand Down

0 comments on commit 1527345

Please sign in to comment.