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.

rulesmodern-enums-methods.md

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

Enum Methods

Add methods to enums to encapsulate related behavior and logic.

Bad Example

<?php

declare(strict_types=1);

enum UserRole: string
{
    case Admin = 'admin';
    case Editor = 'editor';
    case Viewer = 'viewer';
}

// Logic scattered outside the enum
class PermissionChecker
{
    public function canEdit(UserRole $role): bool
    {
        return $role === UserRole::Admin || $role === UserRole::Editor;
    }

    public function canDelete(UserRole $role): bool
    {
        return $role === UserRole::Admin;
    }

    public function canView(UserRole $role): bool
    {
        return true; // All roles can view
    }

    public function getLabel(UserRole $role): string
    {
        return match ($role) {
            UserRole::Admin => 'Administrator',
            UserRole::Editor => 'Content Editor',
            UserRole::Viewer => 'Read-only Viewer',
        };
    }
}

Good Example

<?php

declare(strict_types=1);

enum UserRole: string
{
    case Admin = 'admin';
    case Editor = 'editor';
    case Viewer = 'viewer';

    // Permission methods directly on the enum
    public function canEdit(): bool
    {
        return match ($this) {
            self::Admin, self::Editor => true,
            self::Viewer => false,
        };
    }

    public function canDelete(): bool
    {
        return $this === self::Admin;
    }

    public function canView(): bool
    {
        return true;
    }

    public function canManageUsers(): bool
    {
        return $this === self::Admin;
    }

    // Human-readable label
    public function label(): string
    {
        return match ($this) {
            self::Admin => 'Administrator',
            self::Editor => 'Content Editor',
            self::Viewer => 'Read-only Viewer',
        };
    }

    // Get permissions array
    public function permissions(): array
    {
        return match ($this) {
            self::Admin => ['view', 'edit', 'delete', 'manage_users'],
            self::Editor => ['view', 'edit'],
            self::Viewer => ['view'],
        };
    }

    // Static factory methods
    public static function default(): self
    {
        return self::Viewer;
    }

    public static function fromPermissionLevel(int $level): self
    {
        return match (true) {
            $level >= 100 => self::Admin,
            $level >= 50 => self::Editor,
            default => self::Viewer,
        };
    }
}

// More complex enum with interface implementation
interface Describable
{
    public function description(): string;
}

enum OrderStatus: string implements Describable
{
    case Pending = 'pending';
    case Processing = 'processing';
    case Shipped = 'shipped';
    case Delivered = 'delivered';
    case Cancelled = 'cancelled';

    public function description(): string
    {
        return match ($this) {
            self::Pending => 'Order is awaiting processing',
            self::Processing => 'Order is being prepared',
            self::Shipped => 'Order has been shipped',
            self::Delivered => 'Order has been delivered',
            self::Cancelled => 'Order was cancelled',
        };
    }

    public function isFinal(): bool
    {
        return match ($this) {
            self::Delivered, self::Cancelled => true,
            default => false,
        };
    }

    public function canTransitionTo(self $newStatus): bool
    {
        return match ($this) {
            self::Pending => in_array($newStatus, [self::Processing, self::Cancelled]),
            self::Processing => in_array($newStatus, [self::Shipped, self::Cancelled]),
            self::Shipped => $newStatus === self::Delivered,
            self::Delivered, self::Cancelled => false,
        };
    }

    public function color(): string
    {
        return match ($this) {
            self::Pending => '#FFA500',
            self::Processing => '#0000FF',
            self::Shipped => '#800080',
            self::Delivered => '#008000',
            self::Cancelled => '#FF0000',
        };
    }
}

// Usage
$role = UserRole::Editor;
if ($role->canEdit()) {
    // Edit content
}

$status = OrderStatus::Processing;
if ($status->canTransitionTo(OrderStatus::Shipped)) {
    // Allow status change
}

Why

  • Encapsulation: Behavior lives with the data it operates on
  • Single Source of Truth: All enum-related logic in one place
  • Type Safety: Methods have access to $this for the current case
  • Exhaustive Matching: Static analysis ensures all cases are handled
  • Interface Support: Enums can implement interfaces
  • Clean Architecture: No need for separate helper classes

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