Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295995

Start running PHPStan on Drupal core (level 0)

$
0
0

Problem/Motivation

I'm finding more and more incomplete or wrong type hints with PHPStan. We are running PHPStan on our private code base, but it complains about type mismatches due to Drupal core not being documented correctly.

Example:
Parameter #1 $label of method Drupal\Core\TypedData\DataDefinition::setLabel() expects string, Drupal\Core\StringTranslation\TranslatableMarkup given.

Steps to reproduce

Run PHPStan with at least level 5 on a code base that makes use of a lot of core APIs.

Proposed resolution

Start a PHPStan initiative to run a phpstan.neon config on Drupal core itself to figure out problems. We can enable PHPStan runs on the testbot by modifying drupalci.yml and adding a step. That way we prevent regressions for any Drupal core changes in the future.

  • commit a patch (this issue) with ignores and a baseline that can enable running PHPStan level 0 and prevent further level-0 issues to enter the codebase
  • in this issue, keep to a minimum other code changes - i.e. only those without which PHPStan execution would crash
  • once committed, open followups to clean baseline and ignores to level up the existing codebase to level 0
  • once above done, repeat the same for level 1 and so forth

Remaining tasks

1) we need to have a zero-error run of PHPStan. This is not the case:

  • we still have several errors like
    20:00:11  ------ --------------------------------------------------------------------- 
    20:00:11   Line   core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php           
    20:00:11  ------ --------------------------------------------------------------------- 
    20:00:11          Class Symfony\Contracts\Service\ResetInterface not found.            
    20:00:11          💡 Learn more at https://phpstan.org/user-guide/discovering-symbols  
    20:00:11  ------ --------------------------------------------------------------------- 
    

    that cannot be ignored nor added to the baseline, so we need to solve upfront

  • One ignored error in phpstan.neon
    +++ b/core/phpstan.neon.dist
    @@ -0,0 +1,36 @@
    +		# @todo files below need to be excluded as they prevent baseline generation.
    +		# Fixing them is a priority.
    +    - modules/link/tests/src/Kernel/LinkItemTest.php
    +
    

    should be better fixed before commit, it's currently being looked at https://github.com/mglaman/phpstan-drupal/pull/279, once solved we will need updating to a new version of both phpstan/phpstan and mglaman/phpstan-drupal

  • See https://github.com/mglaman/phpstan-drupal/issues/228, https://www.drupal.org/project/drupal/issues/3254966, https://github.com/phpstan/phpstan/issues/6231

2) Running PHPstan on every individual file via commit-checks.sh takes approx 6 secs each file. On large patches, this would take quite some time to process; running on the entire codebase could take less time. Actually, PHPStan recommends to always run analysis full scope - a result cache is implemented for that purpose, see https://phpstan.org/user-guide/result-cache

PHPStan caches the result of the analysis so the subsequent runs are much faster. You should always analyse the whole project - the list of paths passed to the analyse command should be the same to take advantage of the result cache.

However, it may be hard to do in DrupalCI.

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

@todo


Viewing all articles
Browse latest Browse all 295995

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>