" /> " /> "/>

How do I ensure PHPCS takes annotations into account?

81 views Asked by At

PHPCS is reporting

<error line="5" column="1" severity="warning" message="Unused use statement" source="Drupal.Classes.UnusedUseStatement.UnusedUse"/>

for my use statement

use Drupal\Core\Entity\EntityStorageException;

however later in the same file I have

  /**
   * {@inheritdoc}
   * @throws EntityStorageException
   */
  protected function setUp(): void {

so it seems to me that PHPCS is not checking the annotations when deciding if a namespace is unused?

BTW I'm using the config_set from https://www.drupal.org/project/coder so perhaps it's a bug there. I tried switching config checking off for that line using // phpcs:disable and // phpcs: ignore but that didn't help.

1

There are 1 answers

0
owenmck On

From the documentation (PHP_Codesniffer: ignoring parts of a file):

You can try wrapping the offending code in special comments. Their example:

$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable

(Imperfect answer but at least it can let you get to a clean phpcs run.)