搜索

PythonReactSEOPDF
GitHub Skill

php-refactoring

Refactor PHP code to improve structure, readability, and maintainability. Use when asked to refactor, restructure, improve code design, or apply design patterns.

claude-php☆ 9 仓库 Stars0 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

PHP Refactoring

Overview

Apply systematic refactoring techniques to improve PHP code structure while preserving behavior. Supports modern PHP 8.1+ features and common design patterns.

Refactoring Catalog

Extract Method

When a code fragment can be grouped together:

// Before
function printReport(): void {
    // print header
    echo "=== Report ===\n";
    echo "Date: " . date('Y-m-d') . "\n";

    // print body
    foreach ($items as $item) {
        echo "{$item['name']}: {$item['value']}\n";
    }
}

// After
function printReport(): void {
    $this->printHeader();
    $this->printBody($items);
}
Replace Conditional with Polymorphism
// Before
function calculateArea(string $shape, float ...$dims): float {
    return match($shape) {
        'circle' => M_PI * $dims[0] ** 2,
        'rectangle' => $dims[0] * $dims[1],
        'triangle' => 0.5 * $dims[0] * $dims[1],
    };
}

// After: Use interface + implementations
interface Shape {
    public function area(): float;
}
Introduce Parameter Object

When multiple parameters travel together:

// Before
function createUser(string $name, string $email, string $role, int $age): User

// After
function createUser(CreateUserRequest $request): User
Use PHP 8.1+ Features
  • Constructor promotion
  • Readonly properties
  • Enums instead of string constants
  • Named arguments for clarity
  • Fibers for async operations
  • First-class callable syntax
  • Intersection types
  • Match expressions

Rules

  1. Never change behavior - refactoring preserves existing functionality
  2. Small steps - make one refactoring at a time
  3. Test between steps - run tests after each change
  4. Commit frequently - each refactoring should be a separate commit
  5. Explain rationale - document why each change improves the code

Scripts

Use scripts/detect-smells.php to identify common code smells in a file or directory.

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-03-11
索引更新时间
2026-03-11
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!