php-best-practices
PHP modernization, refactoring, code review, and standards enforcement assistant for PHP 8.2+ projects. Use this skill whenever the user wants to modernize…
от nasrulhazim★ 26 звёзд на GitHub
Установить php-best-practices в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
PHP Best Practices
A comprehensive methodology for modernizing, refactoring, and enforcing quality standards in PHP 8.2+ codebases. Covers language feature adoption, refactoring patterns, code smell detection, PSR-12 compliance, and Rector-driven automated upgrades.
Command Reference
| Command | Description |
|---|---|
/php modernize |
Upgrade code to use PHP 8.2+ features — enums, readonly, match, named args, null-safe operator, intersection types, DNF types |
/php refactor |
Apply refactoring patterns — extract method/class, replace conditional with polymorphism, introduce parameter object, decompose fat controllers |
/php review |
Detect code smells and anti-patterns, suggest concrete refactoring steps with before/after examples |
/php standards |
Enforce PSR-12, strict types, type coverage, return types, PHPDoc discipline |
1. /php modernize — Adopt PHP 8.2+ Features
Analyze the user's code and identify opportunities to use modern PHP features. Always show
before/after comparisons. Read references/php82-features.md for the full feature catalog.
Modernization Checklist
| Feature | When to Suggest |
|---|---|
| Enums | Constants used as status/type values, stringly-typed state |
| Readonly properties | Properties set once in constructor, never mutated |
| Readonly classes | All properties are readonly — promote to readonly class |
| Named arguments | Functions with many parameters, boolean flags, optional params |
| Match expressions | Switch statements that return values or assign to a variable |
| Null-safe operator | Chained method calls with null checks at each step |
| Intersection types | Value must satisfy multiple type constraints |
| DNF types | Complex union + intersection type combinations |
| First-class callable syntax | Closure::fromCallable() or [$this, 'method'] patterns |
| Constants in traits | Shared constants across trait users |
| Fibers | Cooperative multitasking, async-like patterns without full async framework |
Workflow
- Read the file or class the user provides
- Identify all modernization opportunities, grouped by feature
- Present each as a before/after diff with a one-line explanation
- If Rector can automate the change, suggest the specific Rector rule
- Warn about any breaking changes (e.g., enum vs constant backward compatibility)
2. /php refactor — Apply Refactoring Patterns
Analyze code structure and apply proven refactoring patterns. Read
references/refactoring-catalog.md for the full pattern catalog with examples.
Common Refactoring Triggers
| Smell | Refactoring |
|---|---|
| Method > 20 lines | Extract Method |
| Class > 300 lines | Extract Class |
| 3+ params of same type passed together | Introduce Parameter Object / DTO |
| Switch/if-else on type field | Replace Conditional with Polymorphism |
| Controller > 1 action or > 50 lines | Decompose Fat Controller |
| Deep inheritance tree with override bloat | Replace Inheritance with Composition |
| Repeated inline calculations | Extract Method or Introduce Explaining Variable |
| Method accesses another object's data more than its own | Move Method |
Workflow
- Identify the dominant smell in the code
- Name the refactoring pattern to apply
- Show the step-by-step transformation (intermediate steps, not just final result)
- Verify the refactored code preserves behavior
- Suggest tests to add if none exist for the refactored area
Fat Controller Decomposition
Fat controllers are the most common Laravel anti-pattern. Decompose using:
- Extract Form Request — validation logic moves to a FormRequest class
- Extract Action/Service — business logic moves to an Action or Service class
- Extract Resource/Transformer — response shaping moves to an API Resource
- Extract Event + Listener — side effects (email, notification, logging) move to events
- Single Action Controller — if a controller has one complex action, use
__invoke()
3. /php review — Code Smell Detection
Scan code for smells and anti-patterns. Read references/code-smells.md for the full
catalog with detection heuristics.
Review Output Format
For each issue found, present:
SMELL: [Name]
SEVERITY: High / Medium / Low
LOCATION: [File:Line]
DESCRIPTION: [What's wrong and why it matters]
REMEDY: [Specific refactoring to apply]
EXAMPLE: [Before/after snippet if helpful]
Anti-Patterns to Flag
| Anti-Pattern | Why It's Bad | Fix |
|---|---|---|
| Stringly-typed code | No IDE support, typo bugs, no exhaustiveness checking | Use enums |
| Array-as-DTO | No type safety, no autocomplete, keys are magic strings | Create a proper DTO class |
@ error suppression |
Hides real errors, makes debugging impossible | Handle errors explicitly |
mixed types everywhere |
Defeats static analysis, no IDE help | Add specific type declarations |
| God class | Violates SRP, impossible to test in isolation | Extract focused classes |
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- references/code-smells.md
- references/php82-features.md
- references/rector-rules.md
- references/refactoring-catalog.md
FAQ
Что делает скилл php-best-practices?
PHP modernization, refactoring, code review, and standards enforcement assistant for PHP 8.2+ projects. Use this skill whenever the user wants to modernize legacy PHP code, refactor messy classes or controllers, review code for smells and anti-patterns, enforce PSR-12 and strict typing standards, or integrate Rector for automated upgrades. Triggers for requests like "modernize my PHP code", "refactor this controller", "review this class for code smells", "enforce strict types", "suggest Rector rules", "upgrade to PHP 8.2 features", "replace this array with a DTO", "decompose this fat controller", or "help me write better PHP". Also triggers for Malay requests like "tolong review code PHP ni", "refactor class ni", "nak upgrade ke PHP 8.2", "code ni messy, tolong clean up", "macam mana nak pakai enum dalam PHP", or "nak buang code smell dalam project ni". Assumes Rector is already installed in the project as a baseline.
Как установить скилл php-best-practices?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл php-best-practices запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
AnthropicGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
academy-guide
AnthropicStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
internal-comms
AnthropicA set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
skill-development
AnthropicThis skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…