Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
21 changes: 21 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -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\<int\|string, Nette\\ComponentModel\\IComponent\> 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\<int\|string, Nette\\ComponentModel\\IComponent\>\|list\<Nette\\ComponentModel\\IComponent\> 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
23 changes: 12 additions & 11 deletions src/Replicator/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(...));
Comment on lines +69 to +70

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the existing protected method as the explicit callback. This follows component-model 4’s required monitor() API without dropping FormsReplicator’s subclass hook.

Suggested change
$this->monitor(Nette\Forms\Form::class, $this->componentAttached(...));
$this->monitor(Nette\Application\UI\Presenter::class, $this->componentAttached(...));
$this->monitor(Nette\Forms\Form::class, $this->attached(...));
$this->monitor(Nette\Application\UI\Presenter::class, $this->attached(...));


try {
$this->factoryCallback = Closure::fromCallable($factory);
Expand All @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please retain the protected method. It has been subclass-visible since 2019 and is not marked @internal; making the replacement private creates an avoidable BC break. With the explicit callbacks above, no parent::attached() call is needed and this remains compatible with both component-model 3 and 4.

Suggested change
private function componentAttached(Nette\ComponentModel\IComponent $obj): void
protected function attached(Nette\ComponentModel\IComponent $obj): void

{
parent::attached($obj);

if (
!$obj instanceof Nette\Application\UI\Presenter
&&
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Loading