acc-create-phpstan-config

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PHPStan 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
undefined
neon
undefined

phpstan.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: 4
undefined
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/*

# 严格类型检查
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkUninitializedProperties: true
checkImplicitMixed: true
reportUnmatchedIgnoredErrors: true

# 并行处理
parallel:
    maximumNumberOfProcesses: 4
undefined

Existing Project with Baseline

已有项目(带基线)

neon
undefined
neon
undefined

phpstan.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'
undefined
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/*

# 逐步提升严格性
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false

# 遗留代码的类型别名
typeAliases:
    UserId: 'int|string'
    Timestamp: 'int|DateTimeInterface'
undefined

DDD Project Configuration

DDD项目配置

neon
undefined
neon
undefined

phpstan.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\ValueObjectImmutabilityRule
undefined
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/*

# 领域层严格检查
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\ValueObjectImmutabilityRule
undefined

Extension Configuration

扩展配置

PHPUnit Extension

PHPUnit扩展

neon
includes:
    - vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
    # PHPUnit-specific settings
    phpunit:
        configPath: phpunit.xml
neon
includes:
    - vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
    # PHPUnit专属设置
    phpunit:
        configPath: phpunit.xml

Doctrine Extension

Doctrine扩展

neon
includes:
    - vendor/phpstan/phpstan-doctrine/extension.neon

parameters:
    doctrine:
        repositoryClass: Doctrine\ORM\EntityRepository
        objectManagerLoader: tests/object-manager.php
neon
includes:
    - vendor/phpstan/phpstan-doctrine/extension.neon

parameters:
    doctrine:
        repositoryClass: Doctrine\ORM\EntityRepository
        objectManagerLoader: tests/object-manager.php

Symfony 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.php
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.php

Custom 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
undefined
bash
undefined

Generate 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
undefined
vendor/bin/phpstan analyse
undefined

Level Migration Guide

级别迁移指南

From Level 0 to Level 8

从级别0到级别8

neon
undefined
neon
undefined

Step 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

undefined
undefined

Recommended Progression

推荐进阶路径

LevelFocusTimeline
0-2Basic errors, undefined variablesWeek 1
3-4Return types, dead codeWeek 2-3
5-6Argument types, type hintsWeek 4-6
7-8Union types, no mixedWeek 7-10
9Maximum strictnessOngoing
级别重点时间周期
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=github
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=github

GitLab CI

GitLab CI

yaml
phpstan:
  script:
    - vendor/bin/phpstan analyse --memory-limit=1G --error-format=gitlab > phpstan.json
  artifacts:
    reports:
      codequality: phpstan.json
yaml
phpstan:
  script:
    - vendor/bin/phpstan analyse --memory-limit=1G --error-format=gitlab > phpstan.json
  artifacts:
    reports:
      codequality: phpstan.json

Performance Optimization

性能优化

neon
parameters:
    # Parallel processing
    parallel:
        maximumNumberOfProcesses: 8
        processTimeout: 300.0

    # Cache results
    tmpDir: var/cache/phpstan

    # Memory limit
    memoryLimitFile: 1G
neon
parameters:
    # 并行处理
    parallel:
        maximumNumberOfProcesses: 8
        processTimeout: 300.0

    # 缓存结果
    tmpDir: var/cache/phpstan

    # 内存限制
    memoryLimitFile: 1G

Generation Instructions

生成步骤

  1. Analyze project:
    • Check
      composer.json
      for PHPStan version
    • Check existing
      phpstan.neon
    • Identify framework (Symfony, Laravel, etc.)
    • Count existing errors
  2. Determine level:
    • New project: Level 8-9
    • Existing with few errors: Current + 1
    • Legacy: Level 0 + baseline
  3. Add extensions:
    • PHPUnit if tests exist
    • Doctrine if ORM used
    • Symfony/Laravel if framework
  4. Generate baseline if needed:
    • For existing projects with errors
    • Include command to regenerate
  1. 分析项目:
    • 检查
      composer.json
      中的PHPStan版本
    • 检查已有的
      phpstan.neon
      文件
    • 识别使用的框架(Symfony、Laravel等)
    • 统计现有错误数量
  2. 确定级别:
    • 新项目:级别8-9
    • 错误较少的已有项目:当前级别+1
    • 遗留项目:级别0 + 基线
  3. 添加扩展:
    • 若存在测试则添加PHPUnit
    • 若使用ORM则添加Doctrine
    • 若使用框架则添加Symfony/Laravel扩展
  4. 按需生成基线:
    • 针对已有错误的项目
    • 包含重新生成的命令

Usage

使用方法

Provide:
  • Project type (new/existing/legacy)
  • Framework (Symfony, Laravel, none)
  • Current error count (if existing)
  • Target level (optional)
The generator will:
  1. Create appropriate configuration
  2. Add relevant extensions
  3. Include ignore patterns
  4. Generate baseline commands if needed
提供以下信息:
  • 项目类型(新项目/已有项目/遗留项目)
  • 使用的框架(Symfony、Laravel、无)
  • 当前错误数量(若为已有项目)
  • 目标级别(可选)
生成器将:
  1. 创建合适的配置文件
  2. 添加相关扩展
  3. 包含忽略规则
  4. 按需生成基线命令