@@ -36,15 +36,17 @@ public function getRootNode(): self
3636 return $ node ;
3737 }
3838
39- public function getName (): string
39+ public function getName (bool $ emptyParent = false ): string
4040 {
4141 if ($ this ->parent && str_contains ($ this ->name , '$parent ' )) {
4242 $ parent = $ this ;
4343 $ parentName = '' ;
44- while ($ parent = $ parent ->getParent ()) {
45- $ parentName = $ parent ->getName ();
46- if ($ parentName ) {
47- break ;
44+ if (!$ emptyParent ) {
45+ while ($ parent = $ parent ->getParent ()) {
46+ $ parentName = $ parent ->getName ();
47+ if ($ parentName ) {
48+ break ;
49+ }
4850 }
4951 }
5052 return str_replace ('$parent ' , $ parentName , $ this ->name );
@@ -55,17 +57,16 @@ public function getName(): string
5557
5658 public function getClassName (): ?string
5759 {
60+ if ($ this ->getName () && $ this ->getRootNode ()::class === Frame::class) {
61+ return $ this ->sanitizeClassName ($ this ->getName ());
62+ }
5863 $ prefix = $ this ->parent ?->getClassName() ?? '' ;
59- $ name = $ this ->getName () ?: $ this ->getParentKey ();
60- if (!$ name ) {
64+ $ name = $ this ->getName (true ) ?: $ this ->getParentKey ();
65+ if (!$ name || ( $ this -> parent && ! $ prefix ) ) {
6166 return null ;
6267 }
6368
64- return str_replace (
65- ['$ ' , ' ' , '- ' , '. ' ],
66- ['' , '_ ' , '_ ' , '_ ' ],
67- $ prefix === '' ? $ name : $ prefix . '_ ' . $ name ,
68- );
69+ return $ this ->sanitizeClassName ($ prefix === '' ? $ name : ($ prefix . '_ ' . $ name ));
6970 }
7071
7172 public function getParent (): ?self
@@ -140,7 +141,9 @@ public function getInherits(): array
140141 $ inherits = (string ) $ this ->xmlElement ->attributes ()['inherits ' ] ?? '' ;
141142 $ inherits = str_replace (' ' , '' , $ inherits );
142143
143- return $ inherits === '' ? [] : explode (', ' , $ inherits );
144+ return $ inherits === ''
145+ ? []
146+ : array_map ($ this ->sanitizeClassName (...), explode (', ' , $ inherits ));
144147 }
145148
146149 /**
@@ -189,4 +192,13 @@ public function getLineNumber(): int
189192
190193 return $ node ->getLineNo ();
191194 }
195+
196+ private function sanitizeClassName (string $ name ): string
197+ {
198+ return str_replace (
199+ ['$ ' , ' ' , '- ' , '. ' , '! ' ],
200+ ['' , '_ ' , '_ ' , '_ ' , '_ ' ],
201+ $ name ,
202+ );
203+ }
192204}
0 commit comments