DataTypeNamespace complains about namespace when PHPStan types are used

Created on 11 May 2025, 26 days ago

Problem/Motivation

Coder complains with:

Data types in @var tags need to be fully namespaced (Drupal.Commenting.DataTypeNamespace.DataTypeNamespace)

When using PHPStan type imports https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases

Steps to reproduce

Define or import a type
Use the type in /** @var MyType $variableName */

Proposed resolution

Never complain about PHPStan types.

Perhaps coder should check if the class exists in the same namespace as the file, before complaining about non FQN's.

🐛 Bug report
Status

Active

Version

8.3

Component

Coder Sniffer

Created by

🇦🇺Australia dpi Perth, Australia

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

Comments & Activities

  • Issue created by @dpi
  • 🇦🇺Australia dpi Perth, Australia
  • 🇦🇹Austria klausi 🇦🇹 Vienna

    Thanks for reporting!

    Can you put a full code example where the problem triggers in the issue summary?

    I tried to reproduce with this, but works fine for me:

    
    use Foo\Bar\Testi;
    
    /**
     * Example.
     *
     * @phpstan-type UserAddress array{street: string, city: string, zip: string}
     */
    class Test {
    
      /**
       * Address in the form of an array, defined on the class comment.
       *
       * @var UserAddress
       */
      protected $address;
    
      /**
       * Getter.
       *
       * @return UserAddress
       *   The address.
       */
      public function getAddress(): array {
        /** @var UserAddress $otherAddress */
        $otherAddress = [new Testi()];
        return $this->address;
      }
    
    }
    

    What am I doing wrong?

    Side note: Coder cannot check if classes exist. It is coding standards checker that cannot access/process other PHP files to check if classes exist. We are deliberately leaving that to PHPStan.

    As far as I understand our DataTypeNamespaceSniff it only checks that if a type matches the use statement, then the FQN should be used.

Production build 0.71.5 2024