Skip to content

Commit 5c867a8

Browse files
committed
added ClassLike::setNamespace(), replaces parameter $namespace in constructor
1 parent 82bb703 commit 5c867a8

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/PhpGenerator/ClassLike.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public static function fromCode(string $code): static
6464
}
6565

6666

67-
public function __construct(string $name, ?PhpNamespace $namespace = null)
67+
public function __construct(string $name)
6868
{
6969
if (str_contains($name, '\\')) {
7070
$this->namespace = new PhpNamespace(Helpers::extractNamespace($name));
7171
$this->setName(Helpers::extractShortName($name));
7272
} else {
73-
$this->namespace = $namespace;
73+
$this->namespace = func_num_args() > 1 ? func_get_arg(1) : null; // backward compatibility
7474
$this->setName($name);
7575
}
7676
}
@@ -82,7 +82,15 @@ public function __toString(): string
8282
}
8383

8484

85-
/** @deprecated an object can be in multiple namespaces */
85+
/** @internal */
86+
public function setNamespace(?PhpNamespace $namespace): static
87+
{
88+
$this->namespace = $namespace;
89+
return $this;
90+
}
91+
92+
93+
/** @internal */
8694
public function getNamespace(): ?PhpNamespace
8795
{
8896
return $this->namespace;

src/PhpGenerator/ClassType.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ final class ClassType extends ClassLike
3939
private array $implements = [];
4040

4141

42-
public function __construct(?string $name = null, ?PhpNamespace $namespace = null)
42+
public function __construct(?string $name = null)
4343
{
44+
parent::__construct($name ?? 'foo', func_num_args() > 1 ? func_get_arg(1) : null); // backward compatibility
4445
if ($name === null) {
45-
parent::__construct('foo', $namespace);
4646
$this->setName(null);
47-
} else {
48-
parent::__construct($name, $namespace);
4947
}
5048
}
5149

src/PhpGenerator/PhpNamespace.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function add(ClassType|InterfaceType|TraitType|EnumType $class): static
280280
*/
281281
public function addClass(string $name): ClassType
282282
{
283-
$this->add($class = new ClassType($name, $this));
283+
$this->add($class = (new ClassType($name))->setNamespace($this));
284284
return $class;
285285
}
286286

@@ -290,7 +290,7 @@ public function addClass(string $name): ClassType
290290
*/
291291
public function addInterface(string $name): InterfaceType
292292
{
293-
$this->add($iface = new InterfaceType($name, $this));
293+
$this->add($iface = (new InterfaceType($name))->setNamespace($this));
294294
return $iface;
295295
}
296296

@@ -300,7 +300,7 @@ public function addInterface(string $name): InterfaceType
300300
*/
301301
public function addTrait(string $name): TraitType
302302
{
303-
$this->add($trait = new TraitType($name, $this));
303+
$this->add($trait = (new TraitType($name))->setNamespace($this));
304304
return $trait;
305305
}
306306

@@ -310,7 +310,7 @@ public function addTrait(string $name): TraitType
310310
*/
311311
public function addEnum(string $name): EnumType
312312
{
313-
$this->add($enum = new EnumType($name, $this));
313+
$this->add($enum = (new EnumType($name))->setNamespace($this));
314314
return $enum;
315315
}
316316

0 commit comments

Comments
 (0)