承接 simtel/rector-rules 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

simtel/rector-rules

Composer 安装命令:

composer require simtel/rector-rules

包简介

Some custom rector rules for use or extend

README 文档

README

Tests Code Quality PHPStan Level

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 find that return nullable types (e.g., ?User) keep their name
  • Methods starting with find that return non-nullable entity types are renamed to get

This follows the common convention where:

  • find* methods may return null when the entity is not found
  • get* 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:

  1. Method name starts with "find"
  2. Has a return type declaration
  3. Return type is not nullable (?Type)
  4. Return type is not a union type
  5. 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

  1. Fork the repository
  2. Create a feature branch
  3. Add your changes with tests
  4. Ensure all tests pass
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 simtel/rector-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 170
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-03