simtel/rector-rules
Composer 安装命令:
composer require simtel/rector-rules
包简介
Some custom rector rules for use or extend
README 文档
README
A collection of custom Rector rules for automated PHP code refactoring.
Overview
This package provides custom Rector rules to help modernize and improve PHP codebases through automated refactoring. Currently includes the RenameFindAndGetMethodCallRector and WithConsecutiveToCallbackRector rules.
Requirements
- PHP 8.3+
- Rector 2.0+
Installation
Clone this repository and install dependencies:
git clone <repository-url> cd rector-rules composer install
Rules
RenameFindAndGetMethodCallRector
Automatically renames find* methods to get* when they return a non-nullable entity type.
What it does
This rule enforces a naming convention where:
- Methods starting with
findthat return nullable types (e.g.,?User) keep their name - Methods starting with
findthat return non-nullable entity types are renamed toget
This follows the common convention where:
find*methods may returnnullwhen the entity is not foundget*methods always return an entity and throw exceptions when not found
Before
class UserRepository { public function findUserById(int $id): User { // Always returns User, never null return $this->entityManager->find(User::class, $id) ?? throw new UserNotFoundException(); } public function findUserByEmail(string $email): ?User { // May return null return $this->entityManager->getRepository(User::class) ->findOneBy(['email' => $email]); } }
After
class UserRepository { public function getUserById(int $id): User // Renamed find -> get { // Always returns User, never null return $this->entityManager->find(User::class, $id) ?? throw new UserNotFoundException(); } public function findUserByEmail(string $email): ?User // Unchanged (nullable) { // May return null return $this->entityManager->getRepository(User::class) ->findOneBy(['email' => $email]); } }
Rules for transformation
The rule will rename a method from find* to get* if:
- Method name starts with "find"
- Has a return type declaration
- Return type is not nullable (
?Type) - Return type is not a union type
- Return type is not a primitive type (int, string, bool, float, array, object, mixed, void)
WithConsecutiveToCallbackRector
Replaces deprecated PHPUnit withConsecutive method calls with willReturnCallback to ensure compatibility with PHPUnit 10+.
What it does
This rule transforms deprecated withConsecutive method calls to use willReturnCallback with conditional assertions based on invocation count. This is necessary because withConsecutive was deprecated in PHPUnit 9.6 and removed in PHPUnit 10.
Before
$mock = $this->createMock(SomeClass::class); $mock->expects($this->exactly(2)) ->method('someMethod') ->withConsecutive( ['first'], ['second'] );
After
$mock = $this->createMock(SomeClass::class); $mock->expects($this->exactly(2)) ->method('someMethod') ->willReturnCallback(function ($parameters) { static $callCount = 0; $callCount++; if ($callCount === 1) { $this->assertSame(['first'], $parameters); } if ($callCount === 2) { $this->assertSame(['second'], $parameters); } });
Usage
Manual Configuration
Create a rector.php configuration file:
<?php declare(strict_types=1); use Rector\Config\RectorConfig; use Simtel\RectorRules\Rector\RenameFindAndGetMethodCallRector; use Simtel\RectorRules\Rector\PHPUnit\WithConsecutiveToCallbackRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/src', ]); $rectorConfig->rule(RenameFindAndGetMethodCallRector::class); $rectorConfig->rule(WithConsecutiveToCallbackRector::class); };
Running Rector
Execute the refactoring:
# Dry run (shows what would be changed) vendor/bin/rector process --dry-run # Apply changes vendor/bin/rector process
Development
Continuous Integration
This project uses GitHub Actions for automated testing and code quality checks:
- Tests Workflow: Runs PHPUnit tests on PHP 8.3 and 8.4 with both lowest and stable dependencies
- Code Quality Workflow: Performs static analysis with PHPStan, syntax checking, and security audits
- Coverage: Test coverage reports are uploaded to Codecov
Local Development Scripts
# Run all tests composer test # Run tests with coverage report composer test-coverage # Run static analysis composer analyse # Format code with PSR-12 standards composer format # Check code formatting (without fixing) composer format-check # Run all checks (analysis, formatting, and tests) composer check
Running Tests
vendor/bin/phpunit
Code Analysis
vendor/bin/phpstan analyse
Code Formatting
This project uses Laravel Pint for code formatting, configured to follow PSR-12 standards:
# Format all code files vendor/bin/pint # Check formatting without making changes vendor/bin/pint --test # Format specific files or directories vendor/bin/pint src/ vendor/bin/pint tests/
The Pint configuration is stored in pint.json and includes:
- PSR-12 preset
- Short array syntax
- Alphabetical import ordering
- Removal of unused imports
- Trailing commas in multiline arrays
Project Structure
rector-rules/
├── src/
│ └── Rector/
│ ├── PHPUnit/
│ │ └── WithConsecutiveToCallbackRector.php
│ └── RenameFindAndGetMethodCallRector.php
├── tests/
│ ├── RenameFindAndGetMethodCallRector/
│ │ ├── config/
│ │ │ └── configured_rule.php
│ │ ├── Fixture/
│ │ │ └── some_class.php.inc
│ │ └── RenameFindAndGetMethodCallRectorTest.php
│ └── WithConsecutiveToCallbackRector/
│ ├── config/
│ │ └── configured_rule.php
│ ├── Fixture/
│ │ └── with_consecutive.php.inc
│ └── WithConsecutiveToCallbackRectorTest.php
├── composer.json
├── phpunit.xml
└── README.md
Contributing
- Fork the repository
- Create a feature branch
- Add your changes with tests
- Ensure all tests pass
- Submit a pull request
License
This project is open source. Please check the license file for more details.
Author
Created by Simtel
For more information about Rector, visit https://getrector.org/
simtel/rector-rules 适用场景与选型建议
simtel/rector-rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 simtel/rector-rules 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simtel/rector-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 170
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-03