diff --git a/composer.json b/composer.json index 16addb8..b9529f2 100644 --- a/composer.json +++ b/composer.json @@ -67,6 +67,12 @@ "ecs": "php ./vendor/bin/ecs", "rector": "php ./vendor/bin/rector process --dry-run --ansi", "test": "php ./vendor/bin/tester -s -p php --colors 1 -C ./tests/Replicator", - "ci": "[\"@lint\", \"@phpstan\", \"@ecs\", \"@rector\", \"@test\"]" + "ci": [ + "@lint", + "@phpstan", + "@ecs", + "@rector", + "@test" + ] } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 13cd09b..39d1707 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,14 +1,35 @@ parameters: ignoreErrors: # https://github.com/phpstan/phpstan-nette/issues/141 + - + message: '#^Instanceof between Nette\\ComponentModel\\IComponent and Nette\\ComponentModel\\IComponent will always evaluate to true\.$#' + identifier: instanceof.alwaysTrue + # Only occurs with forms < 3.3.0 + reportUnmatched: false + count: 2 + path: src/Replicator/Container.php + - message: '#^Parameter \#1 \$array of function array_filter expects array, Iterator\ given\.$#' identifier: argument.type + # Only occurs with forms < 3.3.0 + reportUnmatched: false count: 3 path: src/Replicator/Container.php - message: '#^Parameter \#1 \$array of function array_filter expects array, Iterator\\|list\ given\.$#' identifier: argument.type + # Only occurs with forms < 3.3.0 + reportUnmatched: false count: 2 path: src/Replicator/Container.php + + - + # https://github.com/nette/forms/pull/354 + message: '#^Parameter \#2 \$callback of static method Nette\\Forms\\Container\:\:extensionMethod\(\) expects callable\(static\)\: mixed, Closure\(Nette\\Forms\\Container, string, callable, int\=, bool\=\)\: static given\.$#' + identifier: argument.type + # Only occurs with forms ≥ 3.3.0 + reportUnmatched: false + count: 1 + path: src/Replicator/Container.php diff --git a/src/Replicator/Container.php b/src/Replicator/Container.php index 1755dbb..0b57bbf 100644 --- a/src/Replicator/Container.php +++ b/src/Replicator/Container.php @@ -66,8 +66,8 @@ class Container extends Nette\Forms\Container */ public function __construct(callable $factory, int $createDefault = 0, bool $forceDefault = FALSE) { - $this->monitor(Nette\Application\UI\Presenter::class); - $this->monitor(Nette\Forms\Form::class); + $this->monitor(Nette\Forms\Form::class, $this->componentAttached(...)); + $this->monitor(Nette\Application\UI\Presenter::class, $this->componentAttached(...)); try { $this->factoryCallback = Closure::fromCallable($factory); @@ -92,10 +92,8 @@ public function setFactory(callable $factory): void /** * Magical component factory */ - protected function attached(Nette\ComponentModel\IComponent $obj): void + private function componentAttached(Nette\ComponentModel\IComponent $obj): void { - parent::attached($obj); - if ( !$obj instanceof Nette\Application\UI\Presenter && @@ -143,14 +141,15 @@ protected function createComponent(string $name): ?Nette\ComponentModel\ICompone ($this->factoryCallback)($container); - return $this->created[$container->name] = $container; + return $this->created[$container->getName()] = $container; } private function getFirstControlName(): ?string { + // TODO: The first instanceof check should be redundant but PHPStan infers getComponent() returns untyped array. $controls = array_filter( $this->getComponents(), - fn ($component): bool => $component instanceof Nette\Forms\Control, + fn ($component): bool => $component instanceof Nette\ComponentModel\IComponent && $component instanceof Nette\Forms\Control, ); $firstControl = reset($controls); @@ -274,8 +273,8 @@ private function getHttpData(): ?array */ public function remove(Nette\ComponentModel\Container $container, bool $cleanUpGroups = FALSE): void { - if ($container->parent !== $this) { - throw new Nette\InvalidArgumentException('Given component ' . $container->name . ' is not children of ' . $this->name . '.'); + if ($container->getParent() !== $this) { + throw new Nette\InvalidArgumentException('Given component ' . $container->getName() . ' is not children of ' . $this->getName() . '.'); } // to check if form was submitted by this one @@ -379,9 +378,11 @@ public function countFilledWithout(array $components = [], array $subComponents public function isAllFilled(array $exceptChildren = []): bool { $components = []; + + // TODO: The first instanceof check should be redundant but PHPStan infers getComponent() returns untyped array. $controls = array_filter( $this->getComponents(), - fn ($component): bool => $component instanceof Nette\Forms\Control, + fn ($component): bool => $component instanceof Nette\ComponentModel\IComponent && $component instanceof Nette\Forms\Control, ); foreach ($controls as $control) { if (($name = $control->getName()) !== null) { @@ -452,7 +453,7 @@ function (Nette\Forms\Controls\SubmitButton $_this, ?callable $callback = NULL) $_this->onClick[] = function (Nette\Forms\Controls\SubmitButton $button) use ($callback) { /** @var self $replicator */ $replicator = $button->lookup(static::class); - $container = $button->parent; + $container = $button->getParent(); \assert($container instanceof Nette\ComponentModel\Container); if (is_callable($callback)) { $callback($replicator, $container);