phpstan/phpstan-nette

Call to an undefined method Nette\ComponentModel\IComponent::addError()

japlavaren opened this issue · 15 comments

When I am using code $form['password']->addError('Error'); what is valid nette error on form component, I am getting Call to an undefined method Nette\ComponentModel\IComponent::addError() that is right. There is no addError defined in IComponent.
Can I add custom rule to skip this validation?

I have found solution - add this to phpstan.neon:

parameters:
    ignoreErrors:
        - '#Call to an undefined method Nette\\ComponentModel\\IComponent::addError\(\)\.#'

Think about include this to phpstan-nette

hrach commented

This is the most annoying error after upgrading to 0.10. I've ignored this too. I see no easy fix on Phpstan's side. This is not an optimal design of Nette Forms :(

Yeah, the form definitions are not easy to find, they are not declared consistently in Nette projects.

You don't understand - what I meant is that $form['password'] is probably a TextInput or something but PHPStan doesn't know that because the $form->addPassword('password') can be in many places.

I understand that is not easy to say, that $form['password'] is part of
nette internal code. But phpstan is true that Nette\Forms\Form array
acces is returning IComponent via.
https://api.nette.org/2.4/source-Forms.Container.php.html#507 But there is
no addError() method for IComponent. So what I did, I added rule for
ignoring errors - '#Call to an undefined method Nette\\ComponentModel\\IComponent::addError\(\)\.#' that is saying,
calling addError() on IComponent is valid. What is true because all
nette form inputs have parent Nette\Forms/Controls/BaseControl (which are
implementing IControl) that have method addError(). So for 99% of cases
there should be added rule ignoreErrors: - '#Call to an undefined method Nette\\ComponentModel\\IComponent::addError\(\)\.#' to
phpstan/phpstan-nette package.

It's not possible to ignore this error because not all projects contain this error and it would report "unmatched ignored error" by default.

I did not know about this. Is there any way to add it there but without causing "unmatched ignored error" when there is not this cause?

Yes, there's reportUnmatchedIgnoredErrors: true (by default) but I don't want to change this setting in PHPStan itself.

I agree that it is not good idea to change it globally. But I was thinking if there is easy way to add rules for ignored errors aside of errors without unmatched ignored error message.

I just got an idea, there should be a dynamic return type extension for getComponent/offsetGet that says that components accessed on Form are Form's BaseControl and not IComponent 😊

hrach commented

That's a problem, since it could be a Nette/Forms/Container or actually some IComponent for advanced forms.

I think we can disregard IComponent because it won't be that in 99% of cases but the Container is a problem.

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.