ctw/ctw-qa
Composer 安装命令:
composer require ctw/ctw-qa
包简介
Configuration for commonly used quality assurance (QA) tools for PHP projects.
README 文档
README
Centralized, opinionated configuration for PHP 8.3+ quality assurance tools: Rector, Easy Coding Standard (ECS), and PHPStan.
Introduction
Why This Library Exists
Setting up quality assurance tools properly is tedious and error-prone. Each project requires configuring:
- Rector for code modernization (100+ lines of configuration)
- Easy Coding Standard for style enforcement (150+ lines)
- PHPStan for static analysis (50+ lines)
Multiplied across numerous projects, this becomes a maintenance burden. Configurations drift, standards diverge, and teams waste time debugging tool setups instead of writing code.
This library provides:
- Battle-tested defaults: Years of PHP best practices encoded in reusable configuration classes
- PHP 8.3+ standards: Modern PHP syntax, strict types, and property promotion
- PSR-12 compliance: Full adherence to PHP coding standards
- Extensible architecture: Override any default via class inheritance
- Consistent tooling: Identical QA configuration across all projects
Problems This Library Solves
- Configuration drift: Projects diverge in coding standards over time
- Repetitive setup: Same boilerplate written for every new project
- Inconsistent quality: Different strictness levels across codebases
- Maintenance burden: Tool updates require changes in multiple places
- Onboarding friction: New developers must learn project-specific configurations
Where to Use This Library
- New PHP projects: Start with modern, strict standards from day one
- Existing codebases: Gradually modernize legacy code with Rector
- Monorepos: Share identical QA configuration across all packages
- CI/CD pipelines: Automated quality gates with consistent rules
- Open source libraries: Enforce professional-grade code quality
Design Goals
- Opinionated defaults: Strong opinions for modern PHP, easily overridable
- Invokable classes: Simple
$config()syntax for integration - Maximum strictness: PHPStan level
max, strict comparisons, strict types - Minimal dependencies: Only the three QA tools themselves
- Extensible: All configuration classes designed for inheritance
Requirements
- PHP 8.3 or higher
- Composer
Installation
Install by adding the package as a Composer requirement:
composer require ctw/ctw-qa --dev
Usage Examples
Rector Configuration
Create rector.php in your project root:
<?php declare(strict_types=1); use Ctw\Qa\Rector\Config\RectorConfig\DefaultFileExtensions; use Ctw\Qa\Rector\Config\RectorConfig\DefaultSets; use Ctw\Qa\Rector\Config\RectorConfig\DefaultSkip; use Rector\Config\RectorConfig; return static function (RectorConfig $rectorConfig): void { $fileExtensions = new DefaultFileExtensions(); $sets = new DefaultSets(); $skip = new DefaultSkip(); $rectorConfig->fileExtensions($fileExtensions()); $rectorConfig->sets($sets()); $rectorConfig->paths(['src', 'test']); $rectorConfig->skip([...$skip()]); };
ECS Configuration
Create ecs.php in your project root:
<?php declare(strict_types=1); use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultFileExtensions; use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultIndentation; use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultLineEnding; use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultRules; use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultRulesWithConfiguration; use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultSets; use Ctw\Qa\EasyCodingStandard\Config\ECSConfig\DefaultSkip; use Symplify\EasyCodingStandard\Config\ECSConfig; use Symplify\EasyCodingStandard\Configuration\ECSConfigBuilder; // Wrapped in an immediately-invoked closure: ECS require()s this file in the // scope of its container factory, where the container is held in a variable // named $ecsConfig. Building at file scope would clobber it; the closure keeps // every local contained. return (static function (): ECSConfigBuilder { $fileExtensions = new DefaultFileExtensions(); $indentation = new DefaultIndentation(); $lineEnding = new DefaultLineEnding(); $rules = new DefaultRules(); $rulesConfig = new DefaultRulesWithConfiguration(); $sets = new DefaultSets(); $skip = new DefaultSkip(); $ecsConfig = ECSConfig::configure() ->withFileExtensions($fileExtensions()) ->withSpacing(indentation: $indentation(), lineEnding: $lineEnding()) ->withPaths(['src', 'test']) ->withSets($sets()) ->withRules($rules()) ->withSkip($skip()); foreach ($rulesConfig() as $checkerClass => $configuration) { $ecsConfig->withConfiguredRule($checkerClass, $configuration); } return $ecsConfig; })();
PHPStan Configuration
Create phpstan.neon in your project root:
parameters: level: max paths: - src - test bootstrapFiles: - vendor/autoload.php
Composer Scripts
Add to your composer.json:
{
"scripts": {
"qa": ["@rector", "@ecs", "@phpstan"],
"qa-fix": ["@rector-fix", "@ecs-fix", "@phpstan"],
"rector": "vendor/bin/rector process --dry-run",
"rector-fix": "vendor/bin/rector process",
"ecs": "vendor/bin/ecs",
"ecs-fix": "vendor/bin/ecs --fix",
"phpstan": "vendor/bin/phpstan analyse"
}
}
Run QA checks:
composer qa # Check everything (dry-run) composer qa-fix # Auto-fix everything possible
Included Rule Sets
Rector (Code Modernization)
| Set | Description |
|---|---|
UP_TO_PHP_83 |
Modernizes code to PHP 8.3 syntax |
PHPUNIT_100 |
Upgrades PHPUnit to version 10.0+ |
CODE_QUALITY |
Simplifies expressions, removes redundancy |
CODING_STYLE |
Enforces consistent style |
DEAD_CODE |
Removes unused code |
NAMING |
Improves naming conventions |
ECS (Code Style)
| Rule | Description |
|---|---|
DeclareStrictTypesFixer |
Adds declare(strict_types=1) |
DisallowLongArraySyntaxSniff |
Enforces short array syntax [] |
StrictComparisonFixer |
Enforces === over == |
NoUnusedImportsFixer |
Removes unused imports |
OrderedImportsFixer |
Alphabetizes imports |
TrailingCommaInMultilineFixer |
Adds trailing commas |
PHPStan (Static Analysis)
- Level:
max(strictest) - Strict rules enabled
- PHPUnit extension included
Customization
Extend any configuration class to modify defaults:
<?php declare(strict_types=1); namespace App\QA; use Ctw\Qa\Rector\Config\RectorConfig\DefaultSkip; class CustomSkip extends DefaultSkip { public function __invoke(): array { $skip = parent::__invoke(); $skip[] = '*/legacy/*'; return $skip; } }
Use your custom class:
$skip = new \App\QA\CustomSkip(); $rectorConfig->skip([...$skip()]);
ctw/ctw-qa 适用场景与选型建议
ctw/ctw-qa 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.13k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ctw/ctw-qa 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ctw/ctw-qa 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.13k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 16
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2023-06-11