phpstan-wordpress icon indicating copy to clipboard operation
phpstan-wordpress copied to clipboard

Most constants for paths like are set incorrectly

Open slaFFik opened this issue 8 months ago • 1 comments

Hello there,

I just noticed in vendor/szepeviktor/phpstan-wordpress/bootstrap.php that the constants that describe the paths are incorrect set in this stubs library.

define('WP_PLUGIN_DIR', './');
define('WPMU_PLUGIN_DIR', './');
define('WP_LANG_DIR', './');
define('WP_CONTENT_DIR', './');

ABSPATH is set correctly with a trailing slash.

WP_PLUGIN_DIR and WPMU_PLUGIN_DIR and WP_LANG_DIR are set in WP itself without a trailing slash in wp-includes/default-constants.php:

if ( ! defined( 'WP_CONTENT_DIR' ) ) {
	define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // No trailing slash, full paths only - WP_CONTENT_URL is defined further down.
}

/**
 * Allows for the plugins directory to be moved from the default location.
 *
 * @since 2.6.0
 */
if ( ! defined( 'WP_PLUGIN_DIR' ) ) {
	define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' ); // Full path, no trailing slash.
}

/**
 * Allows for the mu-plugins directory to be moved from the default location.
 *
 * @since 2.8.0
 */
if ( ! defined( 'WPMU_PLUGIN_DIR' ) ) {
	define( 'WPMU_PLUGIN_DIR', WP_CONTENT_DIR . '/mu-plugins' ); // Full path, no trailing slash.
}

define( 'WP_LANG_DIR', WP_CONTENT_DIR . '/languages' );

This results in incorrect reporting in PhpStorm when PHPStan is configured:

Image

IMO, in bootstrap.php the paths should be modified:

define('WP_CONTENT_DIR', './wp-content');
define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
define('WPMU_PLUGIN_DIR', WP_CONTENT_DIR . '/mu-plugins');
define('WP_LANG_DIR', WP_CONTENT_DIR . '/languages');

slaFFik avatar Mar 17 '25 12:03 slaFFik

Hello @slaFFik 👋🏻

Thank you for the report.

  1. this is misunderstanding: that PHP file would still not exist with these modifications 👉🏻 #239
  2. static analysis should not care about actual values

szepeviktor avatar Mar 17 '25 13:03 szepeviktor

  1. Static analysis should not concern itself with actual values.

The RequireFileExistsRule does, however, consider actual values, as do many—perhaps even all—rules that check constant values.

One way to avoid errors from the RequireFileExistsRule is to add the relevant path constants to dynamicConstantNames in extension.php. PHPStan will then generalise these constant strings to generic strings and therefore no longer report the inclusion of files using these constants as an error. As a consequence, checking for the actual existence of these files is no longer possible—although this is not currently possible in any case.

For now, I think we should proceed in this way. For the same reason—namely, that PHPStan checks constant values—we should also add other constants defined in bootstrap.php to the dynamicConstantNames list.

Against the background that these constants should be resolved to generic types, I see no reason why we should not use the actual default values. The actual values could even prove useful if, at some point, users are given the opportunity to define paths themselves.

IanDelMar avatar Jul 07 '25 11:07 IanDelMar