Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude PHPUnit asserts etc. from DynamicCallOnStaticMethodsRule? #36

Closed
BluePsyduck opened this issue Sep 7, 2018 · 7 comments
Closed

Comments

@BluePsyduck
Copy link

When using PHPUnit, assertions and some other methods are normally called dynamically (e.g. $this->assertSame() or $this->once()), although they are declared as static methods. This of course triggers the DynamicCallOnStaticMethodsRule for all these calls.

According to the author of PHPUnit, it is encouraged to use $this-> instead of self::.

Keeping that in mind the question is: Should the PHPUnit classes (e.g. PHPUnit\Framework\Assert and PHPUnit\Framework\TestCase) be excluded from the DynamicCallOnStaticMethodsRule? (Maybe through a parameter to enable/disable this?)

@mhujer
Copy link

mhujer commented Sep 7, 2018

From https://phpunit.readthedocs.io/en/7.3/assertions.html#static-vs-non-static-usage-of-assertion-methods

A common question, especially from developers new to PHPUnit, is whether using $this->assertTrue() or self::assertTrue(), for instance, is “the right way” to invoke an assertion. The short answer is: there is no right way. And there is no wrong way, either. It is a matter of personal preference.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Sep 7, 2018 via email

@eugcar
Copy link

eugcar commented Sep 17, 2019

@ondrejmirtes Could you gently say explicitly how to simply put a regexp to ignoreErrors config key?

@BluePsyduck
Copy link
Author

@eugcar In your phpstan.neon configuration file, add the following:

parameters:
  ignoreErrors:
    - '#Dynamic call to static method PHPUnit\\Framework\\.*#'

This will ignore the error for all relevant PHPUnit classes.

@ondrejmirtes
Copy link
Member

It's in the README.

@eugcar
Copy link

eugcar commented Sep 17, 2019

@BluePsyduck @ondrejmirtes Thanks for your support. It works :)

@github-actions
Copy link

github-actions bot commented May 1, 2021

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants