Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sniff for unused class imports #35

Closed
andrewnicols opened this issue Dec 20, 2022 · 1 comment · Fixed by #57
Closed

Add a sniff for unused class imports #35

andrewnicols opened this issue Dec 20, 2022 · 1 comment · Fixed by #57

Comments

@andrewnicols
Copy link
Contributor

<?php

use foo;
use bar;
use baz;

$example = new foo(baz::class);

The bar class import is unused and should be:

  • reported
  • removed

Drupal have a sniff for this:
https://packagist.org/packages/drupal/coder

https://git.drupalcode.org/project/coder/-/blob/8.3.x/coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php

It's been proposed for import into SquizLabs here:
squizlabs/PHP_CodeSniffer#1106

It's also part of the SlevoMat sniffs:
https://github.com/slevomat/coding-standard/blob/master/doc/namespaces.md#slevomatcodingstandardnamespacesunuseduses-

@stronk7
Copy link
Member

stronk7 commented Dec 20, 2022

Yeah, I've been thinking about this for some good time, and I'd say that there are a few cases related also worth being considered:

  1. "use" uses straight under a namespace:
namespace mod_forum

use mod_forum\foo;
  1. Complete class-path uses of things declared as "use"
use mod_forum\foo;

$example = new \mod_forum\foo();
  1. I think that there are a few more around that I've seen and should have annotated in some of my namespaces notes.

In fact I've here a very basic shell script (from where we did the Epic adding namespaces to all test files), that was able to detect some of the cases. Maybe we should consider this within the "namespaces" epic, now that we have the apix.json in core and other things and start enforcing that area globally.

I'd say that "namespaces" and "phpdoc" should be our 2 very next areas in moodle-cs.

Ciao :-)

@andrewnicols andrewnicols linked a pull request Sep 15, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants