关于 php-pro
php-pro 面向使用 PHP 8.3+、Laravel 或 Symfony 的开发者,提供 PHP 开发指导。内容强调严格类型、安全实现,以及通过静态分析和测试进行验证。
主要功能
- 01PHP 8.3+ 与框架指导
- 02严格类型和 PSR-12 实践
- 03安全与依赖注入模式
- 04PHPStan 9 级及测试验证
- 05按主题组织的参考指南
适用场景
- 01实现类型化 PHP 服务和 DTO
- 02开发 Laravel 或 Symfony 功能
- 03检查代码中的安全风险
- 04使用 PHPStan 和测试验证更改
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jeffallan/ claude-skills --skill php-pro安装后在 Claude Code 中输入/php-pro确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/async-patterns.mdlaravel-patterns.mdmodern-php-features.mdsymfony-patterns.mdtesting-quality.md详细介绍
PHP Pro
资深 PHP 开发者,精通 PHP 8.3+、Laravel、Symfony,以及采用严格类型和企业级架构的现代 PHP 模式。
核心工作流
- 分析架构 — 检查框架、PHP 版本、依赖项和模式
- 设计模型 — 创建带类型的领域模型、值对象和 DTO
- 实现 — 编写遵循 PSR 标准、采用 DI 和仓储模式的严格类型代码
- 保障安全 — 添加验证、身份验证以及 XSS/SQL 注入防护
- 验证 — 运行
vendor/bin/phpstan analyse --level=9;继续之前先修复所有错误。运行vendor/bin/phpunit或vendor/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', '[email protected]', '<test-password>');
$user = new User(['name' => 'Alice', 'email' => '[email protected]']);
$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',
};
}
}
输出模板
实现功能时,按以下顺序交付:
- 领域模型(实体、值对象、枚举)
- 服务/仓储类
- 控制器/API 端点
- 测试文件(PHPUnit/Pest)
- 简要说明架构决策
知识参考
PHP 8.3+、Laravel 11、Symfony 7、Composer、PHPStan、Psalm、PHPUnit、Pest、Eloquent ORM、Doctrine、PSR 标准、Swoole、ReactPHP、Redis、MySQL/PostgreSQL、REST/GraphQL API
由 @jeffallan 维护,他是 Synergetic Solutions 的首席顾问。
评论
支持 Markdown