# 📘 PHP/Laravel Specific Coding Rules

### 📘 Rule P001 – Follow PSR-12 Coding Standard

- **Objective**: Maintain consistent code formatting across all PHP projects.
- **Details**:
    - Use 4 spaces for indentation.
    - Class opening braces on new lines.
    - Method visibility (public/private/protected) must be declared.
- **Applies to**: PHP
- **Tool**: PHP_CodeSniffer, Laravel Pint
- **Principle**: CODE_QUALITY
- **Version**: 1.0
- **Status**: activated
- **Severity**: major

### 📘 Rule P002 – Skinny Controllers, Fat Models/Services

- **Objective**: Maintain clear separation of concerns and keep controllers easy to read.
- **Details**:
    - Controllers should only handle request input and response output.
    - Business logic should reside in Service classes or Action classes.
    - Aim for actions under 10 lines of code.
- **Applies to**: PHP/Laravel
- **Tool**: Manual Review, Architecture Rules
- **Principle**: MAINTAINABILITY, SRP
- **Version**: 1.0
- **Status**: activated
- **Severity**: major

### 📘 Rule P003 – Use Eloquent over Raw SQL/Query Builder

- **Objective**: Improve code readability and leverage Laravel's built-in security features.
- **Details**:
    - Use Eloquent ORM for most database interactions.
    - Use Query Builder only for complex performance-critical queries.
    - Never use raw SQL unless absolutely necessary and properly parameterized.
- **Applies to**: PHP/Laravel
- **Tool**: Manual Review
- **Principle**: CODE_QUALITY, SECURITY
- **Version**: 1.0
- **Status**: activated
- **Severity**: major

### 📘 Rule P004 – Form Requests for Validation

- **Objective**: Separate validation logic from business logic.
- **Details**:
    - Use dediated `FormRequest` classes for request validation.
    - Avoid calling `$request->validate()` inside controller actions.
- **Applies to**: PHP/Laravel
- **Tool**: Manual Review, PHPStan
- **Principle**: MAINTAINABILITY
- **Version**: 1.0
- **Status**: activated
- **Severity**: major

### 📘 Rule P005 – Typed Properties and Return Types

- **Objective**: Increase code reliability and allow static analysis to catch errors.
- **Details**:
    - Use type hints for class properties (PHP 7.4+).
    - Always declare return types for methods, including `void`.
- **Applies to**: PHP 7.4+
- **Tool**: PHPStan, Psalm
- **Principle**: CODE_QUALITY, RELIABILITY
- **Version**: 1.0
- **Status**: activated
- **Severity**: major

**Good example**:
```php
public function updateStatus(int $id, string $status): void
{
    $item = Item::findOrFail($id);
    $item->status = $status;
    $item->save();
}
```

### 📘 Rule P006 – Prevent N+1 with Eager Loading

- **Objective**: Optimize performance by reducing the number of database queries.
- **Details**:
    - Use the `with()` method when accessing associations in a loop.
- **Applies to**: PHP/Laravel
- **Tool**: Laravel Telescope, Clockwork, laravel-query-detector
- **Principle**: PERFORMANCE
- **Version**: 1.0
- **Status**: activated
- **Severity**: critical

**Good example**:
```php
$posts = Post::with('author')->get();
foreach ($posts as $post) {
    echo $post->author->name;
}
```

**Bad example**:
```php
$posts = Post::all();
foreach ($posts as $post) {
    echo $post->author->name; // Trigger new query for each post
}
```
