Skip to content

Commit 88ebdc0

Browse files
Remove unused HasType trait from several classes. (#196)
1 parent 672ce3e commit 88ebdc0

File tree

11 files changed

+84
-56
lines changed

11 files changed

+84
-56
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
"require-dev": {
2323
"maglnet/composer-require-checker": "^4.3",
2424
"php-forge/support": "^1.0@dev",
25-
"phpunit/phpunit": "^10.0",
26-
"roave/infection-static-analysis-plugin": "^1.28",
25+
"phpunit/phpunit": "^10.5",
26+
"roave/infection-static-analysis-plugin": "^1.3",
2727
"symplify/easy-coding-standard": "^11.5",
28-
"vimeo/psalm": "^5.8"
28+
"vimeo/psalm": "^5.18"
2929
},
3030
"autoload": {
3131
"psr-4": {

src/A.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ final class A extends Base\AbstractElement
1919
use Attribute\Aria\HasRole;
2020
use Attribute\CanBeAutofocus;
2121
use Attribute\CanBeHidden;
22-
use Attribute\Input\HasType;
2322
use Attribute\Tag\HasDownload;
2423
use Attribute\Tag\HasHref;
2524
use Attribute\Tag\HasHreflang;

src/Attribute/Input/HasType.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@
99
*/
1010
trait HasType
1111
{
12+
protected string $type = '';
13+
1214
/**
1315
* Set the type of control to render.
1416
*
1517
* For example, to create a checkbox, a value of checkbox is used.
1618
*
1719
* If omitted (or an unknown value is specified), the input type text is used, creating a plaintext input field.
1820
*
19-
* @param mixed $value The type of control to render.
21+
* @param string $value The type of control to render.
2022
*
2123
* @return static A new instance of the current class with the specified type.
2224
*
2325
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-type
2426
*/
25-
public function type(mixed $value): static
27+
public function type(string $value): static
2628
{
2729
$new = clone $this;
28-
$new->attributes['type'] = $value;
30+
$new->type = $value;
2931

3032
return $new;
3133
}

src/Base/AbstractElement.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class AbstractElement extends Element
2525
use Attribute\HasTabindex;
2626
use Attribute\HasTitle;
2727
use Attribute\Input\HasName;
28+
use Attribute\Input\HasType;
2829

2930
protected array $attributes = [];
3031
protected string $tagName = '';
@@ -47,10 +48,8 @@ public function loadDefaultDefinitions(): array
4748
protected function run(): string
4849
{
4950
$attributes = $this->attributes;
50-
51-
if (array_key_exists('id', $attributes) === false) {
52-
$attributes['id'] = $this->id;
53-
}
51+
$attributes['id'] ??= $this->id;
52+
$attributes['type'] ??= $this->type;
5453

5554
$tokenValues = [
5655
'{prefix}' => $this->renderPrefixTag(),

src/Base/AbstractList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function run(): string
6565
->content($this->content)
6666
->id($this->id)
6767
->tagName($this->tagName)
68+
->type($this->type)
6869
->render();
6970
}
7071
}

src/Button.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ final class Button extends Element
4747
public function loadDefaultDefinitions(): array
4848
{
4949
return [
50+
'id()' => [$this->generateId('button-')],
5051
'template()' => ['{prefix}\n{tag}\n{suffix}'],
52+
'type()' => ['button'],
5153
];
5254
}
5355

@@ -81,14 +83,9 @@ protected function run(): string
8183
$this->validateTagName($this->tagName, 'a', 'button');
8284

8385
$attributes = $this->attributes;
84-
$type = $attributes['type'] ?? 'button';
85-
86-
unset($attributes['type']);
87-
88-
$id = $this->generateId("$type-");
8986

9087
if ($this->ariaDescribedBy === true) {
91-
$attributes['aria-describedby'] = "$id-help";
88+
$attributes['aria-describedby'] = "$this->id-help";
9289
}
9390

9491
if ($this->tagName === 'a' && $this->role === true) {
@@ -100,7 +97,7 @@ protected function run(): string
10097
Tag::widget()
10198
->attributes($attributes)
10299
->content($this->content)
103-
->id($id)
100+
->id($this->id)
104101
->prefix($this->prefix)
105102
->prefixContainer($this->prefixContainer)
106103
->prefixContainerAttributes($this->prefixContainerAttributes)
@@ -111,7 +108,7 @@ protected function run(): string
111108
->suffixContainerTag($this->suffixContainerTag)
112109
->tagName($this->tagName)
113110
->template($this->template)
114-
->type($type)
111+
->type($this->type)
115112
->render()
116113
);
117114
}

src/I.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ final class I extends Base\AbstractElement
1414
{
1515
use Attribute\CanBeAutofocus;
1616
use Attribute\CanBeHidden;
17-
use Attribute\Input\HasType;
1817
use Attribute\Tag\HasDownload;
1918
use Attribute\Tag\HasHref;
2019
use Attribute\Tag\HasHreflang;

src/Input/Base/AbstractButton.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public function loadDefaultDefinitions(): array
4545
{
4646
return [
4747
'container()' => [true],
48+
'id()' => [$this->generateId("$this->type-")],
4849
'template()' => ['{prefix}\n{label}\n{tag}\n{suffix}'],
50+
'type()' => ['button'],
4951
];
5052
}
5153

@@ -54,20 +56,17 @@ protected function run(): string
5456
$this->validateStringValue($this->getValue());
5557

5658
$attributes = $this->attributes;
57-
$type = $attributes['type'] ?? 'button';
58-
59-
$id = $this->generateId("$type-");
60-
$labelFor = $this->labelFor ?? $this->getId();
59+
$labelFor = $this->labelFor ?? $this->id;
6160

6261
if ($this->ariaDescribedBy === true) {
63-
$attributes['aria-describedby'] = "$id-help";
62+
$attributes['aria-describedby'] = "$this->id-help";
6463
}
6564

6665
return $this->renderContainerTag(
6766
null,
6867
Tag::widget()
6968
->attributes($attributes)
70-
->id($id)
69+
->id($this->id)
7170
->prefix($this->prefix)
7271
->prefixContainer($this->prefixContainer)
7372
->prefixContainerAttributes($this->prefixContainerAttributes)
@@ -79,7 +78,7 @@ protected function run(): string
7978
->tagName('input')
8079
->template($this->template)
8180
->tokenValue('{label}', $this->renderLabelTag($labelFor))
82-
->type($type)
81+
->type($this->type)
8382
->render()
8483
);
8584
}

src/Tag.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@
1919
final class Tag extends Base\AbstractElement
2020
{
2121
use Attribute\Custom\HasTagName;
22-
use Attribute\Input\HasType;
2322
use Attribute\Input\HasValue;
2423
}

tests/Button/RenderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public function testGenerateAriaDescribedBy(): void
167167
);
168168
}
169169

170+
public function testGenerateId(): void
171+
{
172+
$this->assertStringContainsString('<button id="button', Button::widget()->render());
173+
}
174+
170175
public function testId(): void
171176
{
172177
Assert::equalsWithoutLE(

tests/Tag/RenderTest.php

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,40 @@
1414
*/
1515
final class RenderTest extends TestCase
1616
{
17-
public function testElement(): void
17+
public function testAttributes(): void
1818
{
1919
Assert::equalsWithoutLE(
2020
<<<HTML
21-
<div>
22-
test element
21+
<div id="id" type="type">
22+
element
2323
</div>
2424
HTML,
25-
Tag::widget()->content('test element')->tagName('div')->render(),
25+
Tag::widget()->attributes(['id' => 'id', 'type' => 'type'])->content('element')->tagName('div')->render()
26+
);
27+
}
28+
29+
public function testId(): void
30+
{
31+
Assert::equalsWithoutLE(
32+
<<<HTML
33+
<div id="id">
34+
element
35+
</div>
36+
HTML,
37+
Tag::widget()->content('element')->id('id')->tagName('div')->render()
2638
);
2739
}
2840

2941
public function testPrefix(): void
3042
{
3143
Assert::equalsWithoutLE(
3244
<<<HTML
33-
<span>test prefix</span>
45+
<span>prefix</span>
3446
<div>
35-
test element
47+
element
3648
</div>
3749
HTML,
38-
Tag::widget()
39-
->content('test element')
40-
->prefix(Span::widget()->content('test prefix'))
41-
->tagName('div')
42-
->render(),
50+
Tag::widget()->content('element')->prefix(Span::widget()->content('prefix'))->tagName('div')->render()
4351
);
4452
}
4553

@@ -48,19 +56,31 @@ public function testPrefixContainer(): void
4856
Assert::equalsWithoutLE(
4957
<<<HTML
5058
<aside>
51-
<span>test prefix</span>
59+
<span>prefix</span>
5260
</aside>
5361
<div>
54-
test element
62+
element
5563
</div>
5664
HTML,
5765
Tag::widget()
58-
->content('test element')
59-
->prefix(Span::widget()->content('test prefix'))
66+
->content('element')
67+
->prefix(Span::widget()->content('prefix'))
6068
->prefixContainer(true)
6169
->prefixContainerTag('aside')
6270
->tagName('div')
63-
->render(),
71+
->render()
72+
);
73+
}
74+
75+
public function testRender(): void
76+
{
77+
Assert::equalsWithoutLE(
78+
<<<HTML
79+
<div>
80+
element
81+
</div>
82+
HTML,
83+
Tag::widget()->content('element')->tagName('div')->render()
6484
);
6585
}
6686

@@ -69,15 +89,11 @@ public function testSuffix(): void
6989
Assert::equalsWithoutLE(
7090
<<<HTML
7191
<div>
72-
test element
92+
element
7393
</div>
74-
<span>test suffix</span>
94+
<span>suffix</span>
7595
HTML,
76-
Tag::widget()
77-
->content('test element')
78-
->suffix(Span::widget()->content('test suffix'))
79-
->tagName('div')
80-
->render(),
96+
Tag::widget()->content('element')->suffix(Span::widget()->content('suffix'))->tagName('div')->render()
8197
);
8298
}
8399

@@ -86,19 +102,31 @@ public function testSuffixContainer(): void
86102
Assert::equalsWithoutLE(
87103
<<<HTML
88104
<div>
89-
test element
105+
element
90106
</div>
91107
<aside>
92-
<span>test prefix</span>
108+
<span>prefix</span>
93109
</aside>
94110
HTML,
95111
Tag::widget()
96-
->content('test element')
97-
->suffix(Span::widget()->content('test prefix'))
112+
->content('element')
113+
->suffix(Span::widget()->content('prefix'))
98114
->suffixContainer(true)
99115
->suffixContainerTag('aside')
100116
->tagName('div')
101-
->render(),
117+
->render()
118+
);
119+
}
120+
121+
public function testType(): void
122+
{
123+
Assert::equalsWithoutLE(
124+
<<<HTML
125+
<div type="type">
126+
element
127+
</div>
128+
HTML,
129+
Tag::widget()->content('element')->tagName('div')->type('type')->render()
102130
);
103131
}
104132
}

0 commit comments

Comments
 (0)