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.

rulestype-intersection-types.md

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

Intersection Types

Use intersection types when a value must implement multiple interfaces (PHP 8.1+).

Bad Example

<?php

declare(strict_types=1);

interface Cacheable
{
    public function getCacheKey(): string;
}

interface Encodable
{
    public function encode(): string;
}

class CacheService
{
    /**
     * @param Cacheable&Encodable $item
     */
    public function store($item): void
    {
        // No type enforcement - relies on docblock
        // Could receive object implementing only one interface
        $key = $item->getCacheKey();
        $data = $item->encode();
        $this->cache->set($key, $data);
    }
}

Good Example

<?php

declare(strict_types=1);

interface Cacheable
{
    public function getCacheKey(): string;
    public function getCacheTtl(): int;
}

interface Encodable
{
    public function encode(): string;
    public function decode(string $data): void;
}

class CacheService
{
    // Object MUST implement both interfaces
    public function store(Cacheable&Encodable $item): void
    {
        $key = $item->getCacheKey();
        $data = $item->encode();
        $ttl = $item->getCacheTtl();

        $this->cache->set($key, $data, $ttl);
    }

    public function retrieve(
        string $key,
        Cacheable&Encodable $prototype
    ): Cacheable&Encodable {
        $data = $this->cache->get($key);
        $prototype->decode($data);
        return $prototype;
    }
}

// Implementation example
class User implements Cacheable, Encodable
{
    public function __construct(
        private int $id,
        private string $name
    ) {}

    public function getCacheKey(): string
    {
        return "user:{$this->id}";
    }

    public function getCacheTtl(): int
    {
        return 3600;
    }

    public function encode(): string
    {
        return json_encode(['id' => $this->id, 'name' => $this->name]);
    }

    public function decode(string $data): void
    {
        $decoded = json_decode($data, true);
        $this->id = $decoded['id'];
        $this->name = $decoded['name'];
    }
}

Why

  • Compound Requirements: Enforces multiple interface implementations
  • Type Safety: PHP enforces all required capabilities at class loading
  • Composition: Enables type-safe composition over inheritance
  • Clear Contracts: Explicitly states all required behaviors
  • Better Than Base Classes: More flexible than requiring a specific base class
  • Static Analysis: Full support in PHPStan and Psalm

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