This rule raises an issue when a private function is never referenced in the code.

Why is this an issue?

A function that is never called is dead code, and should be removed. Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being introduced.

This rule detects functions that are never referenced from inside a translation unit, and cannot be referenced from the outside.

It also raises on unused protected functions in PHP enumerations. In PHP enumerations private and protected are equivalent because inheritance is not allowed.

Code examples

Noncompliant code example

class Foo {
  private function __construct() {}   // Compliant, private empty constructor intentionally used to prevent any direct instantiation of a class.

  public static function doSomething() {
    $foo = new Foo();
    ...
  }

  private function unusedPrivateFunction() {}  // Noncompliant
}

enum ExampleEnum {
  case FIRST_CASE;

  private function unusedPrivateFunction() {} // Noncompliant
  protected function unusedProtectedFunction() {} // Noncompliant
}

Compliant solution

class Foo {
  private function __construct() {}   // Compliant, private empty constructor intentionally used to prevent any direct instantiation of a class.

  public static function doSomething() {
    $foo = new Foo();
  }
}