Skip to content

Commit e8f1ebc

Browse files
committed
Namespace prefixes fix.
1 parent 2c2de4d commit e8f1ebc

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/ReflectionApi/RuntimeClass.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,11 @@ public function getClassName(): string
9696

9797
public function implements(string ...$interfaces): self
9898
{
99-
foreach ($interfaces as $interface) {
99+
foreach ($interfaces as &$interface) {
100100
if (!interface_exists($interface)) {
101101
throw ReflectionApiException::forNonExistentInterface($interface);
102102
}
103+
$interface = '\\' . $interface;
103104
}
104105
$this->implements = $interfaces;
105106

@@ -111,17 +112,18 @@ public function extends(string $class): self
111112
if (!class_exists($class)) {
112113
throw ReflectionApiException::forNonExistentClass($class);
113114
}
114-
$this->extends = $class;
115+
$this->extends = '\\' . $class;
115116

116117
return $this;
117118
}
118119

119120
public function use(string ...$traits): self
120121
{
121-
foreach ($traits as $trait) {
122+
foreach ($traits as &$trait) {
122123
if (!trait_exists($trait)) {
123124
throw ReflectionApiException::forNonExistentTrait($trait);
124125
}
126+
$trait = '\\' . $trait;
125127
}
126128
$this->uses = $traits;
127129

@@ -130,22 +132,17 @@ public function use(string ...$traits): self
130132

131133
public function isUsing(string $name): bool
132134
{
133-
return in_array($name, $this->uses);
134-
}
135-
136-
public function isImplementing(string $name): bool
137-
{
138-
return in_array($name, $this->implements);
135+
return in_array('\\' . $name, $this->uses);
139136
}
140137

141138
public function isExtending(string $name): bool
142139
{
143-
return $name === $this->extends;
140+
return ('\\' . $name) === $this->extends;
144141
}
145142

146143
public function implementsInterface(string $interface): bool
147144
{
148-
return in_array($interface, $this->implements);
145+
return in_array('\\' . $interface, $this->implements);
149146
}
150147

151148
public function addMethod(RuntimeMethod $method): self

tests/Unit/Utils/ReflectionApi/RuntimeClassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ public function testImplements(): void
6666
$class = new RuntimeClass('TestClassC');
6767
$class->implements(AInterface::class);
6868

69-
self::assertSame('class TestClassC implements ' . AInterface::class, explode(PHP_EOL, $class->generateCode())[0]);
70-
self::assertTrue($class->isImplementing(AInterface::class));
69+
self::assertSame('class TestClassC implements \\' . AInterface::class, explode(PHP_EOL, $class->generateCode())[0]);
70+
self::assertTrue($class->implementsInterface(AInterface::class));
7171
}
7272

7373
public function testExtends(): void
7474
{
7575
$class = new RuntimeClass('TestClassD');
7676
$class->extends(\stdClass::class);
7777

78-
self::assertSame('class TestClassD extends stdClass', explode(PHP_EOL, $class->generateCode())[0]);
78+
self::assertSame('class TestClassD extends \stdClass', explode(PHP_EOL, $class->generateCode())[0]);
7979
self::assertTrue($class->isExtending(\stdClass::class));
8080
}
8181

0 commit comments

Comments
 (0)