Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,34 @@ public function getFileContentsForPendingOperation(string $targetPath): string
public function createClassNameDetails(string $name, string $namespacePrefix, string $suffix = '', string $validationErrorMessage = ''): ClassNameDetails
{
$fullNamespacePrefix = $this->namespacePrefix.'\\'.$namespacePrefix;
$suggestedClassName = null;
if ('\\' === $name[0]) {
// class is already "absolute" - leave it alone (but strip opening \)
$className = substr($name, 1);
} else {
$className = Str::asClassName($name, $suffix);
$suggestedClassName = rtrim($fullNamespacePrefix, '\\').'\\'.$className;

try {
Validator::classDoesNotExist($className);
$className = rtrim($fullNamespacePrefix, '\\').'\\'.$className;
$className = $suggestedClassName;
$suggestedClassName = null;
} catch (RuntimeCommandException) {
}
}

if (null !== $suggestedClassName && class_exists($suggestedClassName)) {
Validator::validateClassName($suggestedClassName, $validationErrorMessage);

// if this is a custom class, we may be completely different than the namespace prefix
// the best way can do, is find the PSR4 prefix and use that
if (!str_starts_with($suggestedClassName, $fullNamespacePrefix)) {
$fullNamespacePrefix = $this->fileManager->getNamespacePrefixForClass($suggestedClassName);
}

return new ClassNameDetails($suggestedClassName, $fullNamespacePrefix, $suffix);
}

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

// if this is a custom class, we may be completely different than the namespace prefix
Expand Down
Loading