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-nullable-types.md

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

Nullable Types

Use nullable types explicitly when null is a valid value.

Bad Example

<?php

declare(strict_types=1);

class UserService
{
    // Unclear if null is valid
    public function findByEmail(string $email)
    {
        $data = $this->repository->findByEmail($email);
        if (!$data) {
            return null; // Surprise! Returns null
        }
        return new User($data);
    }

    // Default null but no nullable type
    public function setMiddleName(string $name = null): void
    {
        // This works but is confusing and deprecated pattern
        $this->middleName = $name;
    }

    // Returning null from non-nullable return type
    public function getActiveSubscription(): Subscription
    {
        // Bug: returns null but type says Subscription
        return $this->subscriptions->getActive();
    }
}

Good Example

<?php

declare(strict_types=1);

class UserService
{
    // Explicit nullable return type
    public function findByEmail(string $email): ?User
    {
        $data = $this->repository->findByEmail($email);
        return $data ? new User($data) : null;
    }

    // Explicit nullable parameter with modern syntax
    public function setMiddleName(?string $name): void
    {
        $this->middleName = $name;
    }

    // Clear that subscription might not exist
    public function getActiveSubscription(): ?Subscription
    {
        return $this->subscriptions->getActive();
    }

    // Alternative: throw exception when null is not acceptable
    public function getActiveSubscriptionOrFail(): Subscription
    {
        $subscription = $this->subscriptions->getActive();

        if ($subscription === null) {
            throw new NoActiveSubscriptionException();
        }

        return $subscription;
    }
}

// Proper null handling in calling code
$user = $userService->findByEmail($email);
if ($user !== null) {
    $user->sendWelcomeEmail();
}

// Or with null coalescing
$displayName = $user?->getDisplayName() ?? 'Guest';

Why

  • Explicit Intent: Makes null as a valid value intentional and clear
  • Null Safety: Forces callers to handle the null case
  • No Surprises: Eliminates unexpected null returns
  • IDE Support: IDEs warn about potential null pointer access
  • Static Analysis: Tools catch null-related bugs before runtime
  • Modern Syntax: Use ?Type instead of Type|null for brevity

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