Skip to content

Commit 1527345

Browse files
committed
optimizations
1 parent d346ca0 commit 1527345

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
@@ -82,9 +82,11 @@ public function __construct(string $name = null, PhpNamespace $namespace = null)
8282

8383
public function __toString(): string
8484
{
85+
$resolver = $this->namespace ? [$this->namespace, 'unresolveName'] : function ($s) { return $s; };
86+
8587
$traits = [];
8688
foreach ($this->traits as $trait => $resolutions) {
87-
$traits[] = 'use ' . ($this->namespace ? $this->namespace->unresolveName($trait) : $trait)
89+
$traits[] = 'use ' . $resolver($trait)
8890
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
8991
}
9092

@@ -103,23 +105,19 @@ public function __toString(): string
103105
. ';';
104106
}
105107

106-
$mapper = function (array $arr) {
107-
return $this->namespace ? array_map([$this->namespace, 'unresolveName'], $arr) : $arr;
108-
};
109-
110108
return Strings::normalize(
111109
Helpers::formatDocComment($this->comment . "\n")
112110
. ($this->abstract ? 'abstract ' : '')
113111
. ($this->final ? 'final ' : '')
114112
. ($this->name ? "$this->type $this->name " : '')
115-
. ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '')
116-
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
113+
. ($this->extends ? 'extends ' . implode(', ', array_map($resolver, (array) $this->extends)) . ' ' : '')
114+
. ($this->implements ? 'implements ' . implode(', ', array_map($resolver, $this->implements)) . ' ' : '')
117115
. ($this->name ? "\n" : '') . "{\n"
118116
. Strings::indent(
119-
($this->traits ? implode("\n", $traits) . "\n\n" : '')
120-
. ($this->consts ? implode("\n", $consts) . "\n\n" : '')
121-
. ($this->properties ? implode("\n\n", $properties) . "\n\n\n" : '')
122-
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''), 1)
117+
($traits ? implode("\n", $traits) . "\n\n" : '')
118+
. ($consts ? implode("\n", $consts) . "\n\n" : '')
119+
. ($properties ? implode("\n\n", $properties) . "\n\n\n" : '')
120+
. ($this->methods ? implode("\n\n\n", $this->methods) . "\n" : ''))
123121
. '}'
124122
) . ($this->name ? "\n" : '');
125123
}

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
@@ -73,7 +73,7 @@ public function __toString(): string
7373
? ';'
7474
: (strpos($params, "\n") === false ? "\n" : ' ')
7575
. "{\n"
76-
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1)
76+
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"))
7777
. '}');
7878
}
7979

src/PhpGenerator/PhpNamespace.php

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

107107
$aliasOut = $alias;
108108
$this->uses[$alias] = $name;
109+
asort($this->uses);
109110
return $this;
110111
}
111112

@@ -127,9 +128,9 @@ public function unresolveName(string $name): string
127128
$name = ltrim($name, '\\');
128129
$res = null;
129130
$lower = strtolower($name);
130-
foreach ($this->uses as $alias => $for) {
131-
if (Strings::startsWith($lower . '\\', strtolower($for) . '\\')) {
132-
$short = $alias . substr($name, strlen($for));
131+
foreach ($this->uses as $alias => $original) {
132+
if (Strings::startsWith($lower . '\\', strtolower($original) . '\\')) {
133+
$short = $alias . substr($name, strlen($original));
133134
if (!isset($res) || strlen($res) > strlen($short)) {
134135
$res = $short;
135136
}
@@ -178,15 +179,14 @@ public function getClasses(): array
178179
public function __toString(): string
179180
{
180181
$uses = [];
181-
asort($this->uses);
182-
foreach ($this->uses as $alias => $name) {
183-
$useNamespace = Helpers::extractNamespace($name);
182+
foreach ($this->uses as $alias => $original) {
183+
$useNamespace = Helpers::extractNamespace($original);
184184

185185
if ($this->name !== $useNamespace) {
186-
if ($alias === $name || substr($name, -(strlen($alias) + 1)) === '\\' . $alias) {
187-
$uses[] = "use {$name};";
186+
if ($alias === $original || substr($original, -(strlen($alias) + 1)) === '\\' . $alias) {
187+
$uses[] = "use $original;";
188188
} else {
189-
$uses[] = "use {$name} as {$alias};";
189+
$uses[] = "use $original as $alias;";
190190
}
191191
}
192192
}
@@ -195,12 +195,12 @@ public function __toString(): string
195195
. implode("\n", $this->classes);
196196

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

202202
} else {
203-
return ($this->name ? "namespace {$this->name};\n\n" : '')
203+
return ($this->name ? "namespace $this->name;\n\n" : '')
204204
. $body;
205205
}
206206
}

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)