Skip to main content

Coding Conventions

In This Document

Naming Conventions

General Naming Rules

  • Use descriptive names that clearly convey purpose
  • Avoid abbreviations except for widely accepted ones (e.g., ID, HTTP)
  • Keep names concise but not at the expense of clarity
  • Be consistent with existing codebase patterns
tip

If you're spending more than a minute thinking about a name, it's probably too complex. Consider refactoring or breaking down the component.

File Organization

Directory Structure

Our agreed directory structure follows these conventions:

app/
├── Console/ # Console commands
├── Contracts/ # Interfaces
├── Events/ # Event classes
├── Exceptions/ # Custom exceptions
├── Http/
│ ├── Controllers/ # Controllers grouped by domain
│ ├── Middleware/ # HTTP middleware
│ └── Requests/ # Form requests
├── Jobs/ # Queue jobs
├── Listeners/ # Event listeners
├── Models/ # Eloquent models
├── Policies/ # Authorization policies
├── Providers/ # Service providers
├── Repositories/ # Repository pattern implementations
├── Services/ # Business logic services
└── Utilities/ # Helper classes and functions
note

New directories should be discussed with the team before creation to maintain consistency.

File Placement Rules

  • Each class should be in its own file
  • File name should match the class name exactly
  • Related files should be grouped in subdirectories by domain/feature
  • Test files should mirror the structure of the application code

Code Style

Our team follows the PSR-12 coding style for PHP with some additional agreements:

  • Maximum line length: 100 characters
  • Indent using 4 spaces (not tabs)
  • Always use curly braces for control structures, even for single-line statements
  • Add trailing commas in multiline arrays
  • Order class elements: constants, properties, constructor, public methods, protected methods, private methods

Documentation Standards

Code Comments

  • Document "why" not "what" (the code should be self-explanatory)
  • Add PHPDoc/JSDoc comments for all public methods and classes
  • Include parameter and return type documentation
  • Document exceptions that may be thrown
  • Keep comments up to date when code changes

README Files

Each major component should have a README.md file that includes:

  • Purpose of the component
  • Installation/setup instructions
  • Usage examples
  • Dependencies
  • Common issues and solutions

Agreed Exceptions

While we strive to follow these conventions consistently, we've agreed on these exceptions:

  • Legacy code may not follow these conventions until refactored
  • Third-party integrations may require deviations from naming conventions
  • Performance-critical code may prioritize optimization over strict adherence to conventions

Enforcement

These conventions are enforced through:

  • Code reviews
  • Automated linting tools
  • Pair programming sessions
  • Regular code quality discussions