From bc1e99fda641a83155443b7c286ac4867240c7d8 Mon Sep 17 00:00:00 2001 From: Peter Labos Date: Tue, 23 Jan 2024 22:01:50 +0000 Subject: [PATCH] Generator verifies if class exists before altering class name --- src/Generator.php | 8 +++++++- tests/GeneratorTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Generator.php b/src/Generator.php index 44582900d..48543c6c6 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -147,7 +147,13 @@ public function createClassNameDetails(string $name, string $namespacePrefix, st // class is already "absolute" - leave it alone (but strip opening \) $className = substr($name, 1); } else { - $className = rtrim($fullNamespacePrefix, '\\').'\\'.Str::asClassName($name, $suffix); + $className = Str::asClassName($name, $suffix); + + try { + Validator::classDoesNotExist($className); + $className = rtrim($fullNamespacePrefix, '\\').'\\'.$className; + } catch (RuntimeCommandException $e) { + } } Validator::validateClassName($className, $validationErrorMessage); diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 51a290a28..9c1c85f84 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -76,5 +76,29 @@ public function getClassNameDetailsTests(): \Generator 'App\\Entity\\User', 'User', ]; + + yield 'non_prefixed_fake_fqcn' => [ + 'App\\Entity\\User', + '', + '', + 'App\\App\\Entity\\User', + 'Entity\\User', + ]; + + yield 'real_fqcn_with_suffix' => [ + 'Symfony\\Bundle\\MakerBundle\\Tests\\Generator', + 'Test', + 'Test', + 'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest', + 'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest', + ]; + + yield 'real_fqcn_without_suffix' => [ + 'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest', + '', + '', + 'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest', + 'Symfony\\Bundle\\MakerBundle\\Tests\\GeneratorTest', + ]; } }