phpstan: How to ignore or work around false positives?

This seems related to #1105

Sorry if I do not share a repo or use the playground: there are too many dependencies involved that prevent me to do so.

I’ll try to explain the best I can, though.

I have a class called WPLoginUrlConverter that uses a static class called WPML\Element\API\Languages from a Composer package.

The class is imported using use WPML\Element\API\Languages; before the class’ declaration.

So, a sample code would look like that:

use WPML\Element\API\Languages;

// ...

class WPLoginUrlConverter implements \IWPML_Action {

// ...
	public function generate_rewrite_rules() {
		global $wp_rewrite;

		/** @phpstan-ignore-next-line */
		$language_rules = wpml_collect( Languages::getActive() )->mapWithKeys( function ( $lang ) {
			return [ $lang['code'] . '/wp-login.php' => 'wp-login.php' ];
		} );

		//...
	}

// ...

}

From what PHPStan is concerned, the validation should stop at Languages::getActive() (which I remind comes from a Composer package): it should check that this function returns the type by wpml_collect.

However, it analyzes the content of Languages::getActive(), it see that it calls a function that expects an argument of type callable, but receives a callback in the form of [ $somevariable, 'some_method' ] ($somevariable is an instance of a class, and some_method is a public method, non-static, of that class).

PHPStan decides that this is not good enough and fails the validation because TypeError (Argument 2 passed to WPML\Element\API\Languages::macro() must be callable, array given.

So, here are my objections:

  • First of all, the call to Languages::getActive() is fine, it has no issue whatsoever
  • PHPStan should not validate the cone inside, Languages::getActive because it’s not part of the codebase: again, it’s a Composer package, which sooner or later will also go through its round of validation
  • There is no clear way to ignore or work around the failed validation

I can’t change/fix the problem without some breaking changes in that Composer package.

I could add the ignore comments to that package and, in fact, I’ve tried. And failed. PHPStan seems only to parse these comments when it’s in the codebase.

I’ve tried to add /** @phpstan-ignore-next-line */ before use WPML\Element\API\Languages;: no luck. I’ve tried to add the same comment before the call Languages::getActive(), but this also didn’t work.

I’m now out of options.

PHPStan is supposed only to validate the codebase, so why it does that?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

I’ve showed you what you need to fix: https://github.com/phpstan/phpstan/issues/3983#issuecomment-714451187

What’s probably happening is that in a real application, there’s something bootstrapped that makes this error go away. In that case you need to write your own bootstrap and put it into bootstrapFiles so that the application doesn’t error like this either.

To make this go away, you can use this experimental flag that prevents any analysed code being executed, but at a performance penalty:

parameters:
    featureToggles:
        disableRuntimeReflectionProvider: true

But I believe it’s worth it to investigate why the app crashes like that.