Skip to content

Commit d4d251a

Browse files
committed
chore: attend phpmd observations
1 parent dce07e4 commit d4d251a

17 files changed

Lines changed: 443 additions & 262 deletions

docs/phpmd.md

Lines changed: 90 additions & 122 deletions
Large diffs are not rendered by default.

src/Command/BatchGeneratorCommand.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
use Html\Element\InlineElement;
1515
use Html\Element\VoidElement;
1616
use Html\Helper\Helper;
17+
use Html\Helper\YamlHelper;
1718
use Html\Trait\ClassResolverTrait;
1819
use Html\Trait\GeneratorResolverTrait;
1920
use ReflectionClass;
2021
use Symfony\Component\Console\Command\Command;
2122
use Symfony\Component\Console\Input\InputInterface;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324
use Symfony\Component\Console\Style\SymfonyStyle;
24-
use Symfony\Component\Yaml\Yaml;
2525

2626
class BatchGeneratorCommand extends Command
2727
{
@@ -30,11 +30,21 @@ class BatchGeneratorCommand extends Command
3030

3131
private const HTML_DEFINITION_PATH = __DIR__ . '/../Resources/specifications/html5-with-aria.yaml';
3232

33-
/** @phpstan-ignore-next-line */
33+
private YamlHelper $yaml;
34+
35+
/**
36+
* @phpstan-ignore-next-line
37+
*/
3438
private ?array $data = null;
3539

3640
private SymfonyStyle $io;
3741

42+
public function __construct(?YamlHelper $yaml = null)
43+
{
44+
parent::__construct();
45+
$this->yaml = $yaml ?? new YamlHelper();
46+
}
47+
3848
/**
3949
* @param string $generator The generator(s) to use
4050
* @param string $dest The destination directory to write to
@@ -159,7 +169,7 @@ private function loadHtmlDefinitions(?string $specificationPath): bool
159169
$this->io->error('Specification file not found at ' . $specificationPath);
160170
return false;
161171
}
162-
$this->data = Yaml::parseFile($specificationPath);
172+
$this->data = $this->yaml->parseFile($specificationPath);
163173
return true;
164174
}
165175

@@ -168,7 +178,7 @@ private function loadHtmlDefinitions(?string $specificationPath): bool
168178
return false;
169179
}
170180

171-
$this->data = Yaml::parseFile(self::HTML_DEFINITION_PATH);
181+
$this->data = $this->yaml->parseFile(self::HTML_DEFINITION_PATH);
172182
return true;
173183
}
174184

@@ -177,7 +187,7 @@ private function loadHtmlDefinitions(?string $specificationPath): bool
177187
*/
178188
private function getSafeComponentClassName(string $className): string
179189
{
180-
$reserved = Helper::getReservedWords();
190+
$reserved = (new Helper())->getReservedWords();
181191
if (in_array(strtolower($className), $reserved, true)) {
182192
return $className . 'Element';
183193
}

src/Command/CreateClassCommand.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
namespace Html\Command;
66

7-
use Html\Helper\Helper;
7+
use Html\Helper\CommentHelper;
88
use Html\Helper\NamingHelper;
99
use Html\Helper\TypeMapper;
10-
use Html\Helper\CommentHelper;
10+
use Html\Helper\YamlHelper;
1111
use Silly\Input\InputArgument;
1212
use Silly\Input\InputOption;
1313
use Symfony\Component\Console\Command\Command;
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use 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

src/Command/CreateEnumCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
namespace Html\Command;
1010

1111
use Exception;
12+
use Html\Helper\YamlHelper;
1213
use Silly\Input\InputArgument;
1314
use Silly\Input\InputOption;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Style\SymfonyStyle;
18-
use Symfony\Component\Yaml\Yaml;
1919

2020
/**
2121
* @usage create:enum
@@ -25,6 +25,14 @@ final class CreateEnumCommand extends Command
2525
{
2626
private array $data;
2727

28+
private YamlHelper $yaml;
29+
30+
public function __construct(?YamlHelper $yaml = null)
31+
{
32+
parent::__construct();
33+
$this->yaml = $yaml ?? new YamlHelper();
34+
}
35+
2836
public function __invoke(InputInterface $input, OutputInterface $output): int
2937
{
3038
$io = new SymfonyStyle($input, $output);
@@ -36,7 +44,7 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
3644
$io->error('The provided specification file does not exist.');
3745
return Command::FAILURE;
3846
}
39-
$this->data = Yaml::parseFile($specificationPath);
47+
$this->data = $this->yaml->parseFile($specificationPath);
4048
} else {
4149
// load default specs
4250
$htmlDefinitionPath = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Resources' . \DIRECTORY_SEPARATOR . 'specifications' . \DIRECTORY_SEPARATOR . 'html5-with-aria.yaml';
@@ -45,7 +53,7 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
4553
return Command::FAILURE;
4654
}
4755

48-
$this->data = Yaml::parseFile($htmlDefinitionPath);
56+
$this->data = $this->yaml->parseFile($htmlDefinitionPath);
4957
}
5058

5159

src/Command/CreateJsonCommand.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Html\Command;
66

7+
use Html\Helper\YamlHelper;
78
use Silly\Input\InputOption;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Input\InputInterface;
@@ -17,6 +18,14 @@
1718
*/
1819
final class CreateJsonCommand extends Command
1920
{
21+
private YamlHelper $yaml;
22+
23+
public function __construct(?YamlHelper $yaml = null)
24+
{
25+
parent::__construct();
26+
$this->yaml = $yaml ?? new YamlHelper();
27+
}
28+
2029
public function __invoke(InputInterface $input, OutputInterface $output): int
2130
{
2231
$io = new SymfonyStyle($input, $output);
@@ -33,18 +42,15 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
3342
$htmlDefinitionPathJson = \pathinfo(
3443
$specificationPath,
3544
\PATHINFO_DIRNAME
36-
) . \DIRECTORY_SEPARATOR . \pathinfo(
37-
$specificationPath,
38-
\PATHINFO_FILENAME
39-
) . '.json';
45+
) . \DIRECTORY_SEPARATOR . \pathinfo($specificationPath, \PATHINFO_FILENAME) . '.json';
4046
}
4147

4248
if (! file_exists($htmlDefinitionPath)) {
4349
$io->error("The HTML definition file does not exist at: {$htmlDefinitionPath}");
4450
return Command::FAILURE;
4551
}
4652

47-
$htmlDefinition = Yaml::parseFile($htmlDefinitionPath);
53+
$htmlDefinition = $this->yaml->parseFile($htmlDefinitionPath);
4854
\file_put_contents($htmlDefinitionPathJson, json_encode($htmlDefinition, \JSON_PRETTY_PRINT));
4955

5056
$io->success('JSON output generated successfully.');

0 commit comments

Comments
 (0)