Naming Convention Analyzer
| Analyzer ID | Category | Severity | Time To Fix |
|---|---|---|---|
naming-convention | ✅ Code Quality | Low | 20 minutes |
What This Checks
- Validates class, interface, trait, and enum names follow PascalCase (e.g.,
UserController,OrderService) - Ensures method names follow camelCase (e.g.,
getUserById,processPayment) - Validates property names follow camelCase (e.g.,
firstName,isActive) - Checks public constant names follow SCREAMING_SNAKE_CASE (e.g.,
MAX_LOGIN_ATTEMPTS) - Allows private/protected constants to use camelCase (modern PHP convention)
- Validates Laravel
protected $tableproperties are plural snake_case (e.g.,users,order_items) - Skips magic methods (e.g.,
__construct,__toString) - Reports exact file location and line number of each violation
- Provides suggestions for correct naming
What's NOT Checked (to avoid excessive noise):
- Local variables (e.g.,
$user_id,$is_active) - Function parameters (e.g.,
function process($user_name)) - Closure variables
Why It Matters
- Code consistency: Consistent naming makes code easier to read and understand
- Team collaboration: Standard naming helps team members understand code faster
- PSR compliance: Following PSR standards ensures compatibility with Laravel ecosystem
- IDE support: Proper naming conventions improve IDE autocomplete and navigation
- Code reviews: Consistent naming makes reviews faster and more effective
- Maintainability: Well-named code is easier to modify and extend
- Documentation: Good names reduce the need for comments
- Professional standards: Following conventions shows code quality
How to Fix
Quick Fix (5 minutes)
Rename to follow conventions:
php
// ❌ Before: Wrong naming
class user_controller {
public function get_user($id) {
// ...
}
}
// ✅ After: Correct naming
class UserController {
public function getUser(int $id) {
// ...
}
}Proper Fix (20 minutes)
1: Class Names (PascalCase)
php
// ❌ Before: Wrong class naming
class user_service {}
class OrderProcessor {}
class payment_gateway {}
// ✅ After: PascalCase
class UserService {}
class OrderProcessor {}
class PaymentGateway {}2: Method Names (camelCase)
php
// ❌ Before: Wrong method naming
class UserController {
public function get_user_by_id() {}
public function ProcessPayment() {}
public function send_email_notification() {}
}
// ✅ After: camelCase
class UserController {
public function getUserById() {}
public function processPayment() {}
public function sendEmailNotification() {}
}3: Property Names (camelCase)
php
// ❌ Before: Wrong property naming
class User {
public $first_name;
public $Last_Name;
public $is_active;
}
// ✅ After: camelCase
class User {
public $firstName;
public $lastName;
public $isActive;
}4: Constant Names
PSR-12 Convention: Only public constants require SCREAMING_SNAKE_CASE. Private and protected constants may use camelCase (modern PHP convention).
php
// ❌ Before: Wrong public constant naming
class Config {
public const maxLoginAttempts = 5; // Public must be SCREAMING_SNAKE_CASE
public const api_key = 'secret'; // Public must be SCREAMING_SNAKE_CASE
}
// ✅ After: Correct public constant naming
class Config {
public const MAX_LOGIN_ATTEMPTS = 5;
public const API_KEY = 'secret';
// Private/protected constants can use camelCase (modern PHP)
private const maxRetries = 3;
protected const defaultTimeout = 30;
private const cachePrefix = 'app_';
}Why the difference?
- Public constants are part of the public API and should follow strict PSR-12 conventions
- Private/protected constants are internal implementation details
- Modern PHP codebases often use camelCase for internal constants for consistency with properties
5: Interface and Trait Naming
php
// ❌ Before: Wrong naming
interface user_repository {}
trait has_timestamps {}
// ✅ After: PascalCase
interface UserRepository {}
trait HasTimestamps {}6: Enum Naming (PHP 8.1+)
php
// ❌ Before: Wrong enum naming
enum user_status {
case ACTIVE;
case INACTIVE;
}
// ✅ After: PascalCase
enum UserStatus {
case ACTIVE;
case INACTIVE;
}7: Laravel Table Names
Laravel Convention: Table names in protected $table should be plural snake_case.
php
// ❌ Before: Wrong table naming
class User extends Model {
protected $table = 'user'; // Should be plural
}
class OrderItem extends Model {
protected $table = 'OrderItems'; // Should be snake_case
}
class Person extends Model {
protected $table = 'person'; // Should be 'people' (irregular plural)
}
class Category extends Model {
protected $table = 'category'; // Should be 'categories'
}
// ✅ After: Correct Laravel table naming
class User extends Model {
protected $table = 'users'; // Plural snake_case
}
class OrderItem extends Model {
protected $table = 'order_items'; // Plural snake_case
}
class Person extends Model {
protected $table = 'people'; // Irregular plural
}
class Category extends Model {
protected $table = 'categories'; // -y becomes -ies
}Pluralization Engine:
This analyzer uses Laravel's Str::plural() and Str::singular() methods - the same robust pluralization engine that powers Laravel's Eloquent ORM. This automatically handles:
- Regular plurals:
user→users,product→products - Words ending in -y:
category→categories - Words ending in -s/-x/-ch/-sh:
class→classes,box→boxes - Irregular plurals:
person→people,child→children - Uncountable words:
sheep,fish,equipment - Complex compound words:
order_item→order_items
Note: Only protected $table is checked. Public or private table properties are not validated for Laravel conventions.
8: Boolean Method Names
php
// ❌ Before: Wrong boolean naming
class User {
public function active() {} // Should be isActive
public function hasPermission() {} // Good
public function CanEdit() {} // Wrong case
}
// ✅ After: Boolean conventions
class User {
public function isActive(): bool {}
public function hasPermission(): bool {}
public function canEdit(): bool {}
}References
Related Analyzers
- Missing DocBlock Analyzer - Ensures proper documentation
- Method Length Analyzer - Detects methods that are too long
- Nesting Depth Analyzer - Detects excessive code nesting