5

How can I locally disable a single phpmd rule for a few lines and then re-enable a rule, for example the unusedcode?

fracz
  • 20,536
  • 18
  • 103
  • 149
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147

1 Answers1

7

PHPMD calls this Warning Suppression. Warnings Suppression granularity is that of one function scope. Comments are added to the function/class docblock, and therefore apply to that docblock's scope.

In a function doc block you can add @SuppressWarnings(unused) keyword.

From their examples:

<?php
/**
 * Suppress all rules containing "unused" in this
 * class
 *
 * @SuppressWarnings("unused")
 */
class Bar {
    private $unusedPrivateField = 42;
    public function foo($unusedFormalParameter = 23)
    {
        $unusedLocalVariable = 17;
    }
    private function unusedPrivateMethod() {
    }
}    

Rule names are not explicitly listed for use so some notes on valid names:

Some notes about warning names:

  • unused Is the supression name for the rules that ruleset runusedcode reports.
  • PHPMD is a top level name that can suppress all phpmd warnings for a block.
  • PHPMD.<rulename> where <rulename> is one of the link names from the rules doc work.
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147