Php best practices

From AsyrafHussin/agent-skills · View interactive version

Php best practices is an AI agent skill from the GitHub repository AsyrafHussin/agent-skills. PHP 8.x 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".

How to use this skill

Skill file

PHP Best Practices

Modern PHP 8.x patterns, PSR standards, type system best practices, and SOLID principles. Contains 51 rules for writing clean, maintainable PHP code.

Step 1: Detect PHP Version

Always check the project's PHP version before giving any advice. Features vary significantly across 8.0 - 8.5. Never suggest syntax that doesn't exist in the project's version.

Check composer.json for the required PHP version:

{ "require": { "php": "^8.1" } }   // -> 8.1 rules and below
{ "require": { "php": "^8.3" } }   // -> 8.3 rules and below
{ "require": { "php": ">=8.4" } }  // -> 8.4 rules and below

Also check the runtime version:

php -v   # e.g. PHP 8.3.12

Feature Availability by Version

| Feature | Version | Rule Prefix |

|---------|---------|-------------|

| Union types, match, nullsafe, named args, constructor promotion, attributes | 8.0+ | type-, modern- |

| Enums, readonly properties, intersection types, first-class callables, never, fibers | 8.1+ | modern- |

| Readonly classes, DNF types, true/false/null standalone types | 8.2+ | modern- |

| Typed class constants, #[\Override], json_validate() | 8.3+ | modern- |

| Property hooks, asymmetric visibility, #[\Deprecated], new without parens | 8.4+ | modern- |

| Pipe operator |> | 8.5+ | modern- |

Only suggest features available in the detected version. If the user asks about upgrading or newer features, mention what becomes available at each version.

When to Apply

Reference these guidelines when:

Rule Categories by Priority

| Priority | Category | Impact | Prefix | Rules |

|----------|----------|--------|--------|-------|

| 1 | Type System | CRITICAL | type- | 9 |

| 2 | Modern PHP Features | CRITICAL | modern- | 16 |

| 3 | PSR Standards | HIGH | psr- | 6 |

| 4 | SOLID Principles | HIGH | solid- | 5 |

| 5 | Error Handling | HIGH | error- | 5 |

| 6 | Performance | MEDIUM | perf- | 5 |

| 7 | Security | CRITICAL | sec- | 5 |

Quick Reference

1. Type System (CRITICAL) — 9 rules

2. Modern PHP Features (CRITICAL) — 16 rules

8.0+:

8.1+:

8.2+:

8.3+:

8.4+:

8.5+:

3. PSR Standards (HIGH) — 6 rules

4. SOLID Principles (HIGH) — 5 rules

5. Error Handling (HIGH) — 5 rules

6. Performance (MEDIUM) — 5 rules

7. Security (CRITICAL) — 5 rules

Essential Guidelines

For detailed examples and explanations, see the rule files:

Key Patterns (Quick Reference)

<?php
declare(strict_types=1);

// 8.0+ Constructor promotion + readonly (8.1+)
class User
{
    public function __construct(
        public readonly string $id,
        private string $email,
    ) {}
}

// 8.1+ 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',
        };
    }
}

// 8.0+ Match expression
$result = match($status) {
    'pending' => 'Waiting',
    'active' => 'Running',
    default => 'Unknown',
};

// 8.0+ Nullsafe operator
$country = $user?->getAddress()?->getCountry();

// 8.3+ Typed class constants + #[\Override]
class PaymentService extends BaseService
{
    public const string GATEWAY = 'stripe';

    #[\Override]
    public function process(): void { /* ... */ }
}

// 8.4+ Property hooks + asymmetric visibility
class Product
{
    public string $name { set => trim($value); }
    public private(set) float $price;
}

// 8.5+ Pipe operator
$result = $input
    |> trim(...)
    |> strtolower(...)
    |> htmlspecialchars(...);

Output Format

When auditing code, output findings in this format:

file:line - [category] Description of issue

Example:

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 responsibilities

How to Use

Read individual rule files for detailed explanations:

rules/modern-constructor-promotion.md
rules/type-strict-mode.md
rules/solid-srp.md
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.