simtel/rector-rules

Some custom rector rules for use or extend

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/simtel/rector-rules

1.0 2025-10-03 10:53 UTC

This package is auto-updated.

Last update: 2025-12-09 06:55:29 UTC


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/