@@ -181,19 +181,34 @@ public function getFileContentsForPendingOperation(string $targetPath): string
181181 public function createClassNameDetails (string $ name , string $ namespacePrefix , string $ suffix = '' , string $ validationErrorMessage = '' ): ClassNameDetails
182182 {
183183 $ fullNamespacePrefix = $ this ->namespacePrefix .'\\' .$ namespacePrefix ;
184+ $ suggestedClassName = null ;
184185 if ('\\' === $ name [0 ]) {
185186 // class is already "absolute" - leave it alone (but strip opening \)
186187 $ className = substr ($ name , 1 );
187188 } else {
188189 $ className = Str::asClassName ($ name , $ suffix );
190+ $ suggestedClassName = rtrim ($ fullNamespacePrefix , '\\' ).'\\' .$ className ;
189191
190192 try {
191193 Validator::classDoesNotExist ($ className );
192- $ className = rtrim ($ fullNamespacePrefix , '\\' ).'\\' .$ className ;
194+ $ className = $ suggestedClassName ;
195+ $ suggestedClassName = null ;
193196 } catch (RuntimeCommandException ) {
194197 }
195198 }
196199
200+ if (null !== $ suggestedClassName ) {
201+ Validator::validateClassName ($ suggestedClassName , $ validationErrorMessage );
202+
203+ // if this is a custom class, we may be completely different than the namespace prefix
204+ // the best way can do, is find the PSR4 prefix and use that
205+ if (!str_starts_with ($ suggestedClassName , $ fullNamespacePrefix )) {
206+ $ fullNamespacePrefix = $ this ->fileManager ->getNamespacePrefixForClass ($ suggestedClassName );
207+ }
208+
209+ return new ClassNameDetails ($ suggestedClassName , $ fullNamespacePrefix , $ suffix );
210+ }
211+
197212 Validator::validateClassName ($ className , $ validationErrorMessage );
198213
199214 // if this is a custom class, we may be completely different than the namespace prefix
0 commit comments