Skip to content

Commit 42e818c

Browse files
committed
refactoring
1 parent 77c0c9d commit 42e818c

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,9 @@ public function __toString()
116116
. ";\n";
117117
}
118118

119-
$extends = (array) $this->extends;
120-
$implements = $this->implements;
121-
$traits = $this->traits;
122-
if ($this->namespace) {
123-
$extends = array_map([$this->namespace, 'unresolveName'], $extends);
124-
$implements = array_map([$this->namespace, 'unresolveName'], $implements);
125-
$traits = array_map([$this->namespace, 'unresolveName'], $traits);
126-
}
119+
$mapper = function (array $arr) {
120+
return array_map([$this->namespace ?: new PhpNamespace, 'unresolveName'], $arr);
121+
};
127122

128123
foreach ($this->methods as $method) {
129124
$method->setNamespace($this->namespace);
@@ -135,11 +130,11 @@ public function __toString()
135130
. ($this->final ? 'final ' : '')
136131
. $this->type . ' '
137132
. $this->name . ' '
138-
. ($extends ? 'extends ' . implode(', ', $extends) . ' ' : '')
139-
. ($implements ? 'implements ' . implode(', ', $implements) . ' ' : '')
133+
. ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '')
134+
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
140135
. "\n{\n"
141136
. Strings::indent(
142-
($traits ? 'use ' . implode(";\nuse ", $traits) . ";\n\n" : '')
137+
($this->traits ? 'use ' . implode(";\nuse ", $mapper($this->traits)) . ";\n\n" : '')
143138
. ($this->consts ? implode('', $consts) . "\n" : '')
144139
. ($this->properties ? implode("\n", $properties) . "\n" : '')
145140
. ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1)

src/PhpGenerator/Method.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ public function __construct($name = NULL)
103103
*/
104104
public function __toString()
105105
{
106+
$namespace = $this->namespace ?: new PhpNamespace;
106107
$parameters = [];
107108
foreach ($this->parameters as $param) {
108109
$variadic = $this->variadic && $param === end($this->parameters);
109-
$hint = $this->namespace
110-
? $this->namespace->unresolveName((string) $param->getTypeHint())
111-
: $param->getTypeHint();
112110

113-
$parameters[] = ($hint ? $hint . ' ' : '')
111+
$parameters[] = ($param->getTypeHint() ? $namespace->unresolveName($param->getTypeHint()) . ' ' : '')
114112
. ($param->isReference() ? '&' : '')
115113
. ($variadic ? '...' : '')
116114
. '$' . $param->getName()
@@ -120,9 +118,6 @@ public function __toString()
120118
foreach ($this->uses as $param) {
121119
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
122120
}
123-
$returnType = $this->namespace
124-
? $this->namespace->unresolveName((string) $this->returnType)
125-
: $this->returnType;
126121

127122
return ($this->comment ? str_replace("\n", "\n * ", "/**\n" . $this->comment) . "\n */\n" : '')
128123
. ($this->abstract ? 'abstract ' : '')
@@ -134,7 +129,7 @@ public function __toString()
134129
. ' ' . $this->name
135130
. '(' . implode(', ', $parameters) . ')'
136131
. ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
137-
. ($returnType ? ': ' . $returnType : '')
132+
. ($this->returnType ? ': ' . $namespace->unresolveName($this->returnType) : '')
138133
. ($this->abstract || $this->body === FALSE ? ';'
139134
: ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
140135
}

0 commit comments

Comments
 (0)