Skip to content

Commit

Permalink
Improve type inference for element attributes
Browse files Browse the repository at this point in the history
Marks all attribute arrays and iterables as `<string, mixed>` which is slightly better than `<array-key, mixed>`

Signed-off-by: George Steel <george@net-glue.co.uk>
  • Loading branch information
gsteel committed Nov 16, 2023
1 parent 943ece1 commit bf98515
Show file tree
Hide file tree
Showing 31 changed files with 40 additions and 34 deletions.
14 changes: 8 additions & 6 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class Element implements
InitializableInterface,
LabelAwareInterface
{
/** @var array */
/** @var array<string, mixed> */
protected $attributes = [];

/** @var null|string */
protected $label;

/** @var array */
/** @var array<string, mixed> */
protected $labelAttributes = [];

/**
* Label specific options
*
* @var array
* @var array<string, mixed>
*/
protected $labelOptions = [];

Expand Down Expand Up @@ -226,6 +226,8 @@ public function setAttributes(iterable $arrayOrTraversable)

/**
* Retrieve all attributes at once
*
* @return array<string, mixed>
*/
public function getAttributes(): array
{
Expand All @@ -235,7 +237,7 @@ public function getAttributes(): array
/**
* Remove many attributes at once
*
* @param array $keys
* @param list<string> $keys
* @return $this
*/
public function removeAttributes(array $keys)
Expand Down Expand Up @@ -307,7 +309,7 @@ public function getLabel(): ?string
/**
* Set the attributes to use with the label
*
* @param array $labelAttributes
* @param array<string, mixed> $labelAttributes
* @return $this
*/
public function setLabelAttributes(array $labelAttributes)
Expand All @@ -319,7 +321,7 @@ public function setLabelAttributes(array $labelAttributes)
/**
* Get the attributes to use with the label
*
* @return array
* @return array<string, mixed>
*/
public function getLabelAttributes(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Button extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'button',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Checkbox extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'checkbox',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Color extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'color',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Csrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Csrf extends Element implements InputProviderInterface, ElementPrepareAwar
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'hidden',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Date extends DateTimeElement
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'date',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DateTime extends AbstractDateTime
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'datetime',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/DateTimeLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DateTimeLocal extends AbstractDateTime
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'datetime-local',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Email extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'email',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class File extends Element implements InputProviderInterface, ElementPrepareAwar
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'file',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Hidden extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'hidden',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Image extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'image',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Month extends AbstractDateTime
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'month',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/MultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MultiCheckbox extends Checkbox
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'multi_checkbox',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Number extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'number',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Password extends Element implements ElementPrepareAwareInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'password',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Radio extends MultiCheckbox
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'radio',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Range extends NumberElement
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'range',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Search extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'search',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Select extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'select',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Submit extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'submit',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Tel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Tel extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'tel',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Text extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'text',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Textarea extends Element
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'textarea',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Time extends AbstractDateTime
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'time',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Url extends Element implements InputProviderInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'url',
Expand Down
2 changes: 1 addition & 1 deletion src/Element/Week.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Week extends AbstractDateTime
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'type' => 'week',
Expand Down
1 change: 1 addition & 0 deletions src/ElementAttributeRemovalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function removeAttribute(string $key);
/**
* Remove many attributes at once
*
* @param list<string> $keys
* @return $this
*/
public function removeAttributes(array $keys);
Expand Down
3 changes: 3 additions & 0 deletions src/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ public function hasAttribute(string $key): bool;
*
* Implementation will decide if this will overwrite or merge.
*
* @param iterable<string, mixed> $arrayOrTraversable
* @return $this
*/
public function setAttributes(iterable $arrayOrTraversable);

/**
* Retrieve all attributes at once
*
* @return array<string, mixed>
*/
public function getAttributes(): array;

Expand Down
2 changes: 1 addition & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Form extends Fieldset implements FormInterface
/**
* Seed attributes
*
* @var array
* @var array<string, mixed>
*/
protected $attributes = [
'method' => 'POST',
Expand Down
2 changes: 1 addition & 1 deletion src/LabelAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getLabel(): ?string;
/**
* Set the attributes to use with the label
*
* @param array $labelAttributes
* @param array<string, mixed> $labelAttributes
* @return $this
*/
public function setLabelAttributes(array $labelAttributes);
Expand Down

0 comments on commit bf98515

Please sign in to comment.