All skills
asyrafhussin avatar

/php-best-practices

@e7ea05d

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".

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/php-best-practices

This session only. Nothing lands on disk.

ruleserror-exception-hierarchy.md

≈867 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Exception Hierarchy

Organize exceptions into a meaningful hierarchy so callers can catch at different levels of specificity.

Bad Example

<?php

declare(strict_types=1);

// Flat, unrelated exceptions - no hierarchy
class UserNotFoundException extends \Exception {}
class OrderNotFoundException extends \Exception {}
class ProductNotFoundException extends \Exception {}
class InvalidEmailException extends \Exception {}
class InvalidPriceException extends \Exception {}
class DatabaseConnectionException extends \Exception {}
class ApiTimeoutException extends \Exception {}

// Caller must catch each one individually
try {
    $order = $service->processOrder($data);
} catch (UserNotFoundException $e) {
    // handle
} catch (OrderNotFoundException $e) {
    // handle (same logic as above)
} catch (ProductNotFoundException $e) {
    // handle (same logic again)
}

Good Example

<?php

declare(strict_types=1);

// Base exception for the application
class AppException extends \RuntimeException {}

// Not-found family
class NotFoundException extends AppException
{
    public function __construct(
        private readonly string $entity,
        private readonly string|int $identifier,
    ) {
        parent::__construct("{$entity} not found: {$identifier}");
    }

    public function getEntity(): string
    {
        return $this->entity;
    }
}

class UserNotFoundException extends NotFoundException
{
    public function __construct(string|int $id)
    {
        parent::__construct('User', $id);
    }
}

class OrderNotFoundException extends NotFoundException
{
    public function __construct(string|int $id)
    {
        parent::__construct('Order', $id);
    }
}

// Validation family
class ValidationException extends AppException
{
    /** @param array<string, string> $errors */
    public function __construct(
        private readonly array $errors = [],
    ) {
        parent::__construct('Validation failed');
    }

    /** @return array<string, string> */
    public function getErrors(): array
    {
        return $this->errors;
    }
}

// Infrastructure family
class InfrastructureException extends AppException {}
class DatabaseException extends InfrastructureException {}
class ExternalApiException extends InfrastructureException {}

// Callers can catch at any level
try {
    $order = $service->processOrder($data);
} catch (NotFoundException $e) {
    // Catches UserNotFound, OrderNotFound, ProductNotFound
    return response()->json(['error' => $e->getMessage()], 404);
} catch (ValidationException $e) {
    return response()->json(['errors' => $e->getErrors()], 422);
} catch (InfrastructureException $e) {
    // Catches Database, ExternalApi - log and show generic error
    $logger->error($e->getMessage());
    return response()->json(['error' => 'Service unavailable'], 503);
}

Why

  • Layered Catching: Catch broad categories or specific exceptions as needed
  • DRY Error Handling: One catch block for all "not found" cases
  • Consistent Structure: Shared base provides common interface
  • Extensible: Add new exceptions without changing existing catch blocks
  • Use RuntimeException: Extend \RuntimeException for errors that can't be recovered from programmatically

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill is a comprehensive and safe resource for PHP 8.x best practices. It provides structured guidance on type safety, modern PHP syntax, PSR standards, and secure coding practices (such as prepared statements and password hashing). The agent is instructed to use standard environment detection commands (php -v, grep) to tailor its advice. No malicious patterns or security risks were identified.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    39 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at e7ea05d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
Other metadata
metadata
{
  "author": "php-community",
  "version": "2.1.0",
  "phpVersion": "8.0 - 8.5"
}

README badge

README badge for asyrafhussin/agent-skills/php-best-practices