php-best-practices
Original:🇺🇸 English
Translated
PHP 8.5+ modern patterns, PSR standards, and SOLID principles. Use when reviewing PHP code, checking type safety, auditing code quality, or ensuring PHP best practices. Triggers on "review PHP", "check PHP code", "audit PHP", or "PHP best practices".
17installs
Added on
NPX Install
npx skill4agent add asyrafhussin/agent-skills php-best-practicesTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →PHP Best Practices
Modern PHP 8.x patterns, PSR standards, type system best practices, and SOLID principles. Contains 45+ rules for writing clean, maintainable PHP code.
When to Apply
Reference these guidelines when:
- Writing or reviewing PHP code
- Implementing classes and interfaces
- Using PHP 8.x modern features
- Ensuring type safety
- Following PSR standards
- Applying design patterns
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Type System | CRITICAL | |
| 2 | Modern Features | CRITICAL | |
| 3 | PSR Standards | HIGH | |
| 4 | SOLID Principles | HIGH | |
| 5 | Error Handling | HIGH | |
| 6 | Performance | MEDIUM | |
| 7 | Security | CRITICAL | |
Quick Reference
1. Type System (CRITICAL)
- - Declare strict types
type-strict-mode - - Always declare return types
type-return-types - - Type all parameters
type-parameter-types - - Type class properties
type-property-types - - Use union types effectively
type-union-types - - Use intersection types
type-intersection-types - - Handle nullable types properly
type-nullable - - Avoid mixed type when possible
type-mixed-avoid - - Use void for no-return methods
type-void-return - - Use never for non-returning functions
type-never-return
2. Modern Features (CRITICAL)
- - Use constructor property promotion
modern-constructor-promotion - - Use readonly for immutable data
modern-readonly-properties - - Use readonly classes
modern-readonly-classes - - Use enums instead of constants
modern-enums - - Use attributes for metadata
modern-attributes - - Use match over switch
modern-match-expression - - Use named arguments for clarity
modern-named-arguments - - Use nullsafe operator (?->)
modern-nullsafe-operator - - Use arrow functions for simple closures
modern-arrow-functions - - Use first-class callable syntax
modern-first-class-callables
3. PSR Standards (HIGH)
- - Follow PSR-4 autoloading
psr-4-autoloading - - Follow PSR-12 coding style
psr-12-coding-style - - Class and method naming
psr-naming-conventions - - One class per file
psr-file-structure - - Proper namespace usage
psr-namespace-declaration
4. SOLID Principles (HIGH)
- - One reason to change
solid-single-responsibility - - Open for extension, closed for modification
solid-open-closed - - Subtypes must be substitutable
solid-liskov-substitution - - Small, focused interfaces
solid-interface-segregation - - Depend on abstractions
solid-dependency-inversion
5. Error Handling (HIGH)
- - Create specific exceptions
error-custom-exceptions - - Proper exception inheritance
error-exception-hierarchy - - Catch specific exceptions
error-try-catch-specific - - Use finally for cleanup
error-finally-cleanup - - Don't suppress errors with @
error-never-suppress
6. Performance (MEDIUM)
- - Avoid global variables
perf-avoid-globals - - Load resources lazily
perf-lazy-loading - - Use native array functions
perf-array-functions - - Use native string functions
perf-string-functions - - Use generators for large datasets
perf-generators
7. Security (CRITICAL)
- - Validate all input
sec-input-validation - - Escape output properly
sec-output-escaping - - Use password_hash/verify
sec-password-hashing - - Use prepared statements
sec-sql-prepared - - Validate file uploads
sec-file-uploads
Essential Guidelines
For detailed examples and explanations, see the rule files:
- type-strict-mode.md - Strict types declaration
- modern-constructor-promotion.md - Constructor property promotion
- modern-enums.md - PHP 8.1+ enums with methods
- solid-single-responsibility.md - Single responsibility principle
Key Patterns (Quick Reference)
php
<?php
declare(strict_types=1);
// Constructor promotion + readonly
class User
{
public function __construct(
public readonly string $id,
private string $email,
) {}
}
// Enums with methods
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
public function label(): string
{
return match($this) {
self::Active => 'Active',
self::Inactive => 'Inactive',
};
}
}
// Match expression
$result = match($status) {
'pending' => 'Waiting',
'active' => 'Running',
default => 'Unknown',
};
// Nullsafe operator
$country = $user?->getAddress()?->getCountry();
// Arrow functions
$names = array_map(fn(User $u) => $u->name, $users);Output Format
When auditing code, output findings in this format:
file:line - [category] Description of issueExample:
src/Services/UserService.php:15 - [type] Missing return type declaration
src/Models/Order.php:42 - [modern] Use match expression instead of switch
src/Controllers/ApiController.php:28 - [solid] Class has multiple responsibilitiesHow to Use
Read individual rule files for detailed explanations:
rules/modern-constructor-promotion.md
rules/type-strict-mode.md
rules/solid-single-responsibility.md