home / skills / hoangnguyen0403 / agent-skills-standard / best-practices
This skill helps you implement PHP best practices with PSR-12, autoloading, SOLID design, and clean code structure for maintainable apps.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill best-practicesReview the files below or copy the command above to add this skill to your agents.
---
name: PHP Best Practices
description: PHP best practices, PSR standards, and code quality guidelines.
metadata:
labels: [php, psr, best-practices]
triggers:
files: ['**/*.php']
keywords: [psr-12, camelCase, PascalCase, dry, solid]
---
# PHP Best Practices
## **Priority: P1 (HIGH)**
## Structure
```text
src/
├── {Domain}/ # e.g., Services, Repositories
└── Helpers/ # Pure functions/Traits
```
## Implementation Guidelines
- **PSR-12**: Follow PSR-12 for coding style (indent, braces, space).
- **Autoloading**: Use PSR-4 via Composer.
- **Namespacing**: Map namespaces to directory structure.
- **PascalCase**: Use for all class names.
- **camelCase**: Use for methods and variables.
- **SNAKE_CASE**: Use for class constants.
- **DRY Logic**: Extract repetitive logic into traits/methods.
- **SOLID**: Use interfaces for decoupling and SRP adherence.
## Anti-Patterns
- **God Classes**: **No Monoliths**: Avoid classes with multiple responsibilities.
- **Magic Numbers**: **No Hardcoding**: Use class constants for config values.
- **Deep Nesting**: **No Nesting**: Guard clauses over nested if/else.
- **Direct Output**: **No Echo**: Return data; let controller handle response.
## References
- [Clean Code Patterns](references/implementation.md)
This skill codifies PHP best practices, PSR standards, and code quality guidelines to help agents produce clean, maintainable PHP code. It focuses on project structure, naming conventions, autoloading, and design principles that reduce technical debt and improve readability. The guidance is practical and aimed at real-world PHP projects following modern standards.
The skill inspects code and project layout against PSR-12 and PSR-4 conventions, checks naming and casing rules, and flags anti-patterns such as god classes, magic numbers, deep nesting, and direct output in business logic. It recommends extracting reusable logic into helpers or traits and using interfaces to enforce SOLID design. It outputs concrete remediation steps and examples for quick fixes.
Do I have to follow PSR-12 and PSR-4 exactly?
Aim to follow PSR-12 and PSR-4 for consistency; minor deviations can be tolerated for legacy constraints, but document exceptions and migrate when feasible.
When should I use traits versus standalone helpers?
Use traits for reusable behavior that logically belongs to an object; use pure helper functions for stateless utilities that don't fit an object context.