Explicit Method Visibility
ID |
php.explicit_method_visibility |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Php |
Tags |
code-style, code_smell |
Description
Reports class, abstract and interface methods declared without an explicit visibility
modifier. A method written as function foo() - or one carrying only static, abstract
or final - is implicitly public. The rule flags those declarations so that every method
states its intended access level.
Rationale
PHP defaults a method with no visibility keyword to public. Relying on that default makes
the public surface of a class implicit: a reader must remember the language rule to know
whether a method is part of the API or an internal helper. Spelling out public, private
or protected documents intent at the declaration site, and aligns with PSR-12, which since
PHP 8.0 also requires visibility on interface methods.
<?php
class Service
{
function handle() {} // FLAW - no visibility, implicitly public
static function build() {} // FLAW - static but no visibility
public function run() {} // OK - explicit public
private function cache() {} // OK - explicit private
}
Remediation
Add the intended visibility keyword (public, private or protected) to each method
declaration, including methods inside interfaces and abstract classes.