An empty function is generally considered bad practice and can lead to confusion, readability, and maintenance issues. Empty functions bring no functionality and are misleading to others as they might think the function implementation fulfills a specific and identified requirement.
There are several reasons for a function not to have a body:
This rule doesn’t raise an issue in the following cases:
.stub.php and files with a leading PHPDoc @generate-function-entries or @generate-class-entries tag, as
described in the PHP stub guide.
abstract class Animal {
public function speak() {} // default implementation ignored
}
class Person {
public function __construct(private string $name) {} // constructor with promoted property ignored
}
class Utility {
private function __construct() {} // private constructor without parameters ignored
}
public function shouldNotBeEmpty() { // Noncompliant - method is empty
}
public function notImplemented() { // Noncompliant - method is empty
}
public function emptyOnPurpose() { // Noncompliant - method is empty
}
public function shouldNotBeEmpty() {
doSomething();
}
public function notImplemented() {
throw new Exception('notImplemented() cannot be performed because ...');
}
public function emptyOnPurpose() {
// comment explaining why the method is empty
}