Skip to content

Commit f9589f4

Browse files
committed
ClassType: improved rendering of anonymous classes
1 parent 4baf84d commit f9589f4

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ClassType
3030
/** @var PhpNamespace|NULL */
3131
private $namespace;
3232

33-
/** @var string */
33+
/** @var string|NULL */
3434
private $name;
3535

3636
/** @var string class|interface|trait */
@@ -72,7 +72,7 @@ public static function from($from)
7272
{
7373
$from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from);
7474
if (PHP_VERSION_ID >= 70000 && $from->isAnonymous()) {
75-
$class = new static('anonymous');
75+
$class = new static;
7676
} else {
7777
$class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
7878
}
@@ -99,7 +99,7 @@ public static function from($from)
9999
}
100100

101101

102-
public function __construct($name = '', PhpNamespace $namespace = NULL)
102+
public function __construct($name = NULL, PhpNamespace $namespace = NULL)
103103
{
104104
$this->setName($name);
105105
$this->namespace = $namespace;
@@ -134,18 +134,17 @@ public function __toString()
134134
Helpers::formatDocComment($this->comment . "\n")
135135
. ($this->abstract ? 'abstract ' : '')
136136
. ($this->final ? 'final ' : '')
137-
. $this->type . ' '
138-
. $this->name . ' '
137+
. ($this->name ? "$this->type $this->name " : '')
139138
. ($this->extends ? 'extends ' . implode(', ', $mapper((array) $this->extends)) . ' ' : '')
140139
. ($this->implements ? 'implements ' . implode(', ', $mapper($this->implements)) . ' ' : '')
141-
. "\n{\n"
140+
. ($this->name ? "\n" : '') . "{\n"
142141
. Strings::indent(
143142
($this->traits ? 'use ' . implode(";\nuse ", $mapper($this->traits)) . ";\n\n" : '')
144143
. ($this->consts ? implode('', $consts) . "\n" : '')
145144
. ($this->properties ? implode("\n", $properties) . "\n" : '')
146145
. ($this->methods ? "\n" . implode("\n\n\n", $this->methods) . "\n\n" : ''), 1)
147146
. '}'
148-
) . "\n";
147+
) . ($this->name ? "\n" : '');
149148
}
150149

151150

@@ -159,18 +158,18 @@ public function getNamespace()
159158

160159

161160
/**
162-
* @param string
161+
* @param string|NULL
163162
* @return static
164163
*/
165164
public function setName($name)
166165
{
167-
$this->name = (string) $name;
166+
$this->name = $name;
168167
return $this;
169168
}
170169

171170

172171
/**
173-
* @return string
172+
* @return string|NULL
174173
*/
175174
public function getName()
176175
{

tests/PhpGenerator/ClassType.from.php7.expect

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
class anonymous
21
{
32
public $a;
43

@@ -15,12 +14,10 @@ class anonymous
1514
}
1615

1716
}
18-
19-
class anonymous extends Class1
20-
{
17+
extends Class1 {
2118

2219
public function a()
2320
{
2421
}
2522

26-
}
23+
}

0 commit comments

Comments
 (0)