Using PHPStan can improve the code quality of Drupal core.
This issue can be used to track progress on cleaning up all PHPStan errors found in Drupal Core.
PHPStan runs in different levels, the plan is to go up each level, and fix errors one level at a time.
PHPStan has a baseline to add, so we can go up a level, and by that moment disallow new errors from that level to enter the codebase, and add all the existing errors to the baseline so they can be fixed afterwards.
Start using phpstan
Add and configure phpstan in core
#3178534: Start running PHPStan on Drupal core (level 0)
#3259142: PHPStan commit check fails if a file is deleted
#3259353: Run full phpstan on composer change
#3259355: Always do a full phpstan analysis on DrupalCI
PHPStan ignored errors and files
To allow PHPStan to be added to core it was decided to ignore some files throwing problems and also ignore some errors. Those should be revisited.
excludePaths:
# Skip settings.
- ../*/settings*.php
# Skip test fixtures.
- */tests/fixtures/*.php
# Below extends on purpose a non existing class for testing.
- modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/fruit/ExtendingNonInstalledClass.php
# @todo files below need to be excluded as they prevent baseline generation.
# Fixing them is a priority.
- modules/link/tests/src/Kernel/LinkItemTest.php
ignoreErrors:
# new static() is a best practice in Drupal, so we cannot fix that.
- "#^Unsafe usage of new static#"
# Ignore common errors for now.
- "#^Access to an undefined property#"
- "#^Call to an undefined method#"
- "#^Cannot unset offset#"
- "#should return .* but return statement is missing#"
- "#Drupal calls should be avoided in classes, use dependency injection instead#"
- "#^Plugin definitions cannot be altered.#"
- "#^Missing cache backend declaration for performance.#"
- "#cache tag might be unclear and does not contain the cache key in it.#"
- "#^Class .* extends @internal class#"
#3254966: PHPStan error LinkItemTest
#3259110: Fix undefined variables where files are included
#3259109: Fix 'Cannot unset offset' PHPStan L0 errors
#3259716: Replace usages of static::class . '::methodName' to [static::class, 'methodName']
Upstream issues to followup
We already found some improvements / false positives. The should be fixed upstream and updated when a new release is available for phpstan / phpstan-drupal.
* Function twig_escape_filter not found #310
message: "#^Function twig_escape_filter not found\\.$#"
count: 1
path: lib/Drupal/Core/Template/TwigExtension.php
* BrowserTestBaseDefaultThemeRule ignore InstallerExistingConfigTestBase tests #311
message: "#^Drupal\\\\Tests\\\\BrowserTestBase\\:\\:\\$defaultTheme is required\\. See https\\://www\\.drupal\\.org/node/3083055, which includes recommendations on which theme to use\\.$#"
count: 1
path: modules/quickedit/tests/src/FunctionalJavascript/QuickEditJavascriptTestBase.php
* RenderCallbackRule causes error in PlaceholderGenerator #303
message: "#^The \"\\#lazy_builder\" expects a callable array with arguments\\.$#"
count: 1
path: lib/Drupal/Core/Render/PlaceholderGenerator.php
PHPStan level 0
Review the baseline and check for issues to be created.
https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/phpstan-bas...
One of the most occuring problems in the baseline is
Call to deprecated constant REQUEST_TIME (133 of 230 lines in baseline)
#2902895: [meta][no patch] replace uses of REQUEST_TIME and time() with time service
PHPStan level 1 (and up)
Decide when to go a level up? Does the baseline needs to be fully clean?
#3190406: Update PHPStan to level 1