Follow-up from #2587807: Drupal.Classes.UnusedUseStatement needs to scan comments.

Can Coder Sniffer scan for alias usages in e.g. @var statements and correct them to the FQN as well as removing the extraneous use statement?

PhpStorm auto-aliases this stuff, and unless we turn off automatic importing, we can't escape it (yet). I use that IDE mostly, and I have no good way to disable the UnusedUseStatement sniff when running phpcbf (except turning ON every other sniff, which is cumbersome).

So:

use \Drupal\Core\Url;

// ...

/** @var Url $link **/

becomes

// ...

/** @var \Drupal\Core\Url $link **/

(use statement removed, documentation reference changed to FQN)

Also, the remove-use statement fix would not run unless it was sure it had fixed all aliases in known documentation tokens like @see, @var, etc. (since loose references are probably impossible to parse).

Comments

wizonesolutions created an issue. See original summary.

  • klausi committed 783a88c on 8.x-2.x
    feat(UnusedUseStatementSniff): Improve fixer to update class references...
klausi’s picture

Title: Provide alternative fixer for Drupal.Classes.UnusedUseStatement when reason is due to alias use » Improve fixer for Drupal.Classes.UnusedUseStatement to update @var comments
Status: Active » Fixed

Implemented - please test!

klausi’s picture

Status: Fixed » Closed (fixed)

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