Skip to content

Commit 7bd0bd1

Browse files
committed
Allow creation of classes with the same name as PHP native classes
1 parent dcd2260 commit 7bd0bd1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Generator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ public function getFileContentsForPendingOperation(string $targetPath): string
180180
*/
181181
public function createClassNameDetails(string $name, string $namespacePrefix, string $suffix = '', string $validationErrorMessage = ''): ClassNameDetails
182182
{
183+
if ('' === $name) {
184+
throw new \InvalidArgumentException('The name of the new class cannot be empty.');
185+
}
186+
183187
$fullNamespacePrefix = $this->namespacePrefix.'\\'.$namespacePrefix;
184188
if ('\\' === $name[0]) {
185189
// class is already "absolute" - leave it alone (but strip opening \)
@@ -188,8 +192,8 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st
188192
$className = Str::asClassName($name, $suffix);
189193

190194
try {
191-
Validator::classDoesNotExist($className);
192195
$className = rtrim($fullNamespacePrefix, '\\').'\\'.$className;
196+
Validator::classDoesNotExist($className);
193197
} catch (RuntimeCommandException) {
194198
}
195199
}

tests/Maker/MakeEntityTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,19 @@ public function getTestDetails(): \Generator
762762
$this->runEntityTest($runner);
763763
}),
764764
];
765+
766+
yield 'its_can_create_classes_with_native_class_name' => [$this->createMakeEntityTest()
767+
->run(function (MakerTestRunner $runner) {
768+
$runner->runMaker([
769+
// entity class name
770+
'Directory',
771+
// add not additional fields
772+
'',
773+
]);
774+
775+
$this->runEntityTest($runner);
776+
}),
777+
];
765778
}
766779

767780
/** @param array<string, mixed> $data */

0 commit comments

Comments
 (0)