@phpstan-ignore-next-line annotation collides with PHPCS

Created on 31 March 2025, about 1 month ago

Problem/Motivation

This

class Schema extends BaseMySqlSchema {

  /**
   * {@inheritdoc}
   */
  // @phpstan-ignore-next-line missingType.return
  public function addField($table, $field, $spec, $keys_new = []) {
    if ..

fails PHPCS. If you move the @phpstan annotation above the docblock, PHPStan fails because it's not the next line to be ignored.

This

class Schema extends BaseMySqlSchema {

  // @phpcs:disable
  /**
   * {@inheritdoc}
   */
  // @phpstan-ignore-next-line missingType.return
  public function addField($table, $field, $spec, $keys_new = []) {
  // @phpcs:enable
    if ..

works but obviously has a risk of introducing uncompliant code.

Proposed resolution

Skip @phpstan-xxx inline annotations when sniffing.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

8.3

Component

Coder Sniffer

Created by

🇮🇹Italy mondrake 🇮🇹

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

Comments & Activities

  • Issue created by @mondrake
  • 🇦🇹Austria klausi 🇦🇹 Vienna

    Thanks for reporting!

    I think you can use PHPStan ignores like this:

    class Schema extends BaseMySqlSchema {
    
      /**
       * {@inheritdoc}
       *
       * @phpstan-ignore-next-line missingType.return 
       */
      public function addField($table, $field, $spec, $keys_new = []) {
        if ...
    

    Can you try if that works? I would like avoid writing special code to detect and ignore PHPStan comments as that would make a lot of our sniffs more complicated.

    Downgrading priority to normal, I don't think this is a major issue as you can do this workaround.

  • 🇮🇹Italy mondrake 🇮🇹

    Hi, thanks

    No that does not work - there's the PHPDoc ending token */ in an isolated line and PHPStan does not take it into account - it says that there are no errors to ignore on that line.

  • 🇮🇹Italy mondrake 🇮🇹
  • 🇬🇧United Kingdom jonathan1055

    I think Moondrake might have a valid point here. Not only for the PHPStan problem, but we also the phpstan-ignore line used in this context gives the message:

    ---------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ---------------------------------------------------------------------------
     12 | ERROR | [x] You must use "/**" style comments for a function comment
        |       |     (Drupal.Commenting.FunctionComment.WrongStyle)
    ---------------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------
    

    When you run phpcbf, the fixed file becomes:

    class Schema extends BaseMySqlSchema {
    
      /**
       * Something.
       */
    
      /**
       * @phpstan-ignore-next-line missingType.return
       */
      public function addField($table, $field, $spec, $keys_new = []) {
      }
    

    The phptan-ignore has been converted into a function comment. So maybe it would be worth trying to disregard the phpstan-ignore token as suggested, then Coder would (a) not produce this invalid fixed file and (b) allow phpstan to properly ignore the next line as required.

  • 🇬🇧United Kingdom jonathan1055

    I have cerated PR263 which detects and disregards // @phpstan-ignore comments when searching back from the function definition looking for the function docblock.
    https://github.com/pfrenssen/coder/pull/263

    If this is going to be OK, then I can add a test case for it, but won't do all that if its a no-goer.

  • 🇬🇧United Kingdom catch

    This would be great, having to phpcs-ignore the phpstan ignore declarations is slightly eye watering.

  • 🇬🇧United Kingdom jonathan1055

    Yes that is tedious. This solution works for the single case reported, but ideally we should try to ignore all 'phpstan-...' inline comments. I think this is what Klausi was referring to, as being a lot of work. phpcs_codesniffer helpfully tokenizes their own phpcs- comments, so it is easy to disregard them using Tokens::$phpcsCommentTokens. But unfortunately the phpstan comments are not pre-tokenized, so we have to detect them individually, as I did in this PR.

    If this approach is acceptable then maybe we can make that conditiona (if and preg_match) into a function that is callable from other places, so that the improvement can be utilized in other sniffs.

Production build 0.71.5 2024