From a7be352a00121fa6fe04d799d1544384d221c522 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 3 May 2024 10:10:29 +0200 Subject: [PATCH 1/3] FormElements: Fix return type of `createElement()` method and related code This fixes the confusion of phpstan. Since the `createElement()` element returns an instance of `FormElement`, phpstan cannot find an `addHtml()` method for it, for example. The return type must therefore be `FormElement & BaseHtmlElement`. --- src/FormElement/FormElements.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/FormElement/FormElements.php b/src/FormElement/FormElements.php index 4a2c5986..f413338c 100644 --- a/src/FormElement/FormElements.php +++ b/src/FormElement/FormElements.php @@ -3,6 +3,7 @@ namespace ipl\Html\FormElement; use InvalidArgumentException; +use ipl\Html\BaseHtmlElement; use ipl\Html\Contract\FormElement; use ipl\Html\Contract\FormElementDecorator; use ipl\Html\Contract\ValueCandidates; @@ -29,7 +30,7 @@ trait FormElements /** @var bool Whether the default element loader has been registered */ protected $defaultElementLoaderRegistered = false; - /** @var FormElement[] */ + /** @var (FormElement & BaseHtmlElement)[] */ private $elements = []; /** @var array> */ @@ -38,7 +39,7 @@ trait FormElements /** * Get all elements * - * @return FormElement[] + * @return (FormElement & BaseHtmlElement)[] */ public function getElements() { @@ -70,7 +71,7 @@ public function hasElement($element) * * @param string $name * - * @return FormElement + * @return FormElement & BaseHtmlElement * * @throws InvalidArgumentException If no element with the given name exists */ @@ -89,9 +90,9 @@ public function getElement($name) /** * Add an element * - * @param string|FormElement $typeOrElement Type of the element as string or an instance of FormElement - * @param string $name Name of the element - * @param mixed $options Element options as key-value pairs + * @param string|(FormElement & BaseHtmlElement) $typeOrElement Element name OR FormElement instance + * @param string $name Name of the element + * @param array|null $options Element options as key-value pairs * * @return $this * @@ -135,9 +136,9 @@ public function addElement($typeOrElement, $name = null, $options = null) * * @param string $type Type of the element * @param string $name Name of the element - * @param mixed $options Element options as key-value pairs + * @param array|null $options Element options as key-value pairs * - * @return FormElement + * @return FormElement & BaseHtmlElement * * @throws InvalidArgumentException If the type of the element is unknown */ @@ -154,7 +155,7 @@ public function createElement($type, $name, $options = null) )); } - /** @var FormElement $element */ + /** @var FormElement & BaseFormElement $element */ $element = new $class($name); if ($options !== null) { @@ -169,7 +170,7 @@ public function createElement($type, $name, $options = null) * * Registers the element for value and validation handling but does not add it to the render stack. * - * @param FormElement $element + * @param FormElement & BaseHtmlElement $element * * @return $this * From 7b7f3ccb45f5c92677732416276167dc6b7557af Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 3 May 2024 12:55:25 +0200 Subject: [PATCH 2/3] BaseFormElement: Change param type hint Since `ValidatorChain::addValidators($param)` requires $param of the type `Traversable`, this class should reflect it. --- src/FormElement/BaseFormElement.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/FormElement/BaseFormElement.php b/src/FormElement/BaseFormElement.php index 837ac45b..36edd470 100644 --- a/src/FormElement/BaseFormElement.php +++ b/src/FormElement/BaseFormElement.php @@ -12,6 +12,7 @@ use ipl\Stdlib\Messages; use ipl\Validator\ValidatorChain; use ReflectionProperty; +use Traversable; abstract class BaseFormElement extends BaseHtmlElement implements FormElement, ValueCandidates { @@ -184,7 +185,7 @@ public function getValidators() /** * Set the validators * - * @param iterable $validators + * @param Traversable $validators * * @return $this */ @@ -201,7 +202,7 @@ public function setValidators($validators) /** * Add validators * - * @param iterable $validators + * @param Traversable $validators * * @return $this */ From a0c22b2a174da261b06218a1742b8e9a7bf8059f Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 3 May 2024 14:57:44 +0200 Subject: [PATCH 3/3] Phpstan: Update baseline --- phpstan-baseline.neon | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index eb551a95..853be2fb 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -240,11 +240,6 @@ parameters: count: 1 path: src/Form.php - - - message: "#^Parameter \\#1 \\$attributes of method ipl\\\\Html\\\\Contract\\\\FormElement\\:\\:addAttributes\\(\\) expects iterable, mixed given\\.$#" - count: 1 - path: src/Form.php - - message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge_recursive expects array, array\\|object\\|null given\\.$#" count: 1 @@ -362,11 +357,6 @@ parameters: - message: "#^Cannot access offset 'name' on mixed\\.$#" - count: 2 - path: src/FormElement/BaseFormElement.php - - - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\BaseFormElement\\:\\:addValidators\\(\\) has parameter \\$validators with no value type specified in iterable type iterable\\.$#" count: 1 path: src/FormElement/BaseFormElement.php @@ -395,11 +385,6 @@ parameters: count: 1 path: src/FormElement/BaseFormElement.php - - - message: "#^Method ipl\\\\Html\\\\FormElement\\\\BaseFormElement\\:\\:setValidators\\(\\) has parameter \\$validators with no value type specified in iterable type iterable\\.$#" - 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 @@ -435,11 +420,6 @@ parameters: 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 \\#3 \\$postfix of method ipl\\\\Html\\\\FormElement\\\\FieldsetElement\\:\\:addPluginLoader\\(\\) expects string, string\\|null given\\.$#" count: 2