Skip to content

Commit d4d452f

Browse files
committed
refactoring, typos
1 parent 9d23b75 commit d4d452f

19 files changed

+46
-65
lines changed

src/Bridges/FormsDI/FormsExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct()
2626
}
2727

2828

29-
public function afterCompile(Nette\PhpGenerator\ClassType $class)
29+
public function afterCompile(Nette\PhpGenerator\ClassType $class): void
3030
{
3131
$initialize = $this->initialization ?? $class->getMethod('initialize');
3232

src/Bridges/FormsLatte/Nodes/FieldNNameNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function print(PrintContext $context): string
5757
}
5858

5959

60-
private function init(Tag $tag)
60+
private function init(Tag $tag): void
6161
{
6262
$el = $tag->htmlElement;
6363
$usedAttributes = self::findUsedAttributes($el);

src/Bridges/FormsLatte/Nodes/FormNNameNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function print(PrintContext $context): string
5757
}
5858

5959

60-
private function init(Tag $tag)
60+
private function init(Tag $tag): void
6161
{
6262
$el = $tag->htmlElement;
6363

src/Forms/Container.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,20 @@ public function setDefaults(array|object $data, bool $erase = false): static
6161
* Fill-in with values.
6262
* @internal
6363
*/
64-
public function setValues(array|object $data, bool $erase = false): static
64+
public function setValues(array|object $values, bool $erase = false): static
6565
{
66-
if ($data instanceof \Traversable) {
67-
$values = iterator_to_array($data);
68-
69-
} elseif (is_object($data) || is_array($data) || $data === null) {
70-
$values = (array) $data;
71-
}
66+
$values = $values instanceof \Traversable
67+
? iterator_to_array($values)
68+
: (array) $values;
7269

7370
foreach ($this->getComponents() as $name => $control) {
7471
if ($control instanceof Control) {
75-
if (array_key_exists($name, $values)) {
76-
$control->setValue($values[$name]);
77-
78-
} elseif ($erase) {
79-
$control->setValue(null);
72+
if (array_key_exists($name, $values) || $erase) {
73+
$control->setValue($values[$name] ?? null);
8074
}
8175
} elseif ($control instanceof self) {
82-
if (isset($values[$name])) {
83-
$control->setValues($values[$name], $erase);
84-
85-
} elseif ($erase) {
86-
$control->setValues([], $erase);
76+
if (isset($values[$name]) || $erase) {
77+
$control->setValues($values[$name] ?? [], $erase);
8778
}
8879
}
8980
}
@@ -186,7 +177,6 @@ public function getUnsafeValues($returnType, ?array $controls = null)
186177
}
187178

188179

189-
/** @return static */
190180
public function setMappedType(string $type): static
191181
{
192182
$this->mappedType = $type;
@@ -287,10 +277,7 @@ public function addComponent(
287277
): static
288278
{
289279
parent::addComponent($component, $name, $insertBefore);
290-
if ($this->currentGroup !== null) {
291-
$this->currentGroup->add($component);
292-
}
293-
280+
$this->currentGroup?->add($component);
294281
return $this;
295282
}
296283

@@ -577,10 +564,7 @@ public function addContainer(string|int $name): self
577564
{
578565
$control = new self;
579566
$control->currentGroup = $this->currentGroup;
580-
if ($this->currentGroup !== null) {
581-
$this->currentGroup->add($control);
582-
}
583-
567+
$this->currentGroup?->add($control);
584568
return $this[$name] = $control;
585569
}
586570

src/Forms/Controls/BaseControl.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function isFilled(): bool
169169
*/
170170
public function setDefaultValue($value)
171171
{
172-
$form = $this->getForm(false);
172+
$form = $this->getForm(throw: false);
173173
if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
174174
$this->setValue($value);
175175
}
@@ -182,11 +182,12 @@ public function setDefaultValue($value)
182182
* Disables or enables control.
183183
* @return static
184184
*/
185-
public function setDisabled(bool $value = true)
185+
public function setDisabled(bool $state = true)
186186
{
187-
if ($this->disabled = (bool) $value) {
187+
$this->disabled = $state;
188+
if ($state) {
188189
$this->setValue(null);
189-
} elseif (($form = $this->getForm(false)) && $form->isAnchored() && $form->isSubmitted()) {
190+
} elseif (($form = $this->getForm(throw: false)) && $form->isAnchored() && $form->isSubmitted()) {
190191
$this->loadHttpData();
191192
}
192193

@@ -206,9 +207,9 @@ public function isDisabled(): bool
206207
/**
207208
* Sets whether control value is excluded from $form->getValues() result.
208209
*/
209-
public function setOmitted(bool $value = true): static
210+
public function setOmitted(bool $state = true): static
210211
{
211-
$this->omitted = $value;
212+
$this->omitted = $state;
212213
return $this;
213214
}
214215

src/Forms/Controls/Checkbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(string|Stringable|null $label = null)
3939
public function setValue($value)
4040
{
4141
if (!is_scalar($value) && $value !== null) {
42-
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->name));
42+
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName()));
4343
}
4444

4545
$this->value = (bool) $value;

src/Forms/Controls/CheckboxList.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ public function getControl(): Html
5656
{
5757
$input = parent::getControl();
5858
$items = $this->getItems();
59-
reset($items);
60-
6159
return $this->container->setHtml(
6260
Nette\Forms\Helpers::createInputList(
6361
$this->translate($items),
@@ -66,7 +64,7 @@ public function getControl(): Html
6664
'checked?' => $this->value,
6765
'disabled:' => $this->disabled,
6866
'required' => null,
69-
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
67+
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
7068
]),
7169
$this->itemLabel->attrs,
7270
$this->separator,
@@ -98,7 +96,7 @@ public function getLabelPart($key = null): Html
9896
{
9997
$itemLabel = clone $this->itemLabel;
10098
return func_num_args()
101-
? $itemLabel->setText($this->translate($this->items[$key]))->for($this->getHtmlId() . '-' . $key)
99+
? $itemLabel->setText($this->translate($this->getItems()[$key]))->for($this->getHtmlId() . '-' . $key)
102100
: $this->getLabel();
103101
}
104102

src/Forms/Controls/ChoiceControl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setValue($value)
6262
70,
6363
'...',
6464
);
65-
throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->name}'.");
65+
throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->getName()}'.");
6666
}
6767

6868
$this->value = $value === null ? null : key([(string) $value => null]);

src/Forms/Controls/ColorPicker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function setValue($value): static
3131
{
3232
if ($value === null) {
3333
$this->value = '#000000';
34-
} elseif (is_string($value) && preg_match('~#?[0-9a-f]{6}~DAi', $value)) {
34+
} elseif (is_string($value) && preg_match('~#?[0-9a-f]{6}~Ai', $value)) {
3535
$this->value = '#' . strtolower(ltrim($value, '#'));
3636
} else {
3737
throw new Nette\InvalidArgumentException('Color must have #rrggbb format.');
@@ -44,7 +44,7 @@ public function loadHttpData(): void
4444
{
4545
try {
4646
parent::loadHttpData();
47-
} catch (Nette\InvalidArgumentException $e) {
47+
} catch (Nette\InvalidArgumentException) {
4848
$this->setValue(null);
4949
}
5050
}

src/Forms/Controls/HiddenField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function setValue($value)
4949
} elseif ($value instanceof \BackedEnum) {
5050
$value = $value->value;
5151
} elseif (!is_scalar($value) && !$value instanceof Stringable) {
52-
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->name));
52+
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", get_debug_type($value), $this->getName()));
5353
}
5454

5555
if (!$this->persistValue) {

0 commit comments

Comments
 (0)