Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
The sniff misses the following case...
namespace Drupal\Tests\field\Kernel;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
/**
* Tests the field widget manager.
*
* @group field
*/
class WidgetPluginManagerTest extends FieldKernelTestBase {
The use Drupal\Tests\field\Kernel\FieldKernelTestBase;
is unnecessary because FieldKernelTestBase
is in the same namespace as the class.
@dawehner discovered this whilst reviewing #2674408: Fix "Drupal.Classes.UnusedUseStatement" standard in core
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#9 | 2685877.9.patch | 4.55 KB | alexpott |
#8 | 2685877.8.patch | 5.07 KB | alexpott |
#8 | 4-8-interdiff.txt | 3.47 KB | alexpott |
#6 | interdiff.txt | 1.32 KB | dawehner |
#4 | 2685877.4.patch | 4.77 KB | alexpott |
Comments
Comment #2
alexpottComment #3
alexpottHere's a failing test.
Comment #4
alexpottAnd here's the fix and also took the opportunity to remove dead code that confused me.
Comment #5
alexpottI used this on core and found a few more unused uses... see #2674408: Fix "Drupal.Classes.UnusedUseStatement" standard in core
Also tests pass...
Comment #6
dawehnerAny reason we don't use camelCase for this var as well?
I quickly tried another testcase, see interdiff, but this actually passes aready.
Comment #7
klausiSo this change is not related at all to the issue here? I'll make a separate commit for this.
Can we have a more elaborate comment what we are doing here? Like "Check if the referenced class is in the same namespace as the current file. If it is then the use statement is not necessary."
This will probably break horribly when people use multiple namespace {} constructs and use statements are not at the top of the file. But I think we can ignore that edge case and assume such code is unlikely and solve this another time if needed.
This is a bit unreliable if there are odd spaces or comments between all the parts. I think we should do a loop instead that uses PHP_CodeSniffer_Tokens::$emptyTokens to ignore that. Collecting all namsespace parts in that loop. And then do the same for the use statement.
Comment #8
alexpottThanks for the reviews
#6 - changed to camelCase
Re #7
Prevents this from being an issue an doing anything wrong.
Comment #9
alexpottHmmm #7.1 has already been committing to coder... rerolling.
Comment #11
klausiCommitted, thanks!
Fixed the coding standard errors during commit with
phpcs --standard=PHPCS coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php
and
phpcbf --standard=PHPCS coder_sniffer/Drupal/Sniffs/Classes/UnusedUseStatementSniff.php