From c7256933bbfd7b61ad23f72d0aa4c24b5f8c464c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 27 Apr 2024 13:35:31 +0200 Subject: [PATCH] refactoring, typos --- src/Bridges/FormsDI/FormsExtension.php | 2 +- .../FormsLatte/Nodes/FieldNNameNode.php | 2 +- .../FormsLatte/Nodes/FormNNameNode.php | 2 +- src/Forms/Container.php | 36 ++++++------------- src/Forms/Controls/BaseControl.php | 13 +++---- src/Forms/Controls/Checkbox.php | 2 +- src/Forms/Controls/CheckboxList.php | 6 ++-- src/Forms/Controls/ChoiceControl.php | 2 +- src/Forms/Controls/ColorPicker.php | 4 +-- src/Forms/Controls/HiddenField.php | 2 +- src/Forms/Controls/ImageButton.php | 4 +-- src/Forms/Controls/MultiChoiceControl.php | 6 ++-- src/Forms/Controls/RadioList.php | 2 +- src/Forms/Controls/SubmitButton.php | 4 +-- src/Forms/Controls/TextBase.php | 2 +- src/Forms/Form.php | 13 ++++--- src/Forms/Helpers.php | 2 +- src/Forms/Rendering/DefaultFormRenderer.php | 5 ++- src/Forms/Validator.php | 2 +- 19 files changed, 46 insertions(+), 65 deletions(-) diff --git a/src/Bridges/FormsDI/FormsExtension.php b/src/Bridges/FormsDI/FormsExtension.php index 5b4989f89..b06ca6775 100644 --- a/src/Bridges/FormsDI/FormsExtension.php +++ b/src/Bridges/FormsDI/FormsExtension.php @@ -26,7 +26,7 @@ public function __construct() } - public function afterCompile(Nette\PhpGenerator\ClassType $class) + public function afterCompile(Nette\PhpGenerator\ClassType $class): void { $initialize = $this->initialization ?? $class->getMethod('initialize'); diff --git a/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php b/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php index a3dbb858e..46bb7a1c6 100644 --- a/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php +++ b/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php @@ -57,7 +57,7 @@ public function print(PrintContext $context): string } - private function init(Tag $tag) + private function init(Tag $tag): void { $el = $tag->htmlElement; $usedAttributes = self::findUsedAttributes($el); diff --git a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php index fe569b71d..3c1213f03 100644 --- a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php @@ -57,7 +57,7 @@ public function print(PrintContext $context): string } - private function init(Tag $tag) + private function init(Tag $tag): void { $el = $tag->htmlElement; diff --git a/src/Forms/Container.php b/src/Forms/Container.php index e412c1b41..9c2946ae6 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -61,29 +61,20 @@ public function setDefaults(array|object $data, bool $erase = false): static * Fill-in with values. * @internal */ - public function setValues(array|object $data, bool $erase = false): static + public function setValues(array|object $values, bool $erase = false): static { - if ($data instanceof \Traversable) { - $values = iterator_to_array($data); - - } elseif (is_object($data) || is_array($data) || $data === null) { - $values = (array) $data; - } + $values = $values instanceof \Traversable + ? iterator_to_array($values) + : (array) $values; foreach ($this->getComponents() as $name => $control) { if ($control instanceof Control) { - if (array_key_exists($name, $values)) { - $control->setValue($values[$name]); - - } elseif ($erase) { - $control->setValue(null); + if (array_key_exists($name, $values) || $erase) { + $control->setValue($values[$name] ?? null); } } elseif ($control instanceof self) { - if (isset($values[$name])) { - $control->setValues($values[$name], $erase); - - } elseif ($erase) { - $control->setValues([], $erase); + if (isset($values[$name]) || $erase) { + $control->setValues($values[$name] ?? [], $erase); } } } @@ -186,7 +177,6 @@ public function getUnsafeValues($returnType, ?array $controls = null) } - /** @return static */ public function setMappedType(string $type): static { $this->mappedType = $type; @@ -287,10 +277,7 @@ public function addComponent( ): static { parent::addComponent($component, $name, $insertBefore); - if ($this->currentGroup !== null) { - $this->currentGroup->add($component); - } - + $this->currentGroup?->add($component); return $this; } @@ -577,10 +564,7 @@ public function addContainer(string|int $name): self { $control = new self; $control->currentGroup = $this->currentGroup; - if ($this->currentGroup !== null) { - $this->currentGroup->add($control); - } - + $this->currentGroup?->add($control); return $this[$name] = $control; } diff --git a/src/Forms/Controls/BaseControl.php b/src/Forms/Controls/BaseControl.php index bac909dff..641102755 100644 --- a/src/Forms/Controls/BaseControl.php +++ b/src/Forms/Controls/BaseControl.php @@ -169,7 +169,7 @@ public function isFilled(): bool */ public function setDefaultValue($value) { - $form = $this->getForm(false); + $form = $this->getForm(throw: false); if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) { $this->setValue($value); } @@ -182,11 +182,12 @@ public function setDefaultValue($value) * Disables or enables control. * @return static */ - public function setDisabled(bool $value = true) + public function setDisabled(bool $state = true) { - if ($this->disabled = (bool) $value) { + $this->disabled = $state; + if ($state) { $this->setValue(null); - } elseif (($form = $this->getForm(false)) && $form->isAnchored() && $form->isSubmitted()) { + } elseif (($form = $this->getForm(throw: false)) && $form->isAnchored() && $form->isSubmitted()) { $this->loadHttpData(); } @@ -206,9 +207,9 @@ public function isDisabled(): bool /** * Sets whether control value is excluded from $form->getValues() result. */ - public function setOmitted(bool $value = true): static + public function setOmitted(bool $state = true): static { - $this->omitted = $value; + $this->omitted = $state; return $this; } diff --git a/src/Forms/Controls/Checkbox.php b/src/Forms/Controls/Checkbox.php index 1b4cac23f..1c8ec02dc 100644 --- a/src/Forms/Controls/Checkbox.php +++ b/src/Forms/Controls/Checkbox.php @@ -39,7 +39,7 @@ public function __construct(string|Stringable|null $label = null) public function setValue($value) { if (!is_scalar($value) && $value !== null) { - throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->name)); + throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName())); } $this->value = (bool) $value; diff --git a/src/Forms/Controls/CheckboxList.php b/src/Forms/Controls/CheckboxList.php index a04207a2e..8b2bcc41d 100644 --- a/src/Forms/Controls/CheckboxList.php +++ b/src/Forms/Controls/CheckboxList.php @@ -56,8 +56,6 @@ public function getControl(): Html { $input = parent::getControl(); $items = $this->getItems(); - reset($items); - return $this->container->setHtml( Nette\Forms\Helpers::createInputList( $this->translate($items), @@ -66,7 +64,7 @@ public function getControl(): Html 'checked?' => $this->value, 'disabled:' => $this->disabled, 'required' => null, - 'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']], + 'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']], ]), $this->itemLabel->attrs, $this->separator, @@ -98,7 +96,7 @@ public function getLabelPart($key = null): Html { $itemLabel = clone $this->itemLabel; return func_num_args() - ? $itemLabel->setText($this->translate($this->items[$key]))->for($this->getHtmlId() . '-' . $key) + ? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key) : $this->getLabel(); } diff --git a/src/Forms/Controls/ChoiceControl.php b/src/Forms/Controls/ChoiceControl.php index 3d36d4de1..382290318 100644 --- a/src/Forms/Controls/ChoiceControl.php +++ b/src/Forms/Controls/ChoiceControl.php @@ -62,7 +62,7 @@ public function setValue($value) 70, '...', ); - throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->name}'."); + throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->getName()}'."); } $this->value = $value === null ? null : key([(string) $value => null]); diff --git a/src/Forms/Controls/ColorPicker.php b/src/Forms/Controls/ColorPicker.php index 0b82a9bd1..22e8f0c28 100644 --- a/src/Forms/Controls/ColorPicker.php +++ b/src/Forms/Controls/ColorPicker.php @@ -31,7 +31,7 @@ public function setValue($value): static { if ($value === null) { $this->value = '#000000'; - } elseif (is_string($value) && preg_match('~#?[0-9a-f]{6}~DAi', $value)) { + } elseif (is_string($value) && preg_match('~#?[0-9a-f]{6}~Ai', $value)) { $this->value = '#' . strtolower(ltrim($value, '#')); } else { throw new Nette\InvalidArgumentException('Color must have #rrggbb format.'); @@ -44,7 +44,7 @@ public function loadHttpData(): void { try { parent::loadHttpData(); - } catch (Nette\InvalidArgumentException $e) { + } catch (Nette\InvalidArgumentException) { $this->setValue(null); } } diff --git a/src/Forms/Controls/HiddenField.php b/src/Forms/Controls/HiddenField.php index e65185b0c..547c426e5 100644 --- a/src/Forms/Controls/HiddenField.php +++ b/src/Forms/Controls/HiddenField.php @@ -49,7 +49,7 @@ public function setValue($value) } elseif ($value instanceof \BackedEnum) { $value = $value->value; } elseif (!is_scalar($value) && !$value instanceof Stringable) { - throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->name)); + throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName())); } if (!$this->persistValue) { diff --git a/src/Forms/Controls/ImageButton.php b/src/Forms/Controls/ImageButton.php index 035fcbe0b..c9eedcd7b 100644 --- a/src/Forms/Controls/ImageButton.php +++ b/src/Forms/Controls/ImageButton.php @@ -16,8 +16,8 @@ class ImageButton extends SubmitButton { /** - * @param string $src URI of the image - * @param string $alt alternate text for the image + * @param ?string $src URI of the image + * @param ?string $alt alternate text for the image */ public function __construct(?string $src = null, ?string $alt = null) { diff --git a/src/Forms/Controls/MultiChoiceControl.php b/src/Forms/Controls/MultiChoiceControl.php index 5ab4cb555..a1aade76e 100644 --- a/src/Forms/Controls/MultiChoiceControl.php +++ b/src/Forms/Controls/MultiChoiceControl.php @@ -52,7 +52,7 @@ public function setValue($values) if (is_scalar($values) || $values === null) { $values = (array) $values; } elseif (!is_array($values)) { - throw new Nette\InvalidArgumentException(sprintf("Value must be array or null, %s given in field '%s'.", get_debug_type($values), $this->name)); + throw new Nette\InvalidArgumentException(sprintf("Value must be array or null, %s given in field '%s'.", get_debug_type($values), $this->getName())); } $flip = []; @@ -60,7 +60,7 @@ public function setValue($values) if ($value instanceof \BackedEnum) { $value = $value->value; } elseif (!is_scalar($value) && !$value instanceof \Stringable) { - throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", get_debug_type($value), $this->name)); + throw new Nette\InvalidArgumentException(sprintf("Values must be scalar, %s given in field '%s'.", get_debug_type($value), $this->getName())); } $flip[(string) $value] = true; @@ -74,7 +74,7 @@ public function setValue($values) '...', ); $vals = (count($diff) > 1 ? 's' : '') . " '" . implode("', '", $diff) . "'"; - throw new Nette\InvalidArgumentException("Value$vals are out of allowed set [$set] in field '{$this->name}'."); + throw new Nette\InvalidArgumentException("Value$vals are out of allowed set [$set] in field '{$this->getName()}'."); } $this->value = $values; diff --git a/src/Forms/Controls/RadioList.php b/src/Forms/Controls/RadioList.php index 8fe06d3fc..ef6b8586c 100644 --- a/src/Forms/Controls/RadioList.php +++ b/src/Forms/Controls/RadioList.php @@ -122,7 +122,7 @@ public function getLabelPart($key = null): Html { $itemLabel = clone $this->itemLabel; return func_num_args() - ? $itemLabel->setText($this->translate($this->items[$key]))->for($this->getHtmlId() . '-' . $key) + ? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key) : $this->getLabel(); } diff --git a/src/Forms/Controls/SubmitButton.php b/src/Forms/Controls/SubmitButton.php index fd7cdc2f7..470486ad8 100644 --- a/src/Forms/Controls/SubmitButton.php +++ b/src/Forms/Controls/SubmitButton.php @@ -35,7 +35,7 @@ public function __construct(string|Stringable|null $caption = null) { parent::__construct($caption); $this->control->type = 'submit'; - $this->setOmitted(true); + $this->setOmitted(); } @@ -59,7 +59,7 @@ public function isSubmittedBy(): bool /** * Sets the validation scope. Clicking the button validates only the controls within the specified scope. - * @param ?iterable + * @param ?iterable $scope */ public function setValidationScope(?iterable $scope): static { diff --git a/src/Forms/Controls/TextBase.php b/src/Forms/Controls/TextBase.php index 0dea850fd..57099a280 100644 --- a/src/Forms/Controls/TextBase.php +++ b/src/Forms/Controls/TextBase.php @@ -35,7 +35,7 @@ public function setValue($value) if ($value === null) { $value = ''; } elseif (!is_scalar($value) && !$value instanceof Stringable) { - throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->name)); + throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName())); } $this->value = $value; diff --git a/src/Forms/Form.php b/src/Forms/Form.php index b3ab21af6..db0ea94ff 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -256,7 +256,7 @@ public function setAction(string|Stringable $url): static /** * Returns form's action. */ - public function getAction(): mixed + public function getAction(): string|Stringable { return $this->getElementPrototype()->action; } @@ -358,7 +358,7 @@ public function removeGroup(string|ControlGroup $name): void $name = array_search($group, $this->groups, strict: true); } else { - throw new Nette\InvalidArgumentException("Group not found in form '$this->name'"); + throw new Nette\InvalidArgumentException("Group not found in form '{$this->getName()}'"); } foreach ($group->getControls() as $control) { @@ -458,7 +458,7 @@ public function setSubmittedBy(?SubmitterControl $by): static /** * Returns submitted HTTP data. */ - public function getHttpData(?int $type = null, ?string $htmlName = null): mixed + public function getHttpData(?int $type = null, ?string $htmlName = null): string|array|Nette\Http\FileUpload|null { if (!isset($this->httpData)) { if (!$this->isAnchored()) { @@ -470,11 +470,10 @@ public function getHttpData(?int $type = null, ?string $htmlName = null): mixed $this->submittedBy = is_array($data); } - if ($htmlName === null) { - return $this->httpData; - } + return $htmlName === null + ? $this->httpData + : Helpers::extractHttpData($this->httpData, $htmlName, $type); - return Helpers::extractHttpData($this->httpData, $htmlName, $type); } diff --git a/src/Forms/Helpers.php b/src/Forms/Helpers.php index 3a2fdb480..5d4eb1e50 100644 --- a/src/Forms/Helpers.php +++ b/src/Forms/Helpers.php @@ -66,7 +66,7 @@ public static function extractHttpData( } - private static function sanitize(int $type, $value) + private static function sanitize(int $type, $value): string|array|Nette\Http\FileUpload|null { if ($type === Form::DataText) { return is_scalar($value) diff --git a/src/Forms/Rendering/DefaultFormRenderer.php b/src/Forms/Rendering/DefaultFormRenderer.php index 34ebcb936..d0fc673a8 100644 --- a/src/Forms/Rendering/DefaultFormRenderer.php +++ b/src/Forms/Rendering/DefaultFormRenderer.php @@ -122,7 +122,7 @@ class DefaultFormRenderer implements Nette\Forms\FormRenderer /** * Provides complete form rendering. - * @param string $mode 'begin', 'errors', 'ownerrors', 'body', 'end' or empty to render all + * @param ?string $mode 'begin', 'errors', 'ownerrors', 'body', 'end' or empty to render all */ public function render(Nette\Forms\Form $form, ?string $mode = null): string { @@ -522,7 +522,6 @@ public function getWrapper(string $name): Html protected function getValue(string $name): mixed { $name = explode(' ', $name); - $data = &$this->wrappers[$name[0]][$name[1]]; - return $data; + return $this->wrappers[$name[0]][$name[1]] ?? null; } } diff --git a/src/Forms/Validator.php b/src/Forms/Validator.php index 3a9645f21..fa953e791 100644 --- a/src/Forms/Validator.php +++ b/src/Forms/Validator.php @@ -372,7 +372,7 @@ public static function validateFileSize(Controls\UploadControl $control, $limit) * Has file specified mime type? * @param string|string[] $mimeType */ - public static function validateMimeType(Controls\UploadControl $control, $mimeType): bool + public static function validateMimeType(Controls\UploadControl $control, string|array $mimeType): bool { $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType); foreach (static::toArray($control->getValue()) as $file) {