aryeohq/phpstan-rules
Composer 安装命令:
composer require aryeohq/phpstan-rules
包简介
Aryeo's Custom PHPStan Rules
README 文档
README
How to Install & Use in Your Project
Follow these steps to integrate this custom‐rule pack into any PHP project running PHPStan:
-
Require via Composer (adjust package name as needed):
composer require --dev aryeo/phpstan-rules
-
Add (or merge)
rules.neoninto your application’s PHPStan config:-
In your project root, you probably have a
phpstan.neonorphpstan.neon.dist. -
Ensure it includes this package’s
rules.neon. For example:# phpstan.neon.dist includes: - vendor/aryeo/phpstan-rules/rules.neon parameters: # 1) Enable/disable test rules: aryeo: tests: enabled: true # Override TestCase class if needed testCaseClass: App\Tests\TestCase # 2) Your other PHPStan settings: level: max paths: - src - tests
-
The
includes:line loads all sections from the package’srules.neon(includingconditionalTags,parameters,parametersSchema, andservices).
-
-
Override parameters (optional):
-
If your project’s tests extend from
App\Tests\TestCase, set:parameters: aryeo: tests: testCaseClass: App\Tests\TestCase
-
If you want to disable all PHPStan test rules, set:
parameters: aryeo: tests: enabled: false
-
Leaving
aryeo.tests.enabledout entirely will default totrue(because the package’srules.neonhasenabled: trueby default), so you only need to override it when you want it off.
-
-
Run PHPStan as usual:
vendor/bin/phpstan analyse -c phpstan.neon.dist
-
If you kept the default
testCaseClass: Tests\TestCasebut your project uses a different namespace, you will see errors like:ClassAryeo\PHPStan\Rules\Tests\ClassMustExtendTestCaseRule: Class App\Tests\SomeTest does not extend Tests\TestCaseIn that case, update
aryeo.tests.testCaseClassaccordingly.
-
Rule Descriptions & Examples
Below is a quick summary of what each rule does, plus usage examples.
Aryeo\PHPStan\Rules\Enums\EnumCasePascalCaseRule
-
Always active (no conditional toggle).
-
Enforces: Every Enum
casename must be PascalCase. -
Why? PHPStan’s default
EnumCaserule focuses on visibility or value uniqueness, but this custom rule ensures stylistic consistency across your codebase. -
Example Violation:
enum Status: string { case pending; // invalid: “pending” is not PascalCase case OrderPickedUp; // invalid: starts with uppercase but next word not separated by capital case Shipped; // valid if that’s exactly PascalCase (single‐word “Shipped”) }
-
Fix:
enum Status: string { - case pending; - case OrderPickedUp; + case Pending; + case OrderPickedUp; // if you intended “OrderPickedUp” as PascalCase, ensure proper casing case Shipped; }
Aryeo\PHPStan\Rules\Tests\ClassMustExtendTestCaseRule
-
Conditional: Only runs when
aryeo.tests.enabled=true. -
Constructor Argument:
$testCaseClass(e.g.,'Tests\TestCase'or'App\Tests\TestCase'). -
Enforces: Every class under your
tests/directory (or any file matching*Test.php) must extend the given base test case class. -
Why? Some teams create a
BaseTestCaseorTestCasewith common setup/teardown. This rule ensures no test accidentally extends\PHPUnit\Framework\TestCasedirectly or some other class. -
Example Violation:
// Assume: aryeo.tests.testCaseClass = 'App\Tests\TestCase' namespace App\Tests\Feature; use PHPUnit\Framework\TestCase; class UserTest extends TestCase { public function testSomething() { $this->assertTrue(true); } }
- Error:
Class UserTest does not extend App\Tests\TestCase.
- Error:
-
Fix:
use PHPUnit\Framework\TestCase; class UserTest extends TestCase
to:
use App\Tests\TestCase; // your own base test case class UserTest extends TestCase
Aryeo\PHPStan\Rules\Tests\MethodNamePrefixRule
-
Conditional: Only runs when
aryeo.tests.enabled=true. -
Constructor Argument:
$testCaseClass. -
Enforces: In any class extending
$testCaseClass, all public test methods must start with a given prefix (e.g.,test). -
Why? PHPUnit’s default behavior is to look for methods that start with
test, but you may have configured fallback annotations, or you want to enforce a uniform method naming convention. This rule ensures that any test method is namedtestSomething…rather thansomethingTest()orit_does_something(). -
Example Violation:
class UserTest extends App\Tests\TestCase { public function doesSomething() // invalid: missing “test” prefix { $this->assertTrue(true); } public function testItWorks() // valid { $this->assertTrue(true); } }
-
Fix: Rename method to:
public function testDoesSomething() { $this->assertTrue(true); }
Aryeo\PHPStan\Rules\Tests\MethodNameSnakeCaseRule
-
Conditional: Only runs when
aryeo.tests.enabled=true. -
Constructor Argument:
$testCaseClass. -
Enforces: Test method names in classes extending
$testCaseClassmust usesnake_caserather thancamelCaseorPascalCase. -
Why? Some teams prefer writing test names in snake_case so that method names—when read—describe behavior in a consistent way (e.g.,
test_user_can_register). If your team enforces snake_case, this rule flags camelCase or PascalCase. -
Example Violation:
class UserTest extends App\Tests\TestCase { public function testUserCanRegister() // invalid: camelCase/PascalCase { $this->assertTrue(true); } public function test_user_can_login() // valid: snake_case { $this->assertTrue(true); } }
-
Fix: Convert to snake_case:
- public function testUserCanRegister() + public function test_user_can_register() { $this->assertTrue(true); }
aryeohq/phpstan-rules 适用场景与选型建议
aryeohq/phpstan-rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.77k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aryeohq/phpstan-rules 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aryeohq/phpstan-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-27