44
55namespace Html \Command ;
66
7- use Html \Helper \Helper ;
7+ use Html \Helper \CommentHelper ;
88use Html \Helper \NamingHelper ;
99use Html \Helper \TypeMapper ;
10- use Html \Helper \CommentHelper ;
10+ use Html \Helper \YamlHelper ;
1111use Silly \Input \InputArgument ;
1212use Silly \Input \InputOption ;
1313use Symfony \Component \Console \Command \Command ;
1414use Symfony \Component \Console \Input \InputInterface ;
1515use Symfony \Component \Console \Output \OutputInterface ;
1616use Symfony \Component \Console \Style \SymfonyStyle ;
17- use Symfony \Component \Yaml \Yaml ;
1817
1918/**
2019 * @usage make:classes [element]
@@ -30,12 +29,20 @@ final class CreateClassCommand extends Command
3029
3130 private const OUTPUT_PATH = __DIR__ . '/../ ' ;
3231
32+ private YamlHelper $ yaml ;
33+
3334 private array $ uses = [];
3435
3536 private array $ data = [];
3637
3738 private SymfonyStyle $ io ;
3839
40+ public function __construct (?YamlHelper $ yaml = null )
41+ {
42+ parent ::__construct ();
43+ $ this ->yaml = $ yaml ?? new YamlHelper ();
44+ }
45+
3946 public function __invoke ($ element , InputInterface $ input , OutputInterface $ output ): int
4047 {
4148 $ this ->io = new SymfonyStyle ($ input , $ output );
@@ -100,7 +107,7 @@ private function loadHtmlDefinitions(?string $specificationPath): bool
100107 $ this ->io ->error ('Specification file not found at ' . $ specificationPath );
101108 return false ;
102109 }
103- $ this ->data = Yaml:: parseFile ($ specificationPath );
110+ $ this ->data = $ this -> yaml -> parseFile ($ specificationPath );
104111 return true ;
105112 }
106113
@@ -109,7 +116,7 @@ private function loadHtmlDefinitions(?string $specificationPath): bool
109116 return false ;
110117 }
111118
112- $ this ->data = Yaml:: parseFile (self ::HTML_DEFINITION_PATH );
119+ $ this ->data = $ this -> yaml -> parseFile (self ::HTML_DEFINITION_PATH );
113120 return true ;
114121 }
115122
@@ -333,15 +340,16 @@ private function generateEnumMethod(
333340
334341 // Check if element-specific enum file exists
335342 $ elementSpecificPath = __DIR__ . '/../Enum/ ' . $ elementSpecificEnumName . '.php ' ;
343+ $ enumName = $ genericEnumName ;
336344 if (file_exists ($ elementSpecificPath )) {
337345 $ enumName = $ elementSpecificEnumName ;
338- } else {
339- // Fall back to generic enum or old logic for single-element attributes
340- if ( $ this -> manyElementsHaveAttribute ( $ attribute ) && count ( $ details [ ' elements ' ]) === 1 ) {
341- $ enumName = $ kebapCase . ucfirst ( $ element ) . ' Enum ' ;
342- } else {
343- $ enumName = $ genericEnumName;
344- }
346+ }
347+
348+ // Fall back to element-prefixed enum when many elements have the attribute but only one element in details
349+ if ( $ this -> manyElementsHaveAttribute ( $ attribute ) && count (
350+ $ details [ ' elements ' ]
351+ ) === 1 && $ enumName === $ genericEnumName) {
352+ $ enumName = $ kebapCase . ucfirst ( $ element ) . ' Enum ' ;
345353 }
346354
347355 $ isUnionType = str_replace ('enum ' , '' , $ type ) !== '' ;
@@ -495,15 +503,16 @@ private function processEnumAttribute(string $attribute, array $details, string
495503
496504 // Check if element-specific enum file exists
497505 $ elementSpecificPath = __DIR__ . '/../Enum/ ' . $ elementSpecificEnumName . '.php ' ;
506+ $ enumName = $ genericEnumName ;
498507 if (file_exists ($ elementSpecificPath )) {
499508 $ enumName = $ elementSpecificEnumName ;
500- } else {
501- // Fall back to generic enum or old logic for single-element attributes
502- if ( $ this -> manyElementsHaveAttribute ( $ attribute ) && count ( $ details [ ' elements ' ]) === 1 ) {
503- $ enumName = $ kebapCase . ucfirst ( $ element ) . ' Enum ' ;
504- } else {
505- $ enumName = $ genericEnumName;
506- }
509+ }
510+
511+ // Fall back to element-prefixed enum when many elements have the attribute but only one element in details
512+ if ( $ this -> manyElementsHaveAttribute ( $ attribute ) && count (
513+ $ details [ ' elements ' ]
514+ ) === 1 && $ enumName === $ genericEnumName) {
515+ $ enumName = $ kebapCase . ucfirst ( $ element ) . ' Enum ' ;
507516 }
508517
509518 $ this ->uses [] = sprintf ("Html\Enum\%s " , $ enumName );
@@ -562,13 +571,14 @@ private function getUseStatements(array $children, array $parents, string $ignor
562571 if (count ($ classes ) === 1 ) {
563572 // Single use statement
564573 $ useStatements .= sprintf ("use %s \\%s; \n" , $ namespace , $ classes [0 ]);
565- } else {
566- // Grouped use statement
567- $ useStatements .= sprintf ("use %s \\{ \n" , $ namespace );
568- foreach ($ classes as $ i => $ class ) {
569- $ useStatements .= sprintf (' %s ' , $ class );
570- $ useStatements .= ($ i < count ($ classes ) - 1 ) ? ", \n" : ", \n}; \n" ;
571- }
574+ continue ;
575+ }
576+
577+ // Grouped use statement
578+ $ useStatements .= sprintf ("use %s \\{ \n" , $ namespace );
579+ foreach ($ classes as $ i => $ class ) {
580+ $ useStatements .= sprintf (' %s ' , $ class );
581+ $ useStatements .= ($ i < count ($ classes ) - 1 ) ? ", \n" : ", \n}; \n" ;
572582 }
573583 }
574584
@@ -591,27 +601,27 @@ private function filterAndSortUses(array $uses, string $ignoreClass): array
591601 // String manipulation utilities
592602 private function toVariableName (string $ string ): string
593603 {
594- return NamingHelper:: toVariableName ($ string );
604+ return ( new NamingHelper ())-> toVariableName ($ string );
595605 }
596606
597607 private function toKebapCase (string $ string ): string
598608 {
599- return NamingHelper:: toKebapCase ($ string );
609+ return ( new NamingHelper ())-> toKebapCase ($ string );
600610 }
601611
602612 private function getClassName (string $ classname ): string
603613 {
604- return NamingHelper:: getClassName ($ classname );
614+ return ( new NamingHelper ())-> getClassName ($ classname );
605615 }
606616
607617 private function getAttributeComment (array $ details ): string
608618 {
609- return CommentHelper:: getAttributeComment ($ details );
619+ return ( new CommentHelper ())-> getAttributeComment ($ details );
610620 }
611621
612622 private function mapToPhpType (string $ string ): string
613623 {
614- return TypeMapper:: mapToPhpType ($ string );
624+ return ( new TypeMapper ())-> mapToPhpType ($ string );
615625 }
616626
617627 // Method signature templates
0 commit comments