acc-create-phpstan-config
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePHPStan Configuration Generator
PHPStan配置生成器
Generates optimized PHPStan configurations for PHP 8.4+ projects.
为PHP 8.4+项目生成优化的PHPStan配置。
Generated Files
生成的文件
phpstan.neon # Main configuration
phpstan-baseline.neon # Error baseline (if needed)phpstan.neon # 主配置文件
phpstan-baseline.neon # 错误基线文件(如有需要)Configuration by Project Type
按项目类型配置
New Project (Level 8-9)
新项目(级别8-9)
neon
undefinedneon
undefinedphpstan.neon
phpstan.neon
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
level: 8
phpVersion: 80400
paths:
- src
- tests
excludePaths:
- tests/Fixtures/*
- src/Infrastructure/Legacy/*
# Strict type checking
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkUninitializedProperties: true
checkImplicitMixed: true
reportUnmatchedIgnoredErrors: true
# Parallel processing
parallel:
maximumNumberOfProcesses: 4undefinedincludes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
level: 8
phpVersion: 80400
paths:
- src
- tests
excludePaths:
- tests/Fixtures/*
- src/Infrastructure/Legacy/*
# 严格类型检查
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkUninitializedProperties: true
checkImplicitMixed: true
reportUnmatchedIgnoredErrors: true
# 并行处理
parallel:
maximumNumberOfProcesses: 4undefinedExisting Project with Baseline
已有项目(带基线)
neon
undefinedneon
undefinedphpstan.neon
phpstan.neon
includes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
parameters:
level: 6
paths:
- src
- tests
excludePaths:
- src/Legacy/*
- tests/Fixtures/*
# Gradually increase strictness
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
# Type aliases for legacy code
typeAliases:
UserId: 'int|string'
Timestamp: 'int|DateTimeInterface'undefinedincludes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
parameters:
level: 6
paths:
- src
- tests
excludePaths:
- src/Legacy/*
- tests/Fixtures/*
# 逐步提升严格性
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
# 遗留代码的类型别名
typeAliases:
UserId: 'int|string'
Timestamp: 'int|DateTimeInterface'undefinedDDD Project Configuration
DDD项目配置
neon
undefinedneon
undefinedphpstan.neon
phpstan.neon
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
parameters:
level: 9
phpVersion: 80400
paths:
- src
excludePaths:
- src/Infrastructure/Migrations/*
# Strict for Domain layer
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkUninitializedProperties: true
checkImplicitMixed: true
# Report all issues
reportUnmatchedIgnoredErrors: true
reportStaticMethodSignatures: true
# Parallel for speed
parallel:
maximumNumberOfProcesses: 8
processTimeout: 300.0
# Custom parameters
doctrine:
repositoryClass: App\Infrastructure\Persistence\DoctrineRepository
objectManagerLoader: tests/object-manager.php
# Ignore patterns
ignoreErrors:
# Doctrine entities
- '#Property .+ has no type specified#'
path: src/Infrastructure/Doctrine/Entity/*
# Value Objects immutability
- '#Readonly property .+ is assigned outside of its declaring class#'
path: src/Domain/*/ValueObject/*
# Custom rules
rules:
- App\PHPStan\DomainLayerRule
- App\PHPStan\ValueObjectImmutabilityRuleundefinedincludes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
parameters:
level: 9
phpVersion: 80400
paths:
- src
excludePaths:
- src/Infrastructure/Migrations/*
# 领域层严格检查
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkUninitializedProperties: true
checkImplicitMixed: true
# 报告所有问题
reportUnmatchedIgnoredErrors: true
reportStaticMethodSignatures: true
# 并行处理提升速度
parallel:
maximumNumberOfProcesses: 8
processTimeout: 300.0
# 自定义参数
doctrine:
repositoryClass: App\Infrastructure\Persistence\DoctrineRepository
objectManagerLoader: tests/object-manager.php
# 忽略规则
ignoreErrors:
# Doctrine实体类
- '#Property .+ has no type specified#'
path: src/Infrastructure/Doctrine/Entity/*
# 值对象不可变性
- '#Readonly property .+ is assigned outside of its declaring class#'
path: src/Domain/*/ValueObject/*
# 自定义规则
rules:
- App\PHPStan\DomainLayerRule
- App\PHPStan\ValueObjectImmutabilityRuleundefinedExtension Configuration
扩展配置
PHPUnit Extension
PHPUnit扩展
neon
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
# PHPUnit-specific settings
phpunit:
configPath: phpunit.xmlneon
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
# PHPUnit专属设置
phpunit:
configPath: phpunit.xmlDoctrine Extension
Doctrine扩展
neon
includes:
- vendor/phpstan/phpstan-doctrine/extension.neon
parameters:
doctrine:
repositoryClass: Doctrine\ORM\EntityRepository
objectManagerLoader: tests/object-manager.phpneon
includes:
- vendor/phpstan/phpstan-doctrine/extension.neon
parameters:
doctrine:
repositoryClass: Doctrine\ORM\EntityRepository
objectManagerLoader: tests/object-manager.phpSymfony Extension
Symfony扩展
neon
includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
parameters:
symfony:
containerXmlPath: var/cache/dev/App_KernelDevDebugContainer.xml
consoleApplicationLoader: tests/console-application.phpneon
includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
parameters:
symfony:
containerXmlPath: var/cache/dev/App_KernelDevDebugContainer.xml
consoleApplicationLoader: tests/console-application.phpCustom Error Patterns
自定义错误规则
Common Ignore Patterns
常用忽略规则
neon
parameters:
ignoreErrors:
# Constructor property promotion
- '#Constructor of class .+ has an unused parameter#'
# Doctrine entities
- '#Property .+ does not accept null#'
path: src/Infrastructure/Doctrine/Entity/*
# Test doubles
- '#Call to an undefined method .+Mock::#'
path: tests/*
# Dynamic properties in tests
- '#Access to an undefined property .+Test::\$#'
path: tests/*
# Factory methods
- '#Method .+Factory::create\(\) should return .+ but returns#'
# Event handlers
- '#Parameter .+ of method .+Handler::__invoke\(\) has no type specified#'neon
parameters:
ignoreErrors:
# 构造函数属性提升
- '#Constructor of class .+ has an unused parameter#'
# Doctrine实体类
- '#Property .+ does not accept null#'
path: src/Infrastructure/Doctrine/Entity/*
# 测试替身
- '#Call to an undefined method .+Mock::#'
path: tests/*
# 测试中的动态属性
- '#Access to an undefined property .+Test::\$#'
path: tests/*
# 工厂方法
- '#Method .+Factory::create\(\) should return .+ but returns#'
# 事件处理器
- '#Parameter .+ of method .+Handler::__invoke\(\) has no type specified#'Error Baseline Generation
错误基线生成
bash
undefinedbash
undefinedGenerate baseline for existing errors
为现有错误生成基线
vendor/bin/phpstan analyse --generate-baseline
vendor/bin/phpstan analyse --generate-baseline
Generate baseline with specific name
生成指定名称的基线
vendor/bin/phpstan analyse --generate-baseline=phpstan-baseline.neon
vendor/bin/phpstan analyse --generate-baseline=phpstan-baseline.neon
Analyze and exclude baseline errors
分析并排除基线中的错误
vendor/bin/phpstan analyse
undefinedvendor/bin/phpstan analyse
undefinedLevel Migration Guide
级别迁移指南
From Level 0 to Level 8
从级别0到级别8
neon
undefinedneon
undefinedStep 1: Start with baseline at current level
步骤1:从当前级别开始并生成基线
parameters:
level: 0
parameters:
level: 0
Step 2: Generate baseline
步骤2:生成基线
vendor/bin/phpstan analyse --generate-baseline
vendor/bin/phpstan analyse --generate-baseline
Step 3: Increase level
步骤3:提升级别
parameters:
level: 1
parameters:
level: 1
Step 4: Fix new errors or add to baseline
步骤4:修复新错误或添加到基线
Repeat until level 8
重复此过程直到达到级别8
undefinedundefinedRecommended Progression
推荐进阶路径
| Level | Focus | Timeline |
|---|---|---|
| 0-2 | Basic errors, undefined variables | Week 1 |
| 3-4 | Return types, dead code | Week 2-3 |
| 5-6 | Argument types, type hints | Week 4-6 |
| 7-8 | Union types, no mixed | Week 7-10 |
| 9 | Maximum strictness | Ongoing |
| 级别 | 重点 | 时间周期 |
|---|---|---|
| 0-2 | 基础错误、未定义变量 | 第1周 |
| 3-4 | 返回类型、死代码 | 第2-3周 |
| 5-6 | 参数类型、类型提示 | 第4-6周 |
| 7-8 | 联合类型、禁止mixed类型 | 第7-10周 |
| 9 | 最高严格性 | 持续进行 |
CI Configuration
CI配置
GitHub Actions
GitHub Actions
yaml
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
- run: composer install
- run: vendor/bin/phpstan analyse --memory-limit=1G --error-format=githubyaml
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
- run: composer install
- run: vendor/bin/phpstan analyse --memory-limit=1G --error-format=githubGitLab CI
GitLab CI
yaml
phpstan:
script:
- vendor/bin/phpstan analyse --memory-limit=1G --error-format=gitlab > phpstan.json
artifacts:
reports:
codequality: phpstan.jsonyaml
phpstan:
script:
- vendor/bin/phpstan analyse --memory-limit=1G --error-format=gitlab > phpstan.json
artifacts:
reports:
codequality: phpstan.jsonPerformance Optimization
性能优化
neon
parameters:
# Parallel processing
parallel:
maximumNumberOfProcesses: 8
processTimeout: 300.0
# Cache results
tmpDir: var/cache/phpstan
# Memory limit
memoryLimitFile: 1Gneon
parameters:
# 并行处理
parallel:
maximumNumberOfProcesses: 8
processTimeout: 300.0
# 缓存结果
tmpDir: var/cache/phpstan
# 内存限制
memoryLimitFile: 1GGeneration Instructions
生成步骤
-
Analyze project:
- Check for PHPStan version
composer.json - Check existing
phpstan.neon - Identify framework (Symfony, Laravel, etc.)
- Count existing errors
- Check
-
Determine level:
- New project: Level 8-9
- Existing with few errors: Current + 1
- Legacy: Level 0 + baseline
-
Add extensions:
- PHPUnit if tests exist
- Doctrine if ORM used
- Symfony/Laravel if framework
-
Generate baseline if needed:
- For existing projects with errors
- Include command to regenerate
-
分析项目:
- 检查中的PHPStan版本
composer.json - 检查已有的文件
phpstan.neon - 识别使用的框架(Symfony、Laravel等)
- 统计现有错误数量
- 检查
-
确定级别:
- 新项目:级别8-9
- 错误较少的已有项目:当前级别+1
- 遗留项目:级别0 + 基线
-
添加扩展:
- 若存在测试则添加PHPUnit
- 若使用ORM则添加Doctrine
- 若使用框架则添加Symfony/Laravel扩展
-
按需生成基线:
- 针对已有错误的项目
- 包含重新生成的命令
Usage
使用方法
Provide:
- Project type (new/existing/legacy)
- Framework (Symfony, Laravel, none)
- Current error count (if existing)
- Target level (optional)
The generator will:
- Create appropriate configuration
- Add relevant extensions
- Include ignore patterns
- Generate baseline commands if needed
提供以下信息:
- 项目类型(新项目/已有项目/遗留项目)
- 使用的框架(Symfony、Laravel、无)
- 当前错误数量(若为已有项目)
- 目标级别(可选)
生成器将:
- 创建合适的配置文件
- 添加相关扩展
- 包含忽略规则
- 按需生成基线命令