Skip to content

Commit ea90209

Browse files
committed
optimizations
1 parent 841beb3 commit ea90209

File tree

7 files changed

+26
-28
lines changed

7 files changed

+26
-28
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ public function __construct(string $name = null, PhpNamespace $namespace = null)
8888

8989
public function __toString(): string
9090
{
91+
$resolver = $this->namespace ? [$this->namespace, 'unresolveName'] : function ($s) { return $s; };
92+
9193
$traits = [];
9294
foreach ($this->traits as $trait => $resolutions) {
93-
$traits[] = 'use ' . ($this->namespace ? $this->namespace->unresolveName($trait) : $trait)
95+
$traits[] = 'use ' . $resolver($trait)
9496
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
9597
}
9698

@@ -109,23 +111,19 @@ public function __toString(): string
109111
. ';';
110112
}
111113

112-
$mapper = function (array $arr) {
113-
return $this->namespace ? array_map([$this->namespace, 'unresolveName'], $arr) : $arr;
114-
};
115-
116114
return Strings::normalize(
117115
Helpers::formatDocComment($this->comment . "\n")
118116
. ($this->abstract ? 'abstract ' : '')
119117
. ($this->final ? 'final ' : '')
120118
. ($this->name ? "$this->type $this->name " : '')
121-
. ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '')
122-
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
119+
. ($this->extends ? 'extends ' . implode(', ', array_map($resolver, (array) $this->extends)) . ' ' : '')
120+
. ($this->implements ? 'implements ' . implode(', ', array_map($resolver, $this->implements)) . ' ' : '')
123121
. ($this->name ? "\n" : '') . "{\n"
124122
. Strings::indent(
125-
($this->traits ? implode("\n", $traits) . "\n\n" : '')
126-
. ($this->consts ? implode("\n", $consts) . "\n\n" : '')
127-
. ($this->properties ? implode("\n\n", $properties) . "\n\n\n" : '')
128-
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''), 1)
123+
($traits ? implode("\n", $traits) . "\n\n" : '')
124+
. ($consts ? implode("\n", $consts) . "\n\n" : '')
125+
. ($properties ? implode("\n\n", $properties) . "\n\n\n" : '')
126+
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''))
129127
. '}'
130128
) . ($this->name ? "\n" : '');
131129
}

src/PhpGenerator/Closure.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function __toString(): string
4848
return 'function '
4949
. ($this->returnReference ? '&' : '')
5050
. $this->parametersToString()
51-
. ($this->uses ? " use ($useStr)" : '')
51+
. ($uses ? " use ($useStr)" : '')
5252
. $this->returnTypeToString()
53-
. " {\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}';
53+
. " {\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n")) . '}';
5454
}
5555

5656

src/PhpGenerator/GlobalFunction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function __toString(): string
4141
. $this->name
4242
. $this->parametersToString()
4343
. $this->returnTypeToString()
44-
. "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}';
44+
. "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n")) . '}';
4545
}
4646
}

src/PhpGenerator/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static function formatArgs(string $statement, array $args): string
188188
$items[] = self::dump($tmp);
189189
}
190190
$res .= strlen($tmp = implode(', ', $items)) > self::WRAP_LENGTH && count($items) > 1
191-
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items), 1) . "\n"
191+
? "\n" . Nette\Utils\Strings::indent(implode(",\n", $items)) . "\n"
192192
: $tmp;
193193

194194
} else { // $ -> ::

src/PhpGenerator/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function __toString(): string
7878
? ';'
7979
: (strpos($params, "\n") === false ? "\n" : ' ')
8080
. "{\n"
81-
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1)
81+
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"))
8282
. '}');
8383
}
8484

src/PhpGenerator/PhpNamespace.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function addUse(string $name, string $alias = null, string &$aliasOut = n
115115

116116
$aliasOut = $alias;
117117
$this->uses[$alias] = $name;
118+
asort($this->uses);
118119
return $this;
119120
}
120121

@@ -136,9 +137,9 @@ public function unresolveName(string $name): string
136137
$name = ltrim($name, '\\');
137138
$res = null;
138139
$lower = strtolower($name);
139-
foreach ($this->uses as $alias => $for) {
140-
if (Strings::startsWith($lower . '\\', strtolower($for) . '\\')) {
141-
$short = $alias . substr($name, strlen($for));
140+
foreach ($this->uses as $alias => $original) {
141+
if (Strings::startsWith($lower . '\\', strtolower($original) . '\\')) {
142+
$short = $alias . substr($name, strlen($original));
142143
if (!isset($res) || strlen($res) > strlen($short)) {
143144
$res = $short;
144145
}
@@ -188,15 +189,14 @@ public function getClasses(): array
188189
public function __toString(): string
189190
{
190191
$uses = [];
191-
asort($this->uses);
192-
foreach ($this->uses as $alias => $name) {
193-
$useNamespace = Helpers::extractNamespace($name);
192+
foreach ($this->uses as $alias => $original) {
193+
$useNamespace = Helpers::extractNamespace($original);
194194

195195
if ($this->name !== $useNamespace) {
196-
if ($alias === $name || substr($name, -(strlen($alias) + 1)) === '\\' . $alias) {
197-
$uses[] = "use {$name};";
196+
if ($alias === $original || substr($original, -(strlen($alias) + 1)) === '\\' . $alias) {
197+
$uses[] = "use $original;";
198198
} else {
199-
$uses[] = "use {$name} as {$alias};";
199+
$uses[] = "use $original as $alias;";
200200
}
201201
}
202202
}
@@ -205,12 +205,12 @@ public function __toString(): string
205205
. implode("\n", $this->classes);
206206

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

212212
} else {
213-
return ($this->name ? "namespace {$this->name};\n\n" : '')
213+
return ($this->name ? "namespace $this->name;\n\n" : '')
214214
. $body;
215215
}
216216
}

tests/PhpGenerator/expected/ClassType.from.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Class2 extends Class1 implements Interface2
4444
* Func3
4545
* @return Class1
4646
*/
47-
private function &func3(array $a%a?%, Class2 $b = null, Unknown $c, \Xyz\Unknown $d, callable $e, $f = Abc\Unknown::ABC, $g)
47+
private function &func3(array $a = [], Class2 $b = null, Unknown $c, \Xyz\Unknown $d, callable $e, $f = Abc\Unknown::ABC, $g)
4848
{
4949
}
5050

0 commit comments

Comments
 (0)