Naming Class
ID |
php.naming_class |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming Convention |
Language |
Php |
Tags |
convention, naming_convention |
Description
Reports a class, interface, trait or enum whose name does not follow the PascalCase convention
(also called StudlyCaps in PSR-1): the name must start with an uppercase letter and contain only
letters and digits. A leading lowercase letter, an underscore or any other separator is flagged.
Rationale
PSR-1 fixes PascalCase as the convention for class-like names, and autoloaders that map class
names to file paths rely on a predictable casing. A type named user_service or userService
departs from that expectation, hurts readability and can break case-sensitive autoloading on some
platforms. Anonymous classes have no name and are never reported. Test classes are exempt —
their names commonly carry underscores (WC_Taxes_Tests) and are never autoloaded as production
types — when the name ends in Test/Tests or the class extends a *TestCase base.
<?php
class user_service {} // FLAW — not PascalCase
class userService {} // FLAW — starts with a lowercase letter
class UserService {} // OK — PascalCase
interface user_repo {} // FLAW — not PascalCase
interface UserRepo {} // OK — PascalCase
enum status_kind { // FLAW — not PascalCase
case Active;
}
Remediation
Rename the type to PascalCase: capitalise the first letter of every word and remove separators
(user_service becomes UserService). The accepted pattern is configurable through the
classNamePattern property.