szepeviktor/phpstan-wordpress

`add_action`, `add_filter`, `__` functions incorrectly returning errors on PHP 8.1

timnolte opened this issue ยท 9 comments

johnpbloch/wordpress: 6.3.2
php-stubs/wordpress-globals: v0.2.0
php-stubs/wordpress-stubs: v6.3.2
phpstan/extension-installer: 1.3.1
phpstan/phpstan: 1.10.39
phpstan/phpstan-php-parser: 1.1.0
szepeviktor/phpstan-wordpress: v1.3.2

Errors being reported, that weren't being reported on PHP 8.0.

Function __ invoked with 2 parameters, 0 required.
Function add_action invoked with 2 parameters, 0 required.
Function add_filter invoked with 2 parameters, 0 required.

Adjusting the PHPStand analysis level has no effect. Downgrading PHPStan and/or PHPStan WordPress, or the WordPress Stubs, has no effect on resolving these errors.

Hello Tim! ๐Ÿ‘‹๐Ÿป
Thank you for using my package.

What are your results without these two?
johnpbloch/wordpress: 6.3.2
php-stubs/wordpress-globals: v0.2.0

@szepeviktor so we can't ultimately not install WP Core as then the site won't function. We install WP into a wp directory that isn't even included/visible to PHPStan. Here is what our PHPStan configuration file looks like.

includes:
	- phpstan-baseline.neon

parameters:
	# https://phpstan.org/config-reference#rule-level
	level: max
	# https://phpstan.org/config-reference#phpversion
	phpVersion: 80100
	# https://phpstan.org/config-reference#inferprivatepropertytypefromconstructor
	inferPrivatePropertyTypeFromConstructor: true
	# https://phpstan.org/user-guide/ignoring-errors#reporting-unused-ignores
	reportUnmatchedIgnoredErrors: false

	bootstrapFiles:
		- tests/phpstan-bootstrap.php

	paths:
		- public/wp-content/mu-plugins
		- public/wp-content/plugins
		- public/wp-content/themes/pantb

	excludePaths:
		analyse:
			- public/wp-content/plugins
			- public/wp-content/vendor
			- public/*/source/*
		analyseAndScan:
			- tests/*
			- public/*/build/*
			- public/*/dist/*
			- public/*/languages/*
			- public/*/node_modules/*
			- public/*/css/*
			- public/*/fonts/*
			- public/*/img/*
			- public/*/js/*
			- public/*/sass/*
			- public/wp-content/acf-json
			- public/wp-content/config
			- public/wp-content/mu-plugins/wp-local-media-proxy
			- public/wp-content/upgrade
			- public/wp-content/uploads
			- public/wp-content/wflogs

Something that is also odd is that this isn't happening on another site. All of the same versions are being used though the directory paths are different on that project.

@szepeviktor I did just remove that package from the require dependencies and reran PHPStan to no effect. I ran PHPStan with the --debug mode to ensure that it ran without caching.

@szepeviktor so something else I just tried was to remove the szepeviktor/phpstan-wordpress package and that results in the WordPress functions no longer being recognized. However, what's interesting is that the reported errors are still there.

Function __ invoked with 2 parameters, 0 required.
Function add_action invoked with 2 parameters, 0 required.
Function add_filter invoked with 2 parameters, 0 required.

It seems that these methods are being picked up somewhere else.

picked up somewhere else.

Maybe a grep -rFn 'function add_action(' will find it!

OK, I found the culprit, it was a WordPress plugin.

public/wp-content/plugins/wp-security-audit-log/third-party/vendor/scoper-autoload.php:26:if (!function_exists('add_action')) { function add_action() { return \WSAL_Vendor\add_action(...func_get_args()); } }

@szepeviktor so since this is a WordPress plugin issue that PHPStan is picking up symbols on I'm closing this. Thanks for your support!

๐ŸŽ‰

Analyzing 1 plugin/theme at a time would be the final solution.
I would gladly send a PR.