Problem/motivation

phpstan helps finding code issues. Increasing the scan level increases its scrutiny.

Proposed resolution

Raise phpstan level 0 -> 1

Issue fork linkit-3615428

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

idebr created an issue. See original summary.

idebr’s picture

Status: Active » Needs review

The merge request contains the changes for #3613044: Fix phpcs / phpstan findings in latest pipeline and fixes the new findings for level 1:

 ------ ------------------------------------------------- 
  Line   src/Plugin/Field/FieldWidget/LinkitWidget.php    
 ------ ------------------------------------------------- 
  151    Variable $default_allowed might not be defined.  
         🪪  variable.undefined                           
  194    Variable $default_allowed might not be defined.  
         🪪  variable.undefined                           
 ------ ------------------------------------------------- 

See https://git.drupalcode.org/project/linkit/-/jobs/11396556

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Reviewed & tested by the community

Environment

  • Drupal: 11.4.4
  • PHP: 8.5.5
  • Database: MariaDB 10.11.16
  • DDEV: v1.25.2
  • Linkit: 7.x-dev (0a84c12)

This is a static analysis change, so there is nothing to reproduce in the browser. Verification is done by running PHPStan against the module.

Prerequisites

  • A Drupal 11 site with drupal/linkit:7.x-dev installed and mglaman/phpstan-drupal available.
ddev composer require 'drupal/linkit:7.x-dev'
ddev drush en linkit -y
  • PHPStan needs the Drupal extension to resolve Drupal symbols, so run it through a small wrapper config that includes the module's own phpstan.neon (this keeps the level under test coming from the module itself).

Create linkit-phpstan-run.neon in the project root:

includes:
  - vendor/mglaman/phpstan-drupal/extension.neon
  - web/modules/contrib/linkit/phpstan.neon
parameters:
  customRulesetUsed: true
  paths:
    - web/modules/contrib/linkit
  excludePaths:
    - web/modules/contrib/linkit/src/Plugin/CKEditor4To5Upgrade/*

Steps

  1. Before applying the fix, run PHPStan on the unmodified module and confirm it reports [OK] No errors at the current level 0:
    ddev exec "php vendor/bin/phpstan analyse -c linkit-phpstan-run.neon --no-progress"
  2. Temporarily set level: 1 in web/modules/contrib/linkit/phpstan.neon, re-run the command above and observe the new findings. Restore the file afterwards.
  3. Apply the fix from MR !180: raise the PHPStan level from 0 to 1, initialise $default_allowed in the Linkit field widget, inject the entity type manager instead of a single storage handler in the CKEditor 5 plugin, relax the OverviewForm property visibility, and drop a stray blank line in the entity matcher deriver test.
  4. Rebuild caches: ddev drush cr
  5. Run PHPStan again and confirm it reports no errors, now at level 1.
  6. Run the module's unit and kernel tests to confirm the code changes cause no regression:
    ddev exec "cd web && SIMPLETEST_DB=mysql://db:db@db/db php ../vendor/bin/phpunit -c core/phpunit.xml.dist modules/contrib/linkit/tests/src/Unit modules/contrib/linkit/tests/src/Kernel"

Expected results

  • Without the fix, PHPStan at level 1 reports two variable.undefined errors in src/Plugin/Field/FieldWidget/LinkitWidget.php.
  • With the fix applied, PHPStan runs at level 1 and reports no errors.
  • The module's unit and kernel tests still pass.

Actual results

As expected. On unmodified 7.x the analysis is clean at level 0, and raising the level to 1 surfaces exactly the two findings listed in the issue: Variable $default_allowed might not be defined. at LinkitWidget.php lines 151 and 194. After applying MR !180 the same command runs at level 1 and returns [OK] No errors. The unit and kernel suites pass with 43 tests and 402 assertions; the reported deprecations come from Drupal core and Symfony on PHP 8.5 and are unrelated to this change.


Testing produced with the assistance of an LLM.

mark_fullmer’s picture

1+ for RTBC from me!

  • idebr committed cc0107fd on 7.x
    Issue #3615428: Raise phpstan level 0 -> 1
    
idebr’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.