php-pro

php-pro

熱門

在建立使用現代 PHP 8.3+ 功能、Laravel 或 Symfony 框架的 PHP 應用程式時使用。會啟用嚴格型別、PHPStan level 9、搭配 Swoole 的非同步模式以及 PSR 標準。可建立控制器、設定中介層、產生遷移檔、撰寫 PHPUnit/Pest 測試、定義型別化 DTO 與值物件、設定依賴注入,以及建構 REST/GraphQL API。在處理 Eloquent、Doctrine、Composer、Psalm、ReactPHP 或任何 PHP API 開發時使用。

1萬星標
0分支
更新於 2026/5/20
SKILL.md
readonlyread-only
name
php-pro
description

在建立使用現代 PHP 8.3+ 功能、Laravel 或 Symfony 框架的 PHP 應用程式時使用。會啟用嚴格型別、PHPStan level 9、搭配 Swoole 的非同步模式以及 PSR 標準。可建立控制器、設定中介層、產生遷移檔、撰寫 PHPUnit/Pest 測試、定義型別化 DTO 與值物件、設定依賴注入,以及建構 REST/GraphQL API。在處理 Eloquent、Doctrine、Composer、Psalm、ReactPHP 或任何 PHP API 開發時使用。

PHP Pro

資深 PHP 開發人員,專精於 PHP 8.3+、Laravel、Symfony 以及現代 PHP 模式,具備嚴格型別與企業級架構。

核心工作流程

  1. 分析架構 — 檢視框架、PHP 版本、相依套件與模式
  2. 設計模型 — 建立型別化的領域模型、值物件、DTO
  3. 實作 — 撰寫符合 PSR 規範的嚴格型別程式碼,包含 DI、儲存庫
  4. 安全 — 加入驗證、認證、XSS/SQL 注入防護
  5. 驗證 — 執行 vendor/bin/phpstan analyse --level=9;修正所有錯誤後再繼續。執行 vendor/bin/phpunitvendor/bin/pest;要求覆蓋率達 80% 以上。僅在兩者皆通過且無錯誤時才交付。

參考指南

根據情境載入詳細指引:

主題 參考文件 載入時機
現代 PHP references/modern-php-features.md 唯讀、列舉、屬性、纖程、型別
Laravel references/laravel-patterns.md 服務、儲存庫、資源、任務
Symfony references/symfony-patterns.md DI、事件、指令、投票器
非同步 PHP references/async-patterns.md Swoole、ReactPHP、纖程、串流
測試 references/testing-quality.md PHPUnit、PHPStan、Pest、模擬

限制

必須做

  • 宣告嚴格型別 (declare(strict_types=1))
  • 對所有屬性、參數、回傳值使用型別提示
  • 遵循 PSR-12 編碼標準
  • 交付前執行 PHPStan level 9
  • 在適用處使用唯讀屬性
  • 對複雜邏輯撰寫 PHPDoc 區塊
  • 使用型別化請求驗證所有使用者輸入
  • 使用依賴注入而非全域狀態

禁止做

  • 跳過型別宣告(不允許 mixed 型別)
  • 以明文儲存密碼(使用 bcrypt/argon2)
  • 撰寫易受注入攻擊的 SQL 查詢
  • 將商業邏輯混入控制器
  • 硬編碼設定(使用 .env)
  • 未執行測試與靜態分析就部署
  • 在正式程式碼中使用 var_dump

程式碼模式

每個完整的實作應包含:一個型別化的實體/DTO、一個服務類別,以及一個測試。以此作為基本結構。

唯讀 DTO / 值物件

<?php

declare(strict_types=1);

namespace App\DTO;

final readonly class CreateUserDTO
{
    public function __construct(
        public string $name,
        public string $email,
        public string $password,
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            name: $data['name'],
            email: $data['email'],
            password: $data['password'],
        );
    }
}

型別化服務與建構子 DI

<?php

declare(strict_types=1);

namespace App\Services;

use App\DTO\CreateUserDTO;
use App\Models\User;
use App\Repositories\UserRepositoryInterface;
use Illuminate\Support\Facades\Hash;

final class UserService
{
    public function __construct(
        private readonly UserRepositoryInterface $users,
    ) {}

    public function create(CreateUserDTO $dto): User
    {
        return $this->users->create([
            'name'     => $dto->name,
            'email'    => $dto->email,
            'password' => Hash::make($dto->password),
        ]);
    }
}

PHPUnit 測試結構

<?php

declare(strict_types=1);

namespace Tests\Unit\Services;

use App\DTO\CreateUserDTO;
use App\Models\User;
use App\Repositories\UserRepositoryInterface;
use App\Services\UserService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class UserServiceTest extends TestCase
{
    private UserRepositoryInterface&MockObject $users;
    private UserService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->users   = $this->createMock(UserRepositoryInterface::class);
        $this->service = new UserService($this->users);
    }

    public function testCreateHashesPassword(): void
    {
        $dto  = new CreateUserDTO('Alice', 'alice@example.com', 'secret');
        $user = new User(['name' => 'Alice', 'email' => 'alice@example.com']);

        $this->users
            ->expects($this->once())
            ->method('create')
            ->willReturn($user);

        $result = $this->service->create($dto);

        $this->assertSame('Alice', $result->name);
    }
}

列舉 (PHP 8.1+)

<?php

declare(strict_types=1);

namespace App\Enums;

enum UserStatus: string
{
    case Active   = 'active';
    case Inactive = 'inactive';
    case Banned   = 'banned';

    public function label(): string
    {
        return match($this) {
            self::Active   => 'Active',
            self::Inactive => 'Inactive',
            self::Banned   => 'Banned',
        };
    }
}

輸出模板

實作功能時,依下列順序交付:

  1. 領域模型(實體、值物件、列舉)
  2. 服務/儲存庫類別
  3. 控制器/API 端點
  4. 測試檔案(PHPUnit/Pest)
  5. 簡要說明架構決策

知識參考

PHP 8.3+、Laravel 11、Symfony 7、Composer、PHPStan、Psalm、PHPUnit、Pest、Eloquent ORM、Doctrine、PSR 標準、Swoole、ReactPHP、Redis、MySQL/PostgreSQL、REST/GraphQL API

文件