Skip to content

Commit eb4542f

Browse files
authored
bug #1422 Generator verifies if class exists before altering class name
1 parent f661302 commit eb4542f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Generator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st
147147
// class is already "absolute" - leave it alone (but strip opening \)
148148
$className = substr($name, 1);
149149
} else {
150-
$className = rtrim($fullNamespacePrefix, '\\').'\\'.Str::asClassName($name, $suffix);
150+
$className = Str::asClassName($name, $suffix);
151+
152+
try {
153+
Validator::classDoesNotExist($className);
154+
$className = rtrim($fullNamespacePrefix, '\\').'\\'.$className;
155+
} catch (RuntimeCommandException $e) {
156+
}
151157
}
152158

153159
Validator::validateClassName($className, $validationErrorMessage);

tests/GeneratorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,29 @@ public function getClassNameDetailsTests(): \Generator
7676
'App\\Entity\\User',
7777
'User',
7878
];
79+
80+
yield 'non_prefixed_fake_fqcn' => [
81+
'App\\Entity\\User',
82+
'',
83+
'',
84+
'App\\App\\Entity\\User',
85+
'Entity\\User',
86+
];
87+
88+
yield 'real_fqcn_with_suffix' => [
89+
'Symfony\\Bundle\\MakerBundle\\Tests\\Generator',
90+
'Test',
91+
'Test',
92+
'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest',
93+
'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest',
94+
];
95+
96+
yield 'real_fqcn_without_suffix' => [
97+
'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest',
98+
'',
99+
'',
100+
'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest',
101+
'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest',
102+
];
79103
}
80104
}

0 commit comments

Comments
 (0)