Document how to use phpstan.neon in a contrib module

Created on 5 November 2023, about 1 year ago
Updated 4 December 2023, about 1 year ago

Problem/Motivation

https://www.drupal.org/docs/develop/development-tools/phpstan/handling-u... โ†’ gives the example

parameters:
  ignoreErrors:
    # new static() is a best practice in Drupal, so we cannot fix that.
    - "#^Unsafe usage of new static#"

Yet does not document how to add that. I attempted to put that in module root as phpstan.neon and 2nd attempt as phpstan.neon.dist but both still result in test failure for phpstan via gitlab ci

Steps to reproduce

Add the above to a contrib module.

Proposed resolution

Update documentation to explain how to add.

Remaining tasks

Update documentation.

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/A

๐Ÿ’ฌ Support request
Status

Fixed

Version

11.0 ๐Ÿ”ฅ

Component
Documentationย  โ†’

Last updated 1 day ago

No maintainer
Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @scott_euser
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Wouldnโ€™t it be possible for the GitlabCI template to fallback to coreโ€™s phpstan.neon.dist if the
    contrib module does not include a phpstan.neon file?

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

    Just checked how that would work: phpstan then actually throws errors if an ignore error is in place yet no occurrences of the error are actually found.

  • Status changed to Fixed about 1 year ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

    Okay turns out if I copy the full phpstan.neon.dist from core and remove the ignoreErrors that do not apply to my contrib module, it works fine:

    # This file is copied from ./core/phpstan.neon.dist then set to only
    # ignore errors applicable to this project rather than all errors ignored
    # from Core as phpstan throws errors when ignore lines here are unused.
    
    # Configuration file for PHPStan static code checking, see https://phpstan.org .
    # PHPStan is triggered on Drupal CI in commit-code-check.sh.
    includes:
      - phar://phpstan.phar/conf/bleedingEdge.neon
    
    parameters:
      level: 1
    
      paths:
        - .
    
      ignoreErrors:
        # new static() is a best practice in Drupal, so we cannot fix that.
        - "#^Unsafe usage of new static#"
    

    I will update the documentation page to clarify.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    Just checked how that would work: phpstan then actually throws errors if an ignore error is in place yet no occurrences of the error are actually found.

    Darn. You're right.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States fathershawn New York

    I'll also add it to the docs, but you can add reportUnmatchedIgnoredErrors: false to your neon file and it won't squeak about ignored errors that didn't happen.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    #7 right, but that would still require a custom neon file. Here my point was NOT to have one, and still fallback to core's one.

Production build 0.71.5 2024