@@ -57,17 +57,16 @@ public function getName(bool $emptyParent = false): string
5757
5858 public function getClassName (): ?string
5959 {
60+ if ($ this ->getName ()) {
61+ return $ this ->sanitizeClassName ($ this ->getName ());
62+ }
6063 $ prefix = $ this ->parent ?->getClassName() ?? '' ;
6164 $ name = $ this ->getName (true ) ?: $ this ->getParentKey ();
6265 if (!$ name || ($ this ->parent && !$ prefix )) {
6366 return null ;
6467 }
6568
66- return str_replace (
67- ['$ ' , ' ' , '- ' , '. ' ],
68- ['' , '_ ' , '_ ' , '_ ' ],
69- $ prefix === '' ? $ name : $ prefix . '_ ' . $ name ,
70- );
69+ return $ this ->sanitizeClassName ($ prefix === '' ? $ name : ($ prefix . '_ ' . $ name ));
7170 }
7271
7372 public function getParent (): ?self
@@ -142,7 +141,9 @@ public function getInherits(): array
142141 $ inherits = (string ) $ this ->xmlElement ->attributes ()['inherits ' ] ?? '' ;
143142 $ inherits = str_replace (' ' , '' , $ inherits );
144143
145- return $ inherits === '' ? [] : explode (', ' , $ inherits );
144+ return $ inherits === ''
145+ ? []
146+ : array_map ($ this ->sanitizeClassName (...), explode (', ' , $ inherits ));
146147 }
147148
148149 /**
@@ -191,4 +192,13 @@ public function getLineNumber(): int
191192
192193 return $ node ->getLineNo ();
193194 }
195+
196+ private function sanitizeClassName (string $ name ): string
197+ {
198+ return str_replace (
199+ ['$ ' , ' ' , '- ' , '. ' , '! ' ],
200+ ['' , '_ ' , '_ ' , '_ ' , '_ ' ],
201+ $ name ,
202+ );
203+ }
194204}
0 commit comments