Skip to content

Commit

Permalink
Fix a typo and type hinting improvements (#143)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Rogachev <arogachev90@gmail.com>
  • Loading branch information
vjik and arogachev committed Nov 1, 2022
1 parent d4f08cc commit 84e0e80
Show file tree
Hide file tree
Showing 33 changed files with 161 additions and 298 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,8 @@
rename `replaceIndividualInputAttributes()` to `individualInputAttributes()` and `replaceRadioAttributes()`
to `radioAttributes()` (@vjik)
- Enh #142: Make `NoEncodeStringableInterface` extend from `Stringable` interface (@vjik)
- Bug #143: Fix a typo in the message of exception that thrown on invalid buttons' data in `ButtonGroup` widget (@vjik)
- Enh #143: Minor type hinting improvements (@vjik)

## 2.5.0 July 09, 2022

Expand Down
67 changes: 46 additions & 21 deletions src/Html.php
Expand Up @@ -138,8 +138,7 @@ final class Html
private const DATA_ATTRIBUTES = ['data', 'data-ng', 'ng', 'aria'];

/**
* @var array
* @psalm-var array<string, int>
* @var array<string, int>
*/
private static array $generateIdCounter = [];

Expand Down Expand Up @@ -678,8 +677,12 @@ public static function resetButton(string $content = 'Reset', array $attributes
* attribute will not be generated.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function input(string $type, ?string $name = null, $value = null, array $attributes = []): Input
{
public static function input(
string $type,
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): Input {
$tag = Input::tag()->type($type);
if ($name !== null) {
$tag = $tag->name($name);
Expand Down Expand Up @@ -751,8 +754,11 @@ public static function resetInput(?string $label = 'Reset', array $attributes =
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function textInput(?string $name = null, $value = null, array $attributes = []): Input
{
public static function textInput(
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): Input {
$tag = Input::text($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand All @@ -766,8 +772,11 @@ public static function textInput(?string $name = null, $value = null, array $att
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function hiddenInput(?string $name = null, $value = null, array $attributes = []): Input
{
public static function hiddenInput(
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): Input {
$tag = Input::hidden($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand All @@ -781,8 +790,11 @@ public static function hiddenInput(?string $name = null, $value = null, array $a
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function passwordInput(?string $name = null, $value = null, array $attributes = []): Input
{
public static function passwordInput(
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): Input {
$tag = Input::password($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand All @@ -800,8 +812,11 @@ public static function passwordInput(?string $name = null, $value = null, array
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function file(?string $name = null, $value = null, array $attributes = []): File
{
public static function file(
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): File {
$tag = Input::file($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand All @@ -815,8 +830,11 @@ public static function file(?string $name = null, $value = null, array $attribut
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function radio(?string $name = null, $value = null, array $attributes = []): Radio
{
public static function radio(
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): Radio {
$tag = Input::radio($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand All @@ -830,8 +848,11 @@ public static function radio(?string $name = null, $value = null, array $attribu
* @param float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function range(?string $name = null, $value = null, array $attributes = []): Range
{
public static function range(
?string $name = null,
float|int|string|Stringable|null $value = null,
array $attributes = []
): Range {
$tag = Input::range($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand All @@ -845,8 +866,11 @@ public static function range(?string $name = null, $value = null, array $attribu
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function checkbox(?string $name = null, $value = null, array $attributes = []): Checkbox
{
public static function checkbox(
?string $name = null,
bool|float|int|string|Stringable|null $value = null,
array $attributes = []
): Checkbox {
$tag = Input::checkbox($name, $value);
return $attributes === [] ? $tag : $tag->addAttributes($attributes);
}
Expand Down Expand Up @@ -898,8 +922,10 @@ public static function optgroup(): Optgroup
* @param string|Stringable $content Tag content.
* @param bool|float|int|string|Stringable|null $value The value attribute.
*/
public static function option(string|Stringable $content = '', $value = null): Option
{
public static function option(
string|Stringable $content = '',
bool|float|int|string|Stringable|null $value = null
): Option {
$tag = Option::tag();
if ($content !== '') {
$tag = $tag->content($content);
Expand Down Expand Up @@ -1155,7 +1181,6 @@ public static function li(string|Stringable $content = ''): Li
/**
* Generates a {@see Datalist} tag.
*
* @param string|Stringable $content Tag content.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function datalist(array $attributes = []): Datalist
Expand Down
25 changes: 8 additions & 17 deletions src/Tag/Base/BooleanInputTag.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Html\Tag\Base;

use Stringable;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Input;

Expand All @@ -22,10 +23,8 @@ abstract class BooleanInputTag extends InputTag
* @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-input-checked
*
* @param bool $checked Whether input it checked.
*
* @return static
*/
final public function checked(bool $checked = true): self
final public function checked(bool $checked = true): static
{
$new = clone $this;
$new->attributes['checked'] = $checked;
Expand All @@ -37,10 +36,8 @@ final public function checked(bool $checked = true): self
*
* @param string|null $label Input label.
* @param array $attributes Name-value set of label attributes.
*
* @return static
*/
final public function label(?string $label, array $attributes = []): self
final public function label(?string $label, array $attributes = []): static
{
$new = clone $this;
$new->label = $label;
Expand All @@ -53,10 +50,8 @@ final public function label(?string $label, array $attributes = []): self
*
* @param string|null $label Input label.
* @param array $attributes Name-value set of label attributes.
*
* @return static
*/
final public function sideLabel(?string $label, array $attributes = []): self
final public function sideLabel(?string $label, array $attributes = []): static
{
$new = clone $this;
$new->label = $label;
Expand All @@ -67,25 +62,21 @@ final public function sideLabel(?string $label, array $attributes = []): self

/**
* @param bool $encode Whether to encode label content. Defaults to `true`.
*
* @return static
*/
final public function labelEncode(bool $encode): self
final public function labelEncode(bool $encode): static
{
$new = clone $this;
$new->labelEncode = $encode;
return $new;
}

/**
* @param bool|float|int|string|\Stringable|null $value Value that corresponds to "unchecked" state of the input.
*
* @return static
* @param bool|float|int|string|Stringable|null $value Value that corresponds to "unchecked" state of the input.
*/
final public function uncheckValue($value): self
final public function uncheckValue(bool|float|int|string|Stringable|null $value): static
{
$new = clone $this;
$new->uncheckValue = $value === null ? null : (string)$value;
$new->uncheckValue = $value === null ? null : (string) $value;
return $new;
}

Expand Down
28 changes: 9 additions & 19 deletions src/Tag/Base/InputTag.php
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Html\Tag\Base;

use Stringable;

/**
* Base for all input tags.
*/
Expand All @@ -13,10 +15,8 @@ abstract class InputTag extends VoidTag
* @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-formelements-name
*
* @param string|null $name Name of the input.
*
* @return static
*/
public function name(?string $name): self
public function name(?string $name): static
{
$new = clone $this;
$new->attributes['name'] = $name;
Expand All @@ -26,11 +26,9 @@ public function name(?string $name): self
/**
* @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-input-value
*
* @param bool|float|int|string|\Stringable|null $value Value of the input.
*
* @return static
* @param bool|float|int|string|Stringable|null $value Value of the input.
*/
public function value($value): self
public function value(bool|float|int|string|Stringable|null $value): static
{
$new = clone $this;
$new->attributes['value'] = $value;
Expand All @@ -41,10 +39,8 @@ public function value($value): self
* @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-formelements-form
*
* @param string|null $formId ID of the form input belongs to.
*
* @return static
*/
public function form(?string $formId): self
public function form(?string $formId): static
{
$new = clone $this;
$new->attributes['form'] = $formId;
Expand All @@ -55,10 +51,8 @@ public function form(?string $formId): self
* @link https://www.w3.org/TR/html52/sec-forms.html#the-readonly-attribute
*
* @param bool $readOnly Whether input is read only.
*
* @return static
*/
public function readonly(bool $readOnly = true): self
public function readonly(bool $readOnly = true): static
{
$new = clone $this;
$new->attributes['readonly'] = $readOnly;
Expand All @@ -69,10 +63,8 @@ public function readonly(bool $readOnly = true): self
* @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
*
* @param bool $required Whether input is required.
*
* @return static
*/
public function required(bool $required = true): self
public function required(bool $required = true): static
{
$new = clone $this;
$new->attributes['required'] = $required;
Expand All @@ -83,10 +75,8 @@ public function required(bool $required = true): self
* @link https://www.w3.org/TR/html52/sec-forms.html#element-attrdef-disabledformelements-disabled
*
* @param bool $disabled Whether input is disabled.
*
* @return static
*/
public function disabled(bool $disabled = true): self
public function disabled(bool $disabled = true): static
{
$new = clone $this;
$new->attributes['disabled'] = $disabled;
Expand Down
8 changes: 2 additions & 6 deletions src/Tag/Base/ListTag.php
Expand Up @@ -18,10 +18,8 @@ abstract class ListTag extends NormalTag

/**
* @param Li ...$items One or more list items.
*
* @return static
*/
public function items(Li ...$items): self
public function items(Li ...$items): static
{
$new = clone $this;
$new->items = $items;
Expand All @@ -32,10 +30,8 @@ public function items(Li ...$items): self
* @param string[] $strings Array of list items as strings.
* @param array $attributes The tag attributes in terms of name-value pairs.
* @param bool $encode Whether to encode strings passed.
*
* @return static
*/
public function strings(array $strings, array $attributes = [], bool $encode = true): self
public function strings(array $strings, array $attributes = [], bool $encode = true): static
{
$items = array_map(
static fn (string $string) => Li::tag()
Expand Down

0 comments on commit 84e0e80

Please sign in to comment.