Skip to content

UnnecessaryFinalModifier ​

info on by default quick-fix

Group: Code style · PhpStorm name: UnnecessaryFinalModifierInspection

final on a method is redundant when the class itself is final (nothing can extend it) or when the method is private (it cannot be overridden anyway, constructors and other magic methods aside).

Example ​

php
<?php
class Engine
{
    final protected function start() {}
    final private function ignite() {}
    private static final function spark() {}
    final private function __clone() {}
}

final class Turbine
{
    final public function spin() {}
    final protected function __wakeup() {}
    public function idle() {}
}

class Gauge
{
    public int $level {
        final get => 1;
    }
}
php
<?php
class Engine
{
    final protected function start() {}
    private function ignite() {}
    private static function spark() {}
    final private function __clone() {}
}

final class Turbine
{
    public function spin() {}
    protected function __wakeup() {}
    public function idle() {}
}

Reported:

  • line 5: Redundant final: the method cannot be overridden anyway.
  • line 6: Redundant final: the method cannot be overridden anyway.
  • line 12: Redundant final: the method cannot be overridden anyway.
  • line 13: Redundant final: the method cannot be overridden anyway.

Configure ​

In custos.json:

json
{
  "rules": {
    "UnnecessaryFinalModifier": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore UnnecessaryFinalModifier

/**
 * @noinspection UnnecessaryFinalModifierInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.