home / skills / hoangnguyen0403 / agent-skills-standard / language

language skill

/skills/php/language

This skill enforces modern PHP standards and typing discipline to improve code quality, readability, and maintainability across projects.

npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill language

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.3 KB
---
name: PHP Language Standards
description: Core PHP language standards and modern 8.x features.
metadata:
  labels: [php, language, 8.x]
  triggers:
    files: ['**/*.php']
    keywords: [declare, readonly, match, constructor, promotion, types]
---

# PHP Language Standards

## **Priority: P0 (CRITICAL)**

## Structure

```text
src/
└── {Namespace}/
    └── {Class}.php
```

## Implementation Guidelines

- **Strict Typing**: Declare `declare(strict_types=1);` at file top.
- **Type Hinting**: Apply scalar hints and return types to all members.
- **Modern Types**: Use Union (`string|int`) and Intersection types.
- **Read-only**: Use `readonly` for immutable properties.
- **Constructor Promotion**: Combine declaration and assignment in `__construct`.
- **Match Expressions**: Prefer `match` over `switch` for value returns.
- **Named Arguments**: Use for readability in optional parameters.

## Anti-Patterns

- **No Type Context**: Avoid functions without return or parameter types.
- **Sloppy Comparison**: **No ==**: Use `===` for strict comparison.
- **Legacy Syntax**: **No switch**: Use `match` for simple value mapping.
- **Global Scope**: **No Globals**: Never define logic in global namespace.

## References

- [Modern PHP Patterns](references/implementation.md)

Overview

This skill codifies core PHP language standards focused on modern PHP 8.x features and safe, maintainable code. It provides concrete rules for file layout, typing, modern syntax, and common anti-patterns to ensure consistent, robust PHP code across projects.

How this skill works

The skill inspects PHP source structure and declarations, enforcing strict typing, type hints, and modern type constructs like unions and readonly properties. It flags legacy constructs (loose comparisons, switch statements, global-scope logic) and recommends contemporary alternatives such as match expressions and named arguments for clarity.

When to use it

  • Setting up new PHP projects or modules to ensure consistent architecture
  • Reviewing or refactoring legacy PHP code to modern 8.x standards
  • Code review automation to enforce type safety and syntax best practices
  • Onboarding teams to shared PHP coding conventions
  • Integrating with linters and CI to prevent regressions

Best practices

  • Place PHP classes under src/{Namespace}/{Class}.php to keep a predictable structure
  • Add declare(strict_types=1); at the top of every PHP file to enforce strict typing
  • Annotate parameter and return types everywhere; use scalar and nullable hints consistently
  • Prefer Union types (e.g., string|int) and readonly properties for immutable state
  • Use constructor property promotion to reduce boilerplate in __construct
  • Prefer match expressions for value-based branching and named arguments for optional parameters

Example use cases

  • Automated code review that rejects functions lacking parameter or return types
  • Refactoring a module to replace switch statements with match expressions and strict comparisons
  • Applying readonly and constructor promotion to immutable domain objects
  • Enforcing strict_types at file top across a codebase during CI runs
  • Providing a checklist for developers migrating from PHP 7 to PHP 8.x

FAQ

What file layout should I use for classes?

Use src/{Namespace}/{Class}.php so each class lives in a namespaced file that matches its path.

Why prefer match over switch?

match is expression-based, safer for returning values, and avoids fall-through bugs common with switch.