Skip to content

Commit

Permalink
refactoring, typos
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 29, 2024
1 parent 9d23b75 commit d4d452f
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/FormsDI/FormsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/Nodes/FieldNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/Nodes/FormNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function print(PrintContext $context): string
}


private function init(Tag $tag)
private function init(Tag $tag): void
{
$el = $tag->htmlElement;

Expand Down
36 changes: 10 additions & 26 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -186,7 +177,6 @@ public function getUnsafeValues($returnType, ?array $controls = null)
}


/** @return static */
public function setMappedType(string $type): static
{
$this->mappedType = $type;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 7 additions & 6 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Controls/ColorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -44,7 +44,7 @@ public function loadHttpData(): void
{
try {
parent::loadHttpData();
} catch (Nette\InvalidArgumentException $e) {
} catch (Nette\InvalidArgumentException) {
$this->setValue(null);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Controls/ImageButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ 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 = [];
foreach ($values as $value) {
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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/RadioList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Controls/SubmitButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(string|Stringable|null $caption = null)
{
parent::__construct($caption);
$this->control->type = 'submit';
$this->setOmitted(true);
$this->setOmitted();
}


Expand All @@ -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<Nette\Forms\Control|Nette\Forms\Container|string>
* @param ?iterable<Nette\Forms\Control|Nette\Forms\Container|string> $scope
*/
public function setValidationScope(?iterable $scope): static
{
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/Forms/Rendering/DefaultFormRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d4d452f

Please sign in to comment.