Skip to content

Commit 17711e1

Browse files
committed
fix: Enum constant names with spaces fixed
1 parent 00097c9 commit 17711e1

138 files changed

Lines changed: 262 additions & 226 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/phpmd.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ Issues detected: 10
2525
| Priority | File | Line | Rule | Message |
2626
| -------- | ------ | ------------ | ----- | ------- |
2727
| <span class="prio3">3</span> | src/Command/CreateClassCommand.php | 22 | [ExcessiveClassComplexity](https://phpmd.org/rules/codesize.html#excessiveclasscomplexity) | The class CreateClassCommand has an overall complexity of 81 which is very high. The configured complexity threshold is 50. |
28-
| <span class="prio3">3</span> | src/Command/CreateEnumCommand.php | 28 | [CyclomaticComplexity](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) | The method __invoke() has a Cyclomatic Complexity of 23. The configured cyclomatic complexity threshold is 10. |
29-
| <span class="prio3">3</span> | src/Command/CreateEnumCommand.php | 28 | [NPathComplexity](https://phpmd.org/rules/codesize.html#npathcomplexity) | The method __invoke() has an NPath complexity of 7992. The configured NPath complexity threshold is 200. |
30-
| <span class="prio3">3</span> | src/Command/CreateEnumCommand.php | 28 | [ExcessiveMethodLength](https://phpmd.org/rules/codesize.html#excessivemethodlength) | The method __invoke() has 128 lines of code. Current threshold is set to 100. Avoid really long methods. |
28+
| <span class="prio3">3</span> | src/Command/CreateEnumCommand.php | 28 | [CyclomaticComplexity](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) | The method __invoke() has a Cyclomatic Complexity of 16. The configured cyclomatic complexity threshold is 10. |
29+
| <span class="prio3">3</span> | src/Command/CreateEnumCommand.php | 28 | [NPathComplexity](https://phpmd.org/rules/codesize.html#npathcomplexity) | The method __invoke() has an NPath complexity of 472. The configured NPath complexity threshold is 200. |
3130
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 51 | [CyclomaticComplexity](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) | The method __invoke() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10. |
3231
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 51 | [NPathComplexity](https://phpmd.org/rules/codesize.html#npathcomplexity) | The method __invoke() has an NPath complexity of 768. The configured NPath complexity threshold is 200. |
3332
| <span class="prio3">3</span> | src/Delegator/HTMLElementDelegator.php | 35 | [ExcessiveClassComplexity](https://phpmd.org/rules/codesize.html#excessiveclasscomplexity) | The class HTMLElementDelegator has an overall complexity of 51 which is very high. The configured complexity threshold is 50. |
@@ -42,7 +41,7 @@ Issues detected: 10
4241
| <span class="prio3">3</span> | src/Trait/ClassResolverTrait.php | 44 | [CyclomaticComplexity](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) | The method getElementByQualifiedName() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10. |
4342
| <span class="prio3">3</span> | src/Trait/GlobalAttributesTrait.php | 19 | [ExcessivePublicCount](https://phpmd.org/rules/codesize.html#excessivepubliccount) | The trait GlobalAttributesTrait has 48 public methods and attributes. Consider reducing the number of public items to less than 45. |
4443

45-
Issues detected: 17
44+
Issues detected: 16
4645
## Clean Code
4746

4847
| Priority | File | Line | Rule | Message |
@@ -145,4 +144,4 @@ Issues detected: 80
145144

146145
Issues detected: 9
147146

148-
Sun Oct 19 08:55:44 PM CEST 2025
147+
Sun Oct 19 10:29:32 PM CEST 2025

src/Command/CreateClassCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private function generateEnumMethod(
280280
if (! isset($details['elements']) || ! is_array($details['elements'])) {
281281
$elementsWithAttribute = [];
282282
foreach ($this->data as $tmpEl => $tmpData) {
283-
if (isset($tmpData['attributes']) && isset($tmpData['attributes']['data-theme'])) {
283+
if (isset($tmpData['attributes']) && isset($tmpData['attributes']['data-theme'])) { // @todo dont like data-theme in here
284284
$elementsWithAttribute[] = $tmpEl;
285285
}
286286
}

src/Command/CreateEnumCommand.php

Lines changed: 48 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -50,105 +50,61 @@ public function __invoke(InputInterface $input, OutputInterface $output): int
5050

5151

5252
// Aggregate all data-* attributes with type enum and identical choices
53-
$dataEnums = [];
54-
$dataDescriptions = [];
55-
foreach ($this->data as $element => $details) {
56-
if (isset($details['attributes'])) {
57-
foreach ($details['attributes'] as $attr => $attrDetails) {
58-
if (str_starts_with($attr, 'data-') && isset($attrDetails['type']) && str_contains(
59-
$attrDetails['type'],
60-
'enum'
61-
) && isset($attrDetails['choices'])) {
62-
$key = $attr . ':' . implode('|', $attrDetails['choices']);
63-
$dataEnums[$attr][$key] = $attrDetails['choices'];
64-
if (isset($attrDetails['description'])) {
65-
$dataDescriptions[$attr] = $attrDetails['description'];
66-
}
67-
}
68-
}
69-
}
70-
}
71-
72-
// Only generate one enum per data-* attribute with identical choices
73-
foreach ($dataEnums as $attr => $choicesSets) {
74-
// Use the first set of choices (all should be identical for this attr)
75-
$choices = reset($choicesSets);
76-
$cases = '';
77-
foreach ($choices as $option) {
78-
$caseName = $this->getCaseName($option);
79-
$cases .= sprintf(" case %s = '%s';", $caseName, $option) . \PHP_EOL;
80-
}
81-
$className = $this->getClassName(
82-
ucfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $attr)))) . 'Enum'
83-
);
84-
$parameters = [
85-
'namespace' => 'Html\Enum',
86-
'class_name' => $className,
87-
'cases' => rtrim($cases),
88-
'description' => $dataDescriptions[$attr] ?? '',
89-
'element_name' => $attr,
90-
'defaultValue' => '',
91-
'defaultCase' => '',
92-
'generatedAt' => date('Y-m-d H:i:s'),
93-
];
94-
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Enum' . \DIRECTORY_SEPARATOR . "{$className}.php";
95-
$templatePath = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Resources' . \DIRECTORY_SEPARATOR . 'templates' . \DIRECTORY_SEPARATOR . 'Enum.tpl.php';
96-
$this->createEnumFile($templatePath, $parameters, $path);
97-
$io->success("Enumeration class for '{$attr}' created successfully.");
98-
}
9953

100-
101-
// Continue with normal enum generation for other attributes (excluding data-*)
54+
// Find all enum attributes (type 'enum' or union containing 'enum')
10255
$enumAttributes = $this->findEnumAttributes();
10356

10457
$generatedAt = \date('Y-m-d H:i:s');
10558
foreach ($enumAttributes as $enumAttribute) {
10659
foreach ($enumAttribute as $element => $attributes) {
107-
// Skip data-* attributes, already handled
108-
if (str_starts_with($element, 'data-')) {
109-
continue;
110-
}
11160
$io->info("Creating enumeration class for '{$element}'");
11261

11362
if (! isset($attributes['choices'])) {
11463
throw new Exception('An enum attribute must have choices. Add choices or change type to string.');
11564
}
116-
if (! isset($attributes['elements'])) {
117-
throw new Exception(
118-
'An enum attribute must have elements. Add elements or change type to string.'
119-
);
65+
if (! isset($attributes['elements']) || ! is_array($attributes['elements'])) {
66+
$elementsWithAttribute = [];
67+
foreach ($this->data as $tmpEl => $tmpData) {
68+
if (isset($tmpData['attributes']) && isset($tmpData['attributes'][$element])) { // @todo dont like data-theme in here
69+
$elementsWithAttribute[] = $tmpEl;
70+
}
71+
}
72+
$attributes['elements'] = $elementsWithAttribute;
12073
}
121-
$cases = '';
122-
$className = ucfirst($element);
74+
// throw new Exception(
75+
// 'An enum attribute must have elements. Add elements or change type to string.'
76+
// );
77+
}
78+
$cases = '';
79+
$className = ucfirst($element);
12380

124-
if ($this->manyElementsHaveAttribute($element) && count($attributes['elements']) === 1) {
125-
$className .= ucfirst($attributes['elements'][0]);
126-
}
81+
if ($this->manyElementsHaveAttribute($element) && count($attributes['elements']) === 1) {
82+
$className .= ucfirst($attributes['elements'][0]);
83+
}
12784

128-
$defaultCase = $this->getCaseName((string) $attributes['defaultValue'] ?? '');
129-
foreach ($attributes['choices'] as $option) {
130-
$caseName = $this->getCaseName($option);
131-
$default = $caseName === $defaultCase ? ' // default' : '';
132-
$cases .= sprintf(" case %s = '%s';%s", $caseName, $option, $default) . \PHP_EOL;
133-
}
85+
$defaultCase = $this->getCaseName((string) $attributes['defaultValue'] ?? '');
86+
foreach ($attributes['choices'] as $option) {
87+
$caseName = $this->getCaseName($option);
88+
$default = $caseName === $defaultCase ? ' // default' : '';
89+
$cases .= sprintf(" case %s = '%s';%s", $caseName, $option, $default) . \PHP_EOL;
90+
}
13491

135-
$className = $this->getClassName($className . 'Enum');
136-
$parameters = [
137-
'namespace' => 'Html\Enum',
138-
'class_name' => $className, // fixed missing parenthesis
139-
'cases' => rtrim($cases),
140-
'description' => $attributes['description'] ?? '', // fixed double dollar sign
141-
'element_name' => $element,
142-
'defaultValue' => $attributes['defaultValue'] ?? '',
143-
'defaultCase' => $defaultCase,
144-
'generatedAt' => $generatedAt,
145-
];
92+
$className = $this->getClassName($className . 'Enum');
93+
$parameters = [
94+
'namespace' => 'Html\Enum',
95+
'class_name' => $className,
96+
'cases' => rtrim($cases),
97+
'description' => $attributes['description'] ?? '',
98+
'element_name' => $element,
99+
'defaultValue' => $attributes['defaultValue'] ?? '',
100+
'defaultCase' => $defaultCase,
101+
'generatedAt' => $generatedAt,
102+
];
146103

147-
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Enum' . \DIRECTORY_SEPARATOR . "{$className}.php"; // corrected variable syntax
148-
$templatePath = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Resources' . \DIRECTORY_SEPARATOR . 'templates' . \DIRECTORY_SEPARATOR . 'Enum.tpl.php';
149-
$this->createEnumFile($templatePath, $parameters, $path);
150-
$io->success("Enumeration class for '{$element}' created successfully.");
151-
}
104+
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Enum' . \DIRECTORY_SEPARATOR . "{$className}.php";
105+
$templatePath = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'Resources' . \DIRECTORY_SEPARATOR . 'templates' . \DIRECTORY_SEPARATOR . 'Enum.tpl.php';
106+
$this->createEnumFile($templatePath, $parameters, $path);
107+
$io->success("Enumeration class for '{$element}' created successfully.");
152108
}
153109

154110
return Command::SUCCESS;
@@ -185,8 +141,8 @@ private function getCaseName(string $option): string
185141
$option = $ret;
186142
}
187143

188-
// clenaup
189-
$option = str_replace(['-', '/'], '_', strtoupper($option));
144+
// cleanup: replace spaces, dashes, slashes with underscores, uppercase
145+
$option = str_replace([' ', '-', '/'], '_', strtoupper($option));
190146
$option = trim($option, '_');
191147

192148
return $option;
@@ -216,9 +172,13 @@ private function findEnumAttributes(): array
216172
foreach ($this->data as $details) {
217173
if (isset($details['attributes'])) {
218174
foreach ($details['attributes'] as $attribute => $attributeDetails) {
219-
if (isset($attributeDetails['type']) && $attributeDetails['type'] === 'enum') {
220-
$enumAttributes[$i][$attribute] = $attributeDetails;
221-
$i++;
175+
if (isset($attributeDetails['type'])) {
176+
$type = $attributeDetails['type'];
177+
// Support 'enum', 'enum|string', 'enum|boolean', etc.
178+
if ($type === 'enum' || (is_string($type) && preg_match('/(^|\|)enum($|\|)/', $type))) {
179+
$enumAttributes[$i][$attribute] = $attributeDetails;
180+
$i++;
181+
}
222182
}
223183
}
224184
}

src/Element/Block/Article.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Article - The article element represents a self-contained composition in a document, page, application, or site, which is intended to be independently distributable or reusable.
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/article

src/Element/Block/Aside.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Aside - The aside element represents a section of a page that consists of content that is tangentially related to the content around the aside element, and which could be considered separate from that content.
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside

src/Element/Block/Audio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Audio - The audio element is used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the source element.
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio

src/Element/Block/Blockquote.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Blockquote - The blockquote element represents a section that is quoted from another source. Content inside a blockquote must be quoted from another source, whose address, if it has one, may be cited in the cite attribute.
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/blockquote

src/Element/Block/Body.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Body - The body element represents the content of an HTML document. All the contents such as text, images, headings, links, tables, etc. are placed between the body tags.
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body

src/Element/Block/Canvas.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Canvas - The canvas element is used to draw graphics, on the fly, via scripting (usually JavaScript).
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas

src/Element/Block/Caption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Caption - The caption element represents the title of the table that is its parent, if it has a parent and that is a table element.
77
*
8-
* @generated 2025-10-19 18:53:35
8+
* @generated 2025-10-19 20:20:48
99
* @subpackage Html\Element\Block
1010
* @link https://vardumper.github.io/extended-htmldocument/
1111
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/caption

0 commit comments

Comments
 (0)