Skip to content

Commit 738afd2

Browse files
committed
phpstan level 6
1 parent a94ab82 commit 738afd2

8 files changed

Lines changed: 483 additions & 45 deletions

File tree

phpstan-baseline.neon

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ parameters:
1111

1212
checkMissingCallableSignature: true
1313

14+
ignoreErrors:
15+
- # Latte nodes use new static() by design for extensibility
16+
identifier: new.static
17+
path: src/Bridges/FormsLatte/Nodes/*
18+
1419
includes:
1520
- phpstan-baseline.neon

src/Bridges/FormsLatte/Nodes/FieldNNameNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function init(Tag $tag): void
105105
public static function findUsedAttributes(ElementNode $el): array
106106
{
107107
$res = [];
108-
foreach ($el->attributes?->children as $child) {
108+
foreach ($el->attributes->children as $child) {
109109
if ($child instanceof AttributeNode && $child->name instanceof TextNode) {
110110
$res[] = $child->name->content;
111111
} elseif ($child instanceof ExpressionAttributeNode) {

src/Forms/Container.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,12 @@ public function getUntrustedValues(string|object|null $returnType = null, ?array
181181
}
182182

183183

184-
/** @deprecated use getUntrustedValues() */
185-
public function getUnsafeValues($returnType, ?array $controls = null)
184+
/**
185+
* @deprecated use getUntrustedValues()
186+
* @param ?list<Control> $controls
187+
* @return object|array<string, mixed>
188+
*/
189+
public function getUnsafeValues(string|object|null $returnType, ?array $controls = null): object|array
186190
{
187191
return $this->getUntrustedValues($returnType, $controls);
188192
}
@@ -303,7 +307,52 @@ public function addComponent(
303307
*/
304308
public function getControls(): \Iterator
305309
{
306-
return $this->getComponents(true, Control::class);
310+
return new class ($this->getComponentTree()) implements \Iterator {
311+
/** @var Controls\BaseControl[] */
312+
private array $controls = [];
313+
private int $position = 0;
314+
315+
316+
/** @param list<Nette\ComponentModel\IComponent> $tree */
317+
public function __construct(array $tree)
318+
{
319+
foreach ($tree as $component) {
320+
if ($component instanceof Controls\BaseControl) {
321+
$this->controls[] = $component;
322+
}
323+
}
324+
}
325+
326+
327+
public function current(): Controls\BaseControl
328+
{
329+
return $this->controls[$this->position];
330+
}
331+
332+
333+
public function key(): string
334+
{
335+
return $this->controls[$this->position]->getName();
336+
}
337+
338+
339+
public function next(): void
340+
{
341+
++$this->position;
342+
}
343+
344+
345+
public function rewind(): void
346+
{
347+
$this->position = 0;
348+
}
349+
350+
351+
public function valid(): bool
352+
{
353+
return isset($this->controls[$this->position]);
354+
}
355+
};
307356
}
308357

309358

src/Forms/Control.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@
88
declare(strict_types=1);
99

1010
namespace Nette\Forms;
11+
use Nette\Utils\Html;
1112

1213

1314
/**
1415
* Defines method that must be implemented to allow a component to act like a form control.
16+
* @method ?Form getForm(bool $throw = true)
17+
* @method mixed getOption(string $key)
18+
* @method static setOption(string $key, mixed $value)
19+
* @method Html|string|null getControl()
20+
* @method Html|string|null getLabel()
21+
* @method bool isDisabled()
22+
* @method bool isRequired()
23+
* @method bool hasErrors()
24+
* @method string lookupPath(string $type, bool $throw = true)
25+
* @method void addError(string|\Stringable $message, bool $translate = true)
26+
* @method bool isFilled()
27+
* @method ?Nette\ComponentModel\IContainer getParent()
28+
* @method ?string getName()
29+
* @method string getHtmlName()
1530
*/
1631
interface Control
1732
{

src/Forms/Controls/MultiSelectBox.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Forms\Controls;
1111

1212
use Nette;
13+
use Stringable;
1314
use function is_array;
1415

1516

src/Forms/Form.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ protected function receiveHttpData(): ?array
583583
}
584584

585585
if ($tracker = $this->getComponent(self::TrackerId, throw: false)) {
586+
assert($tracker instanceof Control);
586587
if (!isset($data[self::TrackerId]) || $data[self::TrackerId] !== $tracker->getValue()) {
587588
return null;
588589
}

tests/types/forms-types.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,11 @@
77

88
declare(strict_types=1);
99

10-
use Nette\Forms\Container;
11-
use Nette\Forms\Controls\BaseControl;
1210
use Nette\Forms\Form;
1311
use Nette\Utils\ArrayHash;
1412
use function PHPStan\Testing\assertType;
1513

1614

17-
class FormDto
18-
{
19-
public string $name;
20-
}
21-
22-
23-
function testFormContainerGetValues(Container $container): void
24-
{
25-
$values = $container->getValues();
26-
assertType(ArrayHash::class, $values);
27-
28-
$valuesArray = $container->getValues(asArray: true);
29-
assertType('array<string, mixed>', $valuesArray);
30-
31-
$valuesDto = $container->getValues(FormDto::class);
32-
assertType(FormDto::class, $valuesDto);
33-
}
34-
35-
36-
function testFormContainerGetUnsafeValues(Container $container): void
37-
{
38-
$values = $container->getUnsafeValues(null);
39-
assertType(ArrayHash::class, $values);
40-
41-
$valuesArray = $container->getUnsafeValues('array');
42-
assertType('array<string, mixed>', $valuesArray);
43-
44-
$valuesDto = $container->getUnsafeValues(FormDto::class);
45-
assertType(FormDto::class, $valuesDto);
46-
}
47-
48-
49-
function testFormContainerArrayAccess(Container $container): void
50-
{
51-
$control = $container['name'];
52-
assertType(BaseControl::class, $control);
53-
}
54-
55-
5615
function testFormEvents(Form $form): void
5716
{
5817
$form->onSuccess[] = function (Form $form, ArrayHash $values): void {

0 commit comments

Comments
 (0)