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.

rulespsr-naming-classes.md

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

Class Naming Conventions

Use PascalCase for class names with descriptive, intention-revealing names.

Bad Example

<?php

declare(strict_types=1);

// Wrong: Lowercase, underscores
class user_service
{
}

// Wrong: Abbreviations
class UsrMgr
{
}

// Wrong: Generic names
class Manager
{
}

class Data
{
}

class Helper
{
}

// Wrong: Verb as class name (sounds like method)
class GetUser
{
}

// Wrong: Inconsistent suffixes
class UserProcess
{
}

class OrderHandler
{
}

class ProductManager
{
}

Good Example

<?php

declare(strict_types=1);

// Domain entities - nouns
class User
{
}

class Order
{
}

class Product
{
}

// Services - NounService pattern
class UserService
{
}

class OrderService
{
}

class PaymentService
{
}

// Repositories - NounRepository pattern
class UserRepository
{
}

class OrderRepository
{
}

// Controllers - NounController pattern
class UserController
{
}

class OrderController
{
}

// Factories - NounFactory pattern
class UserFactory
{
}

class OrderFactory
{
}

// Commands/Actions - VerbNounCommand pattern
class CreateUserCommand
{
}

class ProcessPaymentCommand
{
}

class SendEmailCommand
{
}

// Handlers - NounHandler pattern
class CreateUserHandler
{
}

class PaymentFailedHandler
{
}

// Events - PastTenseNoun pattern
class UserCreated
{
}

class OrderShipped
{
}

class PaymentFailed
{
}

// Exceptions - DescriptiveException pattern
class UserNotFoundException
{
}

class InvalidPaymentMethodException
{
}

class InsufficientFundsException
{
}

// Value Objects - descriptive nouns
class EmailAddress
{
}

class Money
{
}

class DateRange
{
}

// Interfaces - Adjective-able or Noun pattern
interface Cacheable
{
}

interface Serializable
{
}

interface UserRepository
{
}

interface PaymentGateway
{
}

// Abstract classes - AbstractNoun or BaseNoun
abstract class AbstractRepository
{
}

abstract class BaseController
{
}

Naming Patterns by Type

<?php

declare(strict_types=1);

namespace App\Domain\Order;

// Entity - simple noun
class Order
{
    public function __construct(
        private OrderId $id,
        private CustomerId $customerId,
        private OrderStatus $status,
    ) {}
}

// Value Object - descriptive noun
readonly class OrderId
{
    public function __construct(
        public string $value,
    ) {}
}

// Enum - singular noun
enum OrderStatus: string
{
    case Pending = 'pending';
    case Completed = 'completed';
}

// Repository Interface - NounRepository
interface OrderRepository
{
    public function find(OrderId $id): ?Order;
    public function save(Order $order): void;
}

// Service - NounService
class OrderService
{
    public function __construct(
        private OrderRepository $repository,
    ) {}
}

// Event - past tense
readonly class OrderCreated
{
    public function __construct(
        public OrderId $orderId,
        public DateTimeImmutable $occurredAt,
    ) {}
}

// Command - imperative
readonly class CreateOrder
{
    public function __construct(
        public CustomerId $customerId,
        public array $items,
    ) {}
}

Why

  • Clarity: PascalCase visually distinguishes classes from variables/functions
  • Consistency: Standard naming patterns make code predictable
  • Discoverability: Suffixes (Service, Repository) indicate purpose
  • Communication: Names reveal intent to other developers
  • IDE Support: Consistent naming improves autocompletion
  • PSR Compliance: Follows PHP-FIG recommendations

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