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, scalar|null>`

Signed-off-by: George Steel <george@net-glue.co.uk>
  • Loading branch information
gsteel committed Nov 17, 2023
1 parent 943ece1 commit a12b893
Show file tree
Hide file tree
Showing 31 changed files with 48 additions and 174 deletions.
35 changes: 10 additions & 25 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, scalar|null> */
protected $attributes = [];

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

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

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

Expand Down Expand Up @@ -158,12 +158,7 @@ public function setOption(string $key, $value)
return $this;
}

/**
* Set a single element attribute
*
* @param mixed $value
* @return $this
*/
/** @inheritDoc */
public function setAttribute(string $key, $value)
{
// Do not include the value in the list of attributes
Expand All @@ -175,11 +170,7 @@ public function setAttribute(string $key, $value)
return $this;
}

/**
* Retrieve a single element attribute
*
* @return mixed|null
*/
/** @inheritDoc */
public function getAttribute(string $key)
{
if (! isset($this->attributes[$key])) {
Expand Down Expand Up @@ -209,11 +200,7 @@ public function hasAttribute(string $key): bool
}

/**
* Set many attributes at once
*
* Implementation will decide if this will overwrite or merge.
*
* @return $this
* @inheritDoc
* @throws Exception\InvalidArgumentException
*/
public function setAttributes(iterable $arrayOrTraversable)
Expand All @@ -224,9 +211,7 @@ public function setAttributes(iterable $arrayOrTraversable)
return $this;
}

/**
* Retrieve all attributes at once
*/
/** @inheritDoc */
public function getAttributes(): array
{
return $this->attributes;
Expand All @@ -235,7 +220,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 +292,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 +304,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
6 changes: 1 addition & 5 deletions src/Element/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

class Button extends Element
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'button',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@

class Checkbox extends Element implements InputProviderInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'checkbox',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@

class Color extends Element implements InputProviderInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'color',
];
Expand Down
8 changes: 3 additions & 5 deletions src/Element/Csrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

class Csrf extends Element implements InputProviderInterface, ElementPrepareAwareInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'hidden',
];
Expand Down Expand Up @@ -103,6 +99,8 @@ public function getValue(): string
* Override: get attributes
*
* Seeds 'value' attribute with validator hash
*
* @inheritDoc
*/
public function getAttributes(): array
{
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

class Date extends DateTimeElement
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'date',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
*/
class DateTime extends AbstractDateTime
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'datetime',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/DateTimeLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@

class DateTimeLocal extends AbstractDateTime
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'datetime-local',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@

class Email extends Element implements InputProviderInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'email',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@

class File extends Element implements InputProviderInterface, ElementPrepareAwareInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'file',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

class Hidden extends Element
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'hidden',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

class Image extends Element
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'image',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class Month extends AbstractDateTime
*/
protected $format = '!Y-m';

/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'month',
];
Expand Down
13 changes: 2 additions & 11 deletions src/Element/MultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

class MultiCheckbox extends Checkbox
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'multi_checkbox',
];
Expand Down Expand Up @@ -99,12 +95,7 @@ public function setOptions(iterable $options)
return $this;
}

/**
* Set a single element attribute
*
* @param mixed $value
* @return $this
*/
/** @inheritDoc */
public function setAttribute(string $key, $value)
{
// Do not include the options in the list of attributes
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

class Number extends Element implements InputProviderInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'number',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@

class Password extends Element implements ElementPrepareAwareInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'password',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

class Radio extends MultiCheckbox
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'radio',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@

class Range extends NumberElement
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'range',
];
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

class Search extends Element
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'search',
];
Expand Down
13 changes: 2 additions & 11 deletions src/Element/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

class Select extends Element implements InputProviderInterface
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'select',
];
Expand Down Expand Up @@ -134,12 +130,7 @@ public function setOptions(iterable $options)
return $this;
}

/**
* Set a single element attribute
*
* @param mixed $value
* @return $this
*/
/** @inheritDoc */
public function setAttribute(string $key, $value)
{
// Do not include the options in the list of attributes
Expand Down
6 changes: 1 addition & 5 deletions src/Element/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

class Submit extends Element
{
/**
* Seed attributes
*
* @var array
*/
/** @var array<string, scalar|null> */
protected $attributes = [
'type' => 'submit',
];
Expand Down
Loading

0 comments on commit a12b893

Please sign in to comment.