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.

Instead of adding PHPStan specific functionality, perhaps coder should instead check if the class exists in the same namespace as the file, before complaining about non FQN's. This would mean that a type not defined as a class AND phpstan would not throw an error, but I think thats fine. The sniff is testing about FQN, not type or class-existence. If we want sniffs ensuring existence, then that should be the responsibility of a different sniff.

Comments

dpi created an issue. See original summary.

dpi’s picture

Issue summary: View changes
klausi’s picture

Status: Active » Postponed (maintainer needs more info)

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.

dpi’s picture

mxr576’s picture

Status: Postponed (maintainer needs more info) » Active

I have just came across the same issue and the example from #4 proves the problem.

In addition

 * @template Foo of \Drupal\foo\Bar
....
   * @phpstan-param Foo $apiBaseUrl

Foo in @phpstan-param also gets changed to FQCN.

klausi’s picture

Version: 8.3.x-dev » 9.x-dev
Status: Active » Fixed

This was fixed in Coder 9 and the sniff DataTypeNamespace removed.

Please test and let me know if this is still an issue!

Note that we are also dropping FQN checks in data type comments in #3360163: Stop using FQCN in @param @var @return annotations, that should land soon.

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.

Status: Fixed » Closed (fixed)

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