Skip to content

Commit

Permalink
Remove unused HasType trait from several classes. (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 2, 2024
1 parent 672ce3e commit 88ebdc0
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 56 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.3",
"php-forge/support": "^1.0@dev",
"phpunit/phpunit": "^10.0",
"roave/infection-static-analysis-plugin": "^1.28",
"phpunit/phpunit": "^10.5",
"roave/infection-static-analysis-plugin": "^1.3",
"symplify/easy-coding-standard": "^11.5",
"vimeo/psalm": "^5.8"
"vimeo/psalm": "^5.18"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ final class A extends Base\AbstractElement
use Attribute\Aria\HasRole;
use Attribute\CanBeAutofocus;
use Attribute\CanBeHidden;
use Attribute\Input\HasType;
use Attribute\Tag\HasDownload;
use Attribute\Tag\HasHref;
use Attribute\Tag\HasHreflang;
Expand Down
8 changes: 5 additions & 3 deletions src/Attribute/Input/HasType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@
*/
trait HasType
{
protected string $type = '';

/**
* Set the type of control to render.
*
* For example, to create a checkbox, a value of checkbox is used.
*
* If omitted (or an unknown value is specified), the input type text is used, creating a plaintext input field.
*
* @param mixed $value The type of control to render.
* @param string $value The type of control to render.
*
* @return static A new instance of the current class with the specified type.
*
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-type
*/
public function type(mixed $value): static
public function type(string $value): static
{
$new = clone $this;
$new->attributes['type'] = $value;
$new->type = $value;

return $new;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Base/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class AbstractElement extends Element
use Attribute\HasTabindex;
use Attribute\HasTitle;
use Attribute\Input\HasName;
use Attribute\Input\HasType;

protected array $attributes = [];
protected string $tagName = '';
Expand All @@ -47,10 +48,8 @@ public function loadDefaultDefinitions(): array
protected function run(): string
{
$attributes = $this->attributes;

if (array_key_exists('id', $attributes) === false) {
$attributes['id'] = $this->id;
}
$attributes['id'] ??= $this->id;
$attributes['type'] ??= $this->type;

$tokenValues = [
'{prefix}' => $this->renderPrefixTag(),
Expand Down
1 change: 1 addition & 0 deletions src/Base/AbstractList.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function run(): string
->content($this->content)
->id($this->id)
->tagName($this->tagName)
->type($this->type)
->render();
}
}
13 changes: 5 additions & 8 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ final class Button extends Element
public function loadDefaultDefinitions(): array
{
return [
'id()' => [$this->generateId('button-')],
'template()' => ['{prefix}\n{tag}\n{suffix}'],
'type()' => ['button'],
];
}

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

$attributes = $this->attributes;
$type = $attributes['type'] ?? 'button';

unset($attributes['type']);

$id = $this->generateId("$type-");

if ($this->ariaDescribedBy === true) {
$attributes['aria-describedby'] = "$id-help";
$attributes['aria-describedby'] = "$this->id-help";
}

if ($this->tagName === 'a' && $this->role === true) {
Expand All @@ -100,7 +97,7 @@ protected function run(): string
Tag::widget()
->attributes($attributes)
->content($this->content)
->id($id)
->id($this->id)
->prefix($this->prefix)
->prefixContainer($this->prefixContainer)
->prefixContainerAttributes($this->prefixContainerAttributes)
Expand All @@ -111,7 +108,7 @@ protected function run(): string
->suffixContainerTag($this->suffixContainerTag)
->tagName($this->tagName)
->template($this->template)
->type($type)
->type($this->type)
->render()
);
}
Expand Down
1 change: 0 additions & 1 deletion src/I.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ final class I extends Base\AbstractElement
{
use Attribute\CanBeAutofocus;
use Attribute\CanBeHidden;
use Attribute\Input\HasType;
use Attribute\Tag\HasDownload;
use Attribute\Tag\HasHref;
use Attribute\Tag\HasHreflang;
Expand Down
13 changes: 6 additions & 7 deletions src/Input/Base/AbstractButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function loadDefaultDefinitions(): array
{
return [
'container()' => [true],
'id()' => [$this->generateId("$this->type-")],
'template()' => ['{prefix}\n{label}\n{tag}\n{suffix}'],
'type()' => ['button'],
];
}

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

$attributes = $this->attributes;
$type = $attributes['type'] ?? 'button';

$id = $this->generateId("$type-");
$labelFor = $this->labelFor ?? $this->getId();
$labelFor = $this->labelFor ?? $this->id;

if ($this->ariaDescribedBy === true) {
$attributes['aria-describedby'] = "$id-help";
$attributes['aria-describedby'] = "$this->id-help";
}

return $this->renderContainerTag(
null,
Tag::widget()
->attributes($attributes)
->id($id)
->id($this->id)
->prefix($this->prefix)
->prefixContainer($this->prefixContainer)
->prefixContainerAttributes($this->prefixContainerAttributes)
Expand All @@ -79,7 +78,7 @@ protected function run(): string
->tagName('input')
->template($this->template)
->tokenValue('{label}', $this->renderLabelTag($labelFor))
->type($type)
->type($this->type)
->render()
);
}
Expand Down
1 change: 0 additions & 1 deletion src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
final class Tag extends Base\AbstractElement
{
use Attribute\Custom\HasTagName;
use Attribute\Input\HasType;
use Attribute\Input\HasValue;
}
5 changes: 5 additions & 0 deletions tests/Button/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ public function testGenerateAriaDescribedBy(): void
);
}

public function testGenerateId(): void
{
$this->assertStringContainsString('<button id="button', Button::widget()->render());
}

public function testId(): void
{
Assert::equalsWithoutLE(
Expand Down
84 changes: 56 additions & 28 deletions tests/Tag/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,40 @@
*/
final class RenderTest extends TestCase
{
public function testElement(): void
public function testAttributes(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
test element
<div id="id" type="type">
element
</div>
HTML,
Tag::widget()->content('test element')->tagName('div')->render(),
Tag::widget()->attributes(['id' => 'id', 'type' => 'type'])->content('element')->tagName('div')->render()
);
}

public function testId(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="id">
element
</div>
HTML,
Tag::widget()->content('element')->id('id')->tagName('div')->render()
);
}

public function testPrefix(): void
{
Assert::equalsWithoutLE(
<<<HTML
<span>test prefix</span>
<span>prefix</span>
<div>
test element
element
</div>
HTML,
Tag::widget()
->content('test element')
->prefix(Span::widget()->content('test prefix'))
->tagName('div')
->render(),
Tag::widget()->content('element')->prefix(Span::widget()->content('prefix'))->tagName('div')->render()
);
}

Expand All @@ -48,19 +56,31 @@ public function testPrefixContainer(): void
Assert::equalsWithoutLE(
<<<HTML
<aside>
<span>test prefix</span>
<span>prefix</span>
</aside>
<div>
test element
element
</div>
HTML,
Tag::widget()
->content('test element')
->prefix(Span::widget()->content('test prefix'))
->content('element')
->prefix(Span::widget()->content('prefix'))
->prefixContainer(true)
->prefixContainerTag('aside')
->tagName('div')
->render(),
->render()
);
}

public function testRender(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
element
</div>
HTML,
Tag::widget()->content('element')->tagName('div')->render()
);
}

Expand All @@ -69,15 +89,11 @@ public function testSuffix(): void
Assert::equalsWithoutLE(
<<<HTML
<div>
test element
element
</div>
<span>test suffix</span>
<span>suffix</span>
HTML,
Tag::widget()
->content('test element')
->suffix(Span::widget()->content('test suffix'))
->tagName('div')
->render(),
Tag::widget()->content('element')->suffix(Span::widget()->content('suffix'))->tagName('div')->render()
);
}

Expand All @@ -86,19 +102,31 @@ public function testSuffixContainer(): void
Assert::equalsWithoutLE(
<<<HTML
<div>
test element
element
</div>
<aside>
<span>test prefix</span>
<span>prefix</span>
</aside>
HTML,
Tag::widget()
->content('test element')
->suffix(Span::widget()->content('test prefix'))
->content('element')
->suffix(Span::widget()->content('prefix'))
->suffixContainer(true)
->suffixContainerTag('aside')
->tagName('div')
->render(),
->render()
);
}

public function testType(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div type="type">
element
</div>
HTML,
Tag::widget()->content('element')->tagName('div')->type('type')->render()
);
}
}

0 comments on commit 88ebdc0

Please sign in to comment.