WordPress / WordPress-Coding-Standards

PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions
MIT License
2.53k stars 471 forks source link

Review how docblock class imports are handled #2358

Open dingo-d opened 1 year ago

dingo-d commented 1 year ago

I noticed that in the docblocks we are unsing FQCN when adding types to the parameters. We are also importing those at the top of the file.

In most of the editors, the editor is smart enough that it will know to what class/interface we are referring to.

So we should consider maybe shortening the FQCNs in the docblocks?

Example:

<?php
...

namespace WordPressCS\WordPress\Helpers;

use PHP_CodeSniffer\Files\File;
...
// Later in the class/trait/interface:
    /**
     * ...
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.

Could be written as:

<?php
...

namespace WordPressCS\WordPress\Helpers;

use PHP_CodeSniffer\Files\File;
...
// Later in the class/trait/interface:
    /**
     * ...
     * @param File $phpcsFile The file being scanned.

As the File is imported at the top of the file.

Thoughts?

jrfnl commented 1 year ago

I tend to favour FQNs in docblocks for two reasons:

  1. Docblocks are not subject to use statements, so while most IDEs are smart enough by now, not everyone uses an IDE and we cannot expect them to.
  2. Docblocks may be re-used outside the context of the code - think a documentation website generated with phpDocumentor or other tooling. In that case, it can be hit-or-miss whether the names will be resolved correctly.

Using FQNs in docblocks prevents issues in both cases.