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-try-catch-specific.md

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

Catch Specific Exceptions

Always catch the most specific exception type possible. Never catch generic \Exception or \Throwable unless at the top-level error boundary.

Bad Example

<?php

declare(strict_types=1);

// Catches everything - hides bugs
try {
    $user = $repository->find($id);
    $mailer->sendWelcome($user);
    $logger->info('User welcomed');
} catch (\Exception $e) {
    // Was it a DB error? Mail error? A typo causing TypeError?
    // All swallowed silently
    return null;
}

// Even worse - catching Throwable swallows fatal errors
try {
    $result = $service->process($data);
} catch (\Throwable $e) {
    // This catches Error (type errors, OOM) - dangerous
    return 'Something went wrong';
}

Good Example

<?php

declare(strict_types=1);

// Catch specific exceptions with appropriate handling
try {
    $user = $repository->find($id);
    $mailer->sendWelcome($user);
} catch (UserNotFoundException $e) {
    $logger->warning('User not found', ['id' => $id]);
    return null;
} catch (MailerException $e) {
    // Email failure shouldn't block the flow - log and continue
    $logger->error('Welcome email failed', [
        'user_id' => $id,
        'error' => $e->getMessage(),
    ]);
}

// Multi-catch for same handling (PHP 8.0+)
try {
    $data = $api->fetch($endpoint);
} catch (ConnectionException | TimeoutException $e) {
    $logger->error('API unreachable', ['error' => $e->getMessage()]);
    throw new ServiceUnavailableException('External service down', previous: $e);
}

// Top-level boundary is the only place for broad catches
// e.g., in error handler, middleware, or command bus
try {
    $response = $kernel->handle($request);
} catch (\Throwable $e) {
    $logger->critical('Unhandled exception', [
        'exception' => $e::class,
        'message' => $e->getMessage(),
        'trace' => $e->getTraceAsString(),
    ]);
    $response = new Response('Internal Server Error', 500);
}

Why

  • No Hidden Bugs: Unexpected exceptions bubble up instead of being silently swallowed
  • Appropriate Responses: Different errors get different handling (404 vs 500 vs retry)
  • Better Debugging: When something breaks, you see the actual error
  • Multi-Catch: PHP 8.0+ catch (A | B $e) groups exceptions with same handling
  • Preserve Context: Use previous: $e when re-throwing to keep the full chain

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