Aller au contenu

Conventions Backend

Conventions de code à respecter pour le développement backend Laravel / DDD de Primatch.


Nommage

Élément Convention Exemple
Controllers {Resource}Controller GameController
Form Requests {Verb}{Resource}Request CreateGameRequest, UpdateScoreRequest
Resources {Resource}Resource GameResource, UserResource
Services {Feature}Service GameService
Repositories (interface) {Resource}RepositoryInterface GameRepositoryInterface
Repositories (impl.) Eloquent{Resource}Repository EloquentGameRepository
DTOs {Verb}{Resource}DTO CreateGameDTO, ScoreDTO
Events {Resource}{Action} GameCompleted, ScoreValidated
Jobs {Action}{Resource} SendGameNotification

Structure d'un nouveau domaine

app/Domain/{Feature}/
├── DTOs/
   └── Create{Feature}DTO.php
├── Events/
   └── {Feature}Created.php
├── Models/           # Entités domaine (pas Eloquent)
   └── {Feature}.php
├── Repositories/
   └── {Feature}RepositoryInterface.php
└── Services/
    └── {Feature}Service.php

app/Infrastructure/Repositories/
└── Eloquent{Feature}Repository.php

Exemple : Service DDD

<?php

namespace App\Domain\Game\Services;

use App\Domain\Game\DTOs\CreateGameDTO;
use App\Domain\Game\Repositories\GameRepositoryInterface;
use App\Domain\Game\Events\GameCreated;

final class GameService
{
    public function __construct(
        private readonly GameRepositoryInterface $gameRepository,
    ) {}

    public function createGame(CreateGameDTO $dto): Game
    {
        // ✅ Logique métier ici, pas dans le controller
        $game = $this->gameRepository->create($dto);

        // ✅ Dispatch d'un event domaine
        event(new GameCreated($game));

        return $game;
    }
}

Règles PHPStan (niveau 6)

  • Typage strict : toujours typer les paramètres et retours de méthodes
  • Pas de mixed sauf cas exceptionnel justifié
  • Utiliser readonly pour les propriétés immuables (DTOs, Value Objects)

Commandes utiles

make phpstan        # Analyse statique niveau 6
make lint           # Laravel Pint (formatage)
make lint-fix       # Correction automatique
make test-arch      # Tests d'architecture DDD boundaries