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-finally-cleanup.md

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

Finally for Cleanup

Use finally blocks to guarantee cleanup code runs whether an exception occurs or not.

Bad Example

<?php

declare(strict_types=1);

function processFile(string $path): array
{
    $handle = fopen($path, 'r');

    try {
        $data = parseContents($handle);
        fclose($handle); // Skipped if parseContents throws
        return $data;
    } catch (ParseException $e) {
        fclose($handle); // Duplicated cleanup
        throw $e;
    }
    // If an unexpected exception occurs, handle is never closed
}

// Lock without guaranteed release
function updateInventory(int $productId, int $quantity): void
{
    $lock = Cache::lock("product:{$productId}", 10);
    $lock->get();

    $product = Product::find($productId);
    $product->stock -= $quantity;
    $product->save();

    $lock->release(); // Never called if save() throws
}

Good Example

<?php

declare(strict_types=1);

function processFile(string $path): array
{
    $handle = fopen($path, 'r');

    try {
        return parseContents($handle);
    } finally {
        fclose($handle); // Always runs, even if exception thrown
    }
}

// Lock with guaranteed release
function updateInventory(int $productId, int $quantity): void
{
    $lock = Cache::lock("product:{$productId}", 10);
    $lock->get();

    try {
        $product = Product::find($productId);
        $product->stock -= $quantity;
        $product->save();
    } finally {
        $lock->release(); // Always released
    }
}

// Database transaction with guaranteed cleanup
function transferFunds(Account $from, Account $to, float $amount): void
{
    $pdo = getConnection();
    $pdo->beginTransaction();

    try {
        $from->debit($amount);
        $to->credit($amount);
        $pdo->commit();
    } catch (\Throwable $e) {
        $pdo->rollBack();
        throw $e;
    }
}

// Temporary state restoration
function withLocale(string $locale, callable $callback): mixed
{
    $original = setlocale(LC_ALL, '0');

    try {
        setlocale(LC_ALL, $locale);
        return $callback();
    } finally {
        setlocale(LC_ALL, $original); // Always restored
    }
}

Why

  • Guaranteed Execution: finally runs whether try succeeds, catch fires, or exception propagates
  • No Duplication: Write cleanup once instead of in both try and catch
  • Resource Safety: File handles, locks, connections always released
  • State Restoration: Temporary changes always reverted

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