From 15efc672dca0ba959d30511848e52e6ea56ed7e2 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 1 Sep 2023 11:54:13 +0200 Subject: [PATCH] Add key & value type hint to all properties of type array --- phpstan-baseline.neon | 65 ----------------------------- src/BaseHtmlElement.php | 2 +- src/Contract/ValueCandidates.php | 4 +- src/FormElement/BaseFormElement.php | 2 +- src/FormElement/FieldsetElement.php | 13 ++++++ src/FormElement/FormElements.php | 4 +- 6 files changed, 19 insertions(+), 71 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d28dfd7a..eb551a95 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -155,11 +155,6 @@ parameters: count: 1 path: src/BaseHtmlElement.php - - - message: "#^Property ipl\\\\Html\\\\BaseHtmlElement\\:\\:\\$defaultAttributes type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/BaseHtmlElement.php - - message: "#^Property ipl\\\\Html\\\\BaseHtmlElement\\:\\:\\$voidElements type has no value type specified in iterable type array\\.$#" count: 1 @@ -180,16 +175,6 @@ parameters: count: 1 path: src/Contract/FormElementDecorator.php - - - message: "#^Method ipl\\\\Html\\\\Contract\\\\ValueCandidates\\:\\:getValueCandidates\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contract/ValueCandidates.php - - - - message: "#^Method ipl\\\\Html\\\\Contract\\\\ValueCandidates\\:\\:setValueCandidates\\(\\) has parameter \\$values with no value type specified in iterable type array\\.$#" - count: 1 - path: src/Contract/ValueCandidates.php - - message: "#^Method ipl\\\\Html\\\\Error\\:\\:getPhpTypeName\\(\\) has no return type specified\\.$#" count: 1 @@ -235,11 +220,6 @@ parameters: count: 1 path: src/Form.php - - - message: "#^Method ipl\\\\Html\\\\Form\\:\\:populate\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/Form.php - - message: "#^Method ipl\\\\Html\\\\Form\\:\\:registerAttributeCallbacks\\(\\) has no return type specified\\.$#" count: 1 @@ -280,11 +260,6 @@ parameters: count: 1 path: src/Form.php - - - message: "#^Property ipl\\\\Html\\\\Form\\:\\:\\$populatedValues type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/Form.php - - message: "#^Property ipl\\\\Html\\\\Form\\:\\:\\$submitButton \\(ipl\\\\Html\\\\Contract\\\\FormSubmitElement\\) does not accept null\\.$#" count: 1 @@ -395,11 +370,6 @@ parameters: count: 1 path: src/FormElement/BaseFormElement.php - - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\BaseFormElement\\:\\:getValueCandidates\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/FormElement/BaseFormElement.php - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\BaseFormElement\\:\\:getValueOfNameAttribute\\(\\) has no return type specified\\.$#" count: 1 @@ -430,21 +400,11 @@ parameters: count: 1 path: src/FormElement/BaseFormElement.php - - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\BaseFormElement\\:\\:setValueCandidates\\(\\) has parameter \\$values with no value type specified in iterable type array\\.$#" - count: 1 - path: src/FormElement/BaseFormElement.php - - message: "#^Parameter \\#1 \\$attributes of method ipl\\\\Html\\\\BaseHtmlElement\\:\\:addAttributes\\(\\) expects array\\|ipl\\\\Html\\\\Attributes, mixed given\\.$#" count: 1 path: src/FormElement/BaseFormElement.php - - - message: "#^Property ipl\\\\Html\\\\FormElement\\\\BaseFormElement\\:\\:\\$valueCandidates type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/FormElement/BaseFormElement.php - - message: "#^Trying to invoke mixed but it's not a callable\\.$#" count: 1 @@ -475,21 +435,11 @@ parameters: count: 1 path: src/FormElement/FieldsetElement.php - - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\FieldsetElement\\:\\:populate\\(\\) has parameter \\$values with no value type specified in iterable type iterable\\.$#" - count: 1 - path: src/FormElement/FieldsetElement.php - - message: "#^Parameter \\#1 \\$attributes of method ipl\\\\Html\\\\Contract\\\\FormElement\\:\\:addAttributes\\(\\) expects iterable, mixed given\\.$#" count: 1 path: src/FormElement/FieldsetElement.php - - - message: "#^Parameter \\#1 \\$values of method ipl\\\\Html\\\\FormElement\\\\FieldsetElement\\:\\:populate\\(\\) expects iterable, mixed given\\.$#" - count: 1 - path: src/FormElement/FieldsetElement.php - - message: "#^Parameter \\#3 \\$postfix of method ipl\\\\Html\\\\FormElement\\\\FieldsetElement\\:\\:addPluginLoader\\(\\) expects string, string\\|null given\\.$#" count: 2 @@ -500,11 +450,6 @@ parameters: count: 1 path: src/FormElement/FieldsetElement.php - - - message: "#^Property ipl\\\\Html\\\\FormElement\\\\FieldsetElement\\:\\:\\$populatedValues type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/FormElement/FieldsetElement.php - - message: "#^Cannot access offset 0 on mixed\\.$#" count: 1 @@ -545,11 +490,6 @@ parameters: count: 1 path: src/FormElement/InputElement.php - - - message: "#^Property ipl\\\\Html\\\\FormElement\\\\LocalDateTimeElement\\:\\:\\$defaultAttributes type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/FormElement/LocalDateTimeElement.php - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\PasswordElement\\:\\:registerAttributeCallbacks\\(\\) has no return type specified\\.$#" count: 1 @@ -640,11 +580,6 @@ parameters: count: 1 path: src/FormElement/SubmitButtonElement.php - - - message: "#^Property ipl\\\\Html\\\\FormElement\\\\SubmitButtonElement\\:\\:\\$defaultAttributes type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/FormElement/SubmitButtonElement.php - - message: "#^Property ipl\\\\Html\\\\FormElement\\\\SubmitElement\\:\\:\\$buttonLabel has no type specified\\.$#" count: 1 diff --git a/src/BaseHtmlElement.php b/src/BaseHtmlElement.php index 2004be5d..5dc01ce7 100644 --- a/src/BaseHtmlElement.php +++ b/src/BaseHtmlElement.php @@ -70,7 +70,7 @@ abstract class BaseHtmlElement extends HtmlDocument /** @var bool|null Whether the element is void. If null, void check should use {@link $voidElements} */ protected $isVoid; - /** @var array You may want to set default attributes when extending this class */ + /** @var array You may want to set default attributes when extending this class */ protected $defaultAttributes; /** @var string Tag of element. Set this property in order to provide the element's tag when extending this class */ diff --git a/src/Contract/ValueCandidates.php b/src/Contract/ValueCandidates.php index 0271500e..1cd2d6fd 100644 --- a/src/Contract/ValueCandidates.php +++ b/src/Contract/ValueCandidates.php @@ -7,14 +7,14 @@ interface ValueCandidates /** * Get value candidates of this element * - * @return array + * @return array */ public function getValueCandidates(); /** * Set value candidates of this element * - * @param array $values + * @param array $values * * @return $this */ diff --git a/src/FormElement/BaseFormElement.php b/src/FormElement/BaseFormElement.php index 541291ee..837ac45b 100644 --- a/src/FormElement/BaseFormElement.php +++ b/src/FormElement/BaseFormElement.php @@ -42,7 +42,7 @@ abstract class BaseFormElement extends BaseHtmlElement implements FormElement, V /** @var mixed Value of the element */ protected $value; - /** @var array Value candidates of the element */ + /** @var array Value candidates of the element */ protected $valueCandidates = []; /** diff --git a/src/FormElement/FieldsetElement.php b/src/FormElement/FieldsetElement.php index a81661f3..0d70ea4c 100644 --- a/src/FormElement/FieldsetElement.php +++ b/src/FormElement/FieldsetElement.php @@ -2,11 +2,14 @@ namespace ipl\Html\FormElement; +use InvalidArgumentException; use ipl\Html\Contract\FormElement; use ipl\Html\Contract\FormElementDecorator; use ipl\Html\Contract\Wrappable; use LogicException; +use function ipl\Stdlib\get_php_type; + class FieldsetElement extends BaseFormElement { use FormElements { @@ -46,6 +49,16 @@ public function getValue($name = null, $default = null) public function setValue($value) { + if (! is_iterable($value)) { + throw new InvalidArgumentException( + sprintf( + '%s expects parameter $value to be an array|iterable, got %s instead', + __METHOD__, + get_php_type($value) + ) + ); + } + // We expect an array/iterable here, // so call populate to loop through it and apply values to the child elements of the fieldset. $this->populate($value); diff --git a/src/FormElement/FormElements.php b/src/FormElement/FormElements.php index 6d363fd6..4a2c5986 100644 --- a/src/FormElement/FormElements.php +++ b/src/FormElement/FormElements.php @@ -32,7 +32,7 @@ trait FormElements /** @var FormElement[] */ private $elements = []; - /** @var array */ + /** @var array> */ private $populatedValues = []; /** @@ -311,7 +311,7 @@ public function getValues() /** * Populate values of registered elements * - * @param iterable $values Values as name-value pairs + * @param iterable $values Values as name-value pairs * * @return $this */