Skip to content

Commit 783e948

Browse files
committed
added ClassLike::setNamespace(), replaces parameter $namespace in constructor
1 parent fb46dc6 commit 783e948

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/PhpGenerator/ClassLike.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\PhpGenerator;
99

1010
use Nette;
11-
use function array_map, is_object, str_contains, strtolower;
11+
use function array_map, func_num_args, is_object, str_contains, strtolower;
1212

1313

1414
/**
@@ -62,13 +62,13 @@ public static function fromCode(string $code): static
6262
}
6363

6464

65-
public function __construct(string $name, ?PhpNamespace $namespace = null)
65+
public function __construct(string $name)
6666
{
6767
if (str_contains($name, '\\')) {
6868
$this->namespace = new PhpNamespace(Helpers::extractNamespace($name));
6969
$this->setName(Helpers::extractShortName($name));
7070
} else {
71-
$this->namespace = $namespace;
71+
$this->namespace = func_num_args() > 1 ? func_get_arg(1) : null; // backward compatibility
7272
$this->setName($name);
7373
}
7474
}
@@ -80,7 +80,14 @@ public function __toString(): string
8080
}
8181

8282

83-
/** @deprecated an object can be in multiple namespaces */
83+
/** @internal */
84+
public function setNamespace(?PhpNamespace $namespace): static
85+
{
86+
$this->namespace = $namespace;
87+
return $this;
88+
}
89+
90+
8491
public function getNamespace(): ?PhpNamespace
8592
{
8693
return $this->namespace;

src/PhpGenerator/ClassType.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\PhpGenerator;
99

1010
use Nette;
11-
use function array_diff, array_map, strtolower;
11+
use function array_diff, array_map, func_num_args, strtolower;
1212

1313

1414
/**
@@ -37,13 +37,11 @@ final class ClassType extends ClassLike
3737
private array $implements = [];
3838

3939

40-
public function __construct(?string $name = null, ?PhpNamespace $namespace = null)
40+
public function __construct(?string $name = null)
4141
{
42+
parent::__construct($name ?? 'foo', func_num_args() > 1 ? func_get_arg(1) : null); // backward compatibility
4243
if ($name === null) {
43-
parent::__construct('foo', $namespace);
4444
$this->setName(null);
45-
} else {
46-
parent::__construct($name, $namespace);
4745
}
4846
}
4947

src/PhpGenerator/PhpNamespace.php

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

@@ -288,7 +288,7 @@ public function addClass(string $name): ClassType
288288
*/
289289
public function addInterface(string $name): InterfaceType
290290
{
291-
$this->add($iface = new InterfaceType($name, $this));
291+
$this->add($iface = (new InterfaceType($name))->setNamespace($this));
292292
return $iface;
293293
}
294294

@@ -298,7 +298,7 @@ public function addInterface(string $name): InterfaceType
298298
*/
299299
public function addTrait(string $name): TraitType
300300
{
301-
$this->add($trait = new TraitType($name, $this));
301+
$this->add($trait = (new TraitType($name))->setNamespace($this));
302302
return $trait;
303303
}
304304

@@ -308,7 +308,7 @@ public function addTrait(string $name): TraitType
308308
*/
309309
public function addEnum(string $name): EnumType
310310
{
311-
$this->add($enum = new EnumType($name, $this));
311+
$this->add($enum = (new EnumType($name))->setNamespace($this));
312312
return $enum;
313313
}
314314

0 commit comments

Comments
 (0)