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-pipe-operator.md

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

Pipe Operator

Use the pipe operator (|>) for readable function chaining (PHP 8.5+).

Bad Example

<?php

declare(strict_types=1);

// Deeply nested function calls - read inside-out
$result = htmlspecialchars(
    strtolower(
        trim(
            $input
        )
    )
);

// Temporary variables - cluttered
$step1 = trim($input);
$step2 = strtolower($step1);
$step3 = htmlspecialchars($step2);
$result = $step3;

// Array processing - hard to follow
$slugs = array_unique(
    array_filter(
        array_map(
            fn(string $tag) => strtolower(trim($tag)),
            explode(',', $tags)
        ),
        fn(string $tag) => $tag !== ''
    )
);

Good Example

<?php

declare(strict_types=1);

// Pipe operator - read left-to-right, top-to-bottom
$result = $input
    |> trim(...)
    |> strtolower(...)
    |> htmlspecialchars(...);

// Chain with first-class callables
$length = "Hello World"
    |> trim(...)
    |> strlen(...);

// Use arrow functions for multi-argument functions
$slugs = $tags
    |> (fn(string $s) => explode(',', $s))
    |> (fn(array $a) => array_map(fn(string $t) => strtolower(trim($t)), $a))
    |> (fn(array $a) => array_filter($a, fn(string $t) => $t !== ''))
    |> array_unique(...);

// Practical example: building a slug
$slug = $title
    |> trim(...)
    |> strtolower(...)
    |> (fn(string $s) => preg_replace('/[^a-z0-9]+/', '-', $s))
    |> (fn(string $s) => trim($s, '-'));

// Practical example: processing user input
$sanitized = $request->input('comment')
    |> trim(...)
    |> strip_tags(...)
    |> htmlspecialchars(...);

Important

The pipe operator passes the left-hand value as the sole argument to the right-hand callable. $x |> foo(...) is equivalent to foo($x). For multi-argument functions, wrap in an arrow function:

// str_replace has 3 args - wrap in arrow function
$clean = $input |> (fn(string $s) => str_replace(' ', '-', $s));

Why

  • Readable Flow: Data transformations read left-to-right, top-to-bottom
  • No Nesting: Eliminates deeply nested function calls
  • No Temp Variables: Avoids cluttering scope with intermediate values
  • Functional Style: Encourages composable, single-purpose functions
  • Sole Argument: $x |> fn(...) is equivalent to fn($x) — wrap in closures for multi-argument functions
  • Pairs with First-Class Callables: trim(...) syntax works naturally with |>

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