phpstan/phpstan-nette

Nette forms callbacks

sitole opened this issue · 2 comments

Hello,
currently I have encountered problems in testing applications because PHPStan started throwing exceptions for Nette Forms Form. He doesn't like callbacks.

Array (array<callable(Nette\Forms\Form, mixed): void>) does not accept Closure(Nette\Application\UI\Form, Nette\Utils\ArrayHash): void.

for

$form->onSuccess[] = function (Nette\Application\UI\Form $form, ArrayHash $values): void {
	...
}

Array (array<callable(Nette\Forms\Form, mixed): void>) does not accept array($this(App\Modules\Section\Presenters\Forum\ForumPresenter), 'onSuccess').

for

$form->onSuccess[] = [$this, 'onSuccess'];

Is there a solution via phpstan-nette extension to fix it?

Hi, the problem is that the signature breaks LSP. When you have array<callable(Nette\Forms\Form, mixed), it's unsafe to expect Nette\Forms\Form child class in the callback.

You should change it to this code:

$form->onSuccess[] = function (\Nette\Forms\Form $form, ArrayHash $values): void {
	...
}

I get that Nette will probably work even with your code but that might change in the future without being able to detect that from the typesystem PoV. So there's no way to fix it in the extension. You should probably put it in ignoreErrors.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.