Skip to content

Commit 3e549c8

Browse files
committed
fix: make createFromString() and createFromFile() compatible with DOM\HTMLDocument
1 parent 62d24dd commit 3e549c8

6 files changed

Lines changed: 4389 additions & 828 deletions

File tree

clover.xml

Lines changed: 4340 additions & 772 deletions
Large diffs are not rendered by default.

docs/phpmd.md

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,3 @@ layout: home
44
# PHP Mess Detector Report
55

66
[[toc]]
7-
## Unused Code
8-
9-
| Priority | File | Line | Rule | Message |
10-
| -------- | ------ | ------------ | ----- | ------- |
11-
| <span class="prio3">3</span> | src/Command/BatchGeneratorCommand.php | 58 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$specification`. |
12-
| <span class="prio3">3</span> | src/Command/BatchGeneratorCommand.php | 95 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$elementShortName`. |
13-
| <span class="prio3">3</span> | src/Command/CreateClassCommand.php | 388 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$type`. |
14-
| <span class="prio3">3</span> | src/Command/GenerateComposedCommand.php | 57 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$specification`. |
15-
| <span class="prio3">3</span> | src/Command/MergeSpecifications.php | 26 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$input`. |
16-
| <span class="prio3">3</span> | src/Command/MergeSpecifications.php | 34 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$props`. |
17-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 37 | [UnusedPrivateField](https://phpmd.org/rules/unusedcode.html#unusedprivatefield) | Avoid unused private fields such as `$data`. |
18-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 216 | [UnusedPrivateMethod](https://phpmd.org/rules/unusedcode.html#unusedprivatemethod) | Avoid unused private methods such as `parseFile`. |
19-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 216 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$generator`. |
20-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 216 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$sourceFile`. |
21-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 216 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$dest`. |
22-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 216 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$io`. |
23-
| <span class="prio3">3</span> | src/Command/WatchCommand.php | 224 | [UnusedPrivateMethod](https://phpmd.org/rules/unusedcode.html#unusedprivatemethod) | Avoid unused private methods such as `formatHtml`. |
24-
| <span class="prio3">3</span> | src/Service/ComponentBuilder.php | 50 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$childElement`. |
25-
| <span class="prio3">3</span> | src/TemplateGenerator/BladeGenerator.php | 275 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$parentRef`. |
26-
| <span class="prio3">3</span> | src/TemplateGenerator/NextJSGenerator.php | 204 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$level`. |
27-
| <span class="prio3">3</span> | src/TemplateGenerator/NextJSGenerator.php | 213 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$propsInterface`. |
28-
| <span class="prio3">3</span> | src/TemplateGenerator/NextJSGenerator.php | 298 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$value`. |
29-
| <span class="prio3">3</span> | src/TemplateGenerator/StorybookJSGenerator.php | 565 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$choices`. |
30-
| <span class="prio3">3</span> | src/TemplateGenerator/StorybookJSGenerator.php | 575 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$type`. |
31-
| <span class="prio3">3</span> | src/TemplateGenerator/StorybookJSGenerator.php | 626 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$props`. |
32-
| <span class="prio3">3</span> | src/TemplateGenerator/StorybookJSGenerator.php | 629 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$renderAssignments`. |
33-
| <span class="prio3">3</span> | src/TemplateGenerator/StorybookJSGenerator.php | 743 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$level`. |
34-
| <span class="prio3">3</span> | src/TemplateGenerator/StorybookJSGenerator.php | 843 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$parentRef`. |
35-
| <span class="prio3">3</span> | src/TemplateGenerator/TwigGenerator.php | 257 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$key`. |
36-
| <span class="prio3">3</span> | src/TemplateGenerator/TwigGenerator.php | 450 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$parentRef`. |
37-
| <span class="prio3">3</span> | src/TemplateGenerator/TwigGenerator.php | 626 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$document`. |
38-
| <span class="prio3">3</span> | src/TemplateGenerator/TwigGenerator.php | 657 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$templateName`. |
39-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 155 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$level`. |
40-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 158 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$enums`. |
41-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 172 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$example`. |
42-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 200 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$setterMethod`. |
43-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 268 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$getter`. |
44-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 395 | [UnusedPrivateMethod](https://phpmd.org/rules/unusedcode.html#unusedprivatemethod) | Avoid unused private methods such as `getGlobalAttributeTraits`. |
45-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 397 | [UnusedLocalVariable](https://phpmd.org/rules/unusedcode.html#unusedlocalvariable) | Avoid unused local variables such as `$traits`. |
46-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 537 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$enumImports`. |
47-
| <span class="prio3">3</span> | src/TemplateGenerator/TypeScriptGenerator.php | 748 | [UnusedFormalParameter](https://phpmd.org/rules/unusedcode.html#unusedformalparameter) | Avoid unused parameters such as `$ref`. |
48-
49-
Issues detected: 37

src/Delegator/HTMLDocumentDelegator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,19 @@ public static function getInstance(HTMLDocument $document): self
9797
return self::$instances[$key] ?? new self($document);
9898
}
9999

100-
public static function createEmpty(): self
100+
public static function createEmpty(string $encoding = 'UTF-8'): self
101101
{
102-
return new self((new DomHelper())->createEmpty());
102+
return new self((new DomHelper())->createEmpty($encoding));
103103
}
104104

105-
public static function createFromString(string $html): self
105+
public static function createFromString(string $html, int $options = 0, ?string $overrideEncoding = null): self
106106
{
107-
return new self((new DomHelper())->createFromString($html));
107+
return new self((new DomHelper())->createFromString($html, $options, $overrideEncoding));
108108
}
109109

110-
public static function createFromFile(string $path): self
110+
public static function createFromFile(string $path, int $options = 0, ?string $overrideEncoding = null): self
111111
{
112-
return new self((new DomHelper())->createFromFile($path));
112+
return new self((new DomHelper())->createFromFile($path, $options, $overrideEncoding));
113113
}
114114

115115
public function createElement(string $qualifiedName, ?string $nodeValue = null): HTMLElementDelegator

src/Helper/DomHelper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
final class DomHelper
1010
{
11-
public function createEmpty(): HTMLDocument
11+
public function createEmpty(string $encoding = 'UTF-8'): HTMLDocument
1212
{
13-
return HTMLDocument::createEmpty();
13+
return HTMLDocument::createEmpty($encoding);
1414
}
1515

16-
public function createFromString(string $html): HTMLDocument
16+
public function createFromString(string $html, int $options = 0, ?string $overrideEncoding = null): HTMLDocument
1717
{
18-
return HTMLDocument::createFromString($html);
18+
return HTMLDocument::createFromString($html, $options, $overrideEncoding);
1919
}
2020

21-
public function createFromFile(string $path): HTMLDocument
21+
public function createFromFile(string $path, int $options = 0, ?string $overrideEncoding = null): HTMLDocument
2222
{
23-
return HTMLDocument::createFromFile($path);
23+
return HTMLDocument::createFromFile($path, $options, $overrideEncoding);
2424
}
2525
}

src/Interface/HTMLDocumentDelegatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __set($name, $value);
2424

2525
public function __toString(): string;
2626

27-
public static function createEmpty(): self;
27+
public static function createEmpty(string $encoding = 'UTF-8'): self;
2828

2929
public function createElement(string $qualifiedName): HTMLElementDelegator;
3030

tests/Delegator/HTMLDocumentDelegatorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@
264264
expect(HTMLDocumentDelegator::createEmpty())->toBeInstanceOf(HTMLDocumentDelegator::class);
265265
});
266266

267+
test('create empty accepts encoding', function () {
268+
$delegator = HTMLDocumentDelegator::createEmpty('ISO-8859-1');
269+
expect($delegator)
270+
->toBeInstanceOf(HTMLDocumentDelegator::class);
271+
expect($delegator->delegated->characterSet)
272+
->toBe('ISO-8859-1');
273+
});
274+
267275
test('create from string', function () {
268276
$html = '<!DOCTYPE html><html><head><title>Test</title></head><body></body></html>';
269277
$delegator = HTMLDocumentDelegator::createFromString($html);
@@ -273,6 +281,19 @@
273281
->toEqual($html);
274282
});
275283

284+
test('create from string accepts options and overrideEncoding', function () {
285+
$html = '<div>Hi</div>';
286+
$options = \LIBXML_NOERROR | \LIBXML_HTML_NOIMPLIED;
287+
288+
$expected = HTMLDocument::createFromString($html, $options, 'UTF-8')->saveHTML();
289+
$delegator = HTMLDocumentDelegator::createFromString($html, $options, 'UTF-8');
290+
291+
expect((string) $delegator)
292+
->toEqual($expected);
293+
expect((string) $delegator)
294+
->toEqual('<div>Hi</div>');
295+
});
296+
276297
test('create from file', function () {
277298
file_put_contents('file.html', '<!doctype html><html><body>Hello World</body></html>');
278299
expect(file_exists('file.html'))
@@ -283,6 +304,21 @@
283304
unlink('file.html');
284305
});
285306

307+
test('create from file accepts options and overrideEncoding', function () {
308+
$path = 'file.html';
309+
file_put_contents($path, '<div>Hi</div>');
310+
$options = \LIBXML_NOERROR | \LIBXML_HTML_NOIMPLIED;
311+
312+
$expected = HTMLDocument::createFromFile($path, $options, 'UTF-8')->saveHTML();
313+
$delegator = HTMLDocumentDelegator::createFromFile($path, $options, 'UTF-8');
314+
315+
unlink($path);
316+
expect((string) $delegator)
317+
->toEqual($expected);
318+
expect((string) $delegator)
319+
->toEqual('<div>Hi</div>');
320+
});
321+
286322
test('get elements by tag name', function () {
287323
$html = '<!DOCTYPE html><html><head><title>Test</title></head><body><div><p>Test</p></div></body></html>';
288324
$delegator = HTMLDocumentDelegator::createFromString($html);

0 commit comments

Comments
 (0)