PHPackages                             simtel/rector-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. simtel/rector-rules

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

simtel/rector-rules
===================

Some custom rector rules for use or extend

1.0(7mo ago)051MITPHPPHP ^8.3CI passing

Since Oct 3Pushed 5mo agoCompare

[ Source](https://github.com/Simtel/rector-rules)[ Packagist](https://packagist.org/packages/simtel/rector-rules)[ RSS](/packages/simtel-rector-rules/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Rector Rules
============

[](#rector-rules)

[![Tests](https://github.com/simtel/rector-rules/workflows/Tests/badge.svg)](https://github.com/simtel/rector-rules/actions)[![Code Quality](https://github.com/simtel/rector-rules/workflows/Code%20Quality/badge.svg)](https://github.com/simtel/rector-rules/actions)[![PHPStan Level](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174)](https://phpstan.org/)

A collection of custom Rector rules for automated PHP code refactoring.

Overview
--------

[](#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
------------

[](#requirements)

- PHP 8.3+
- Rector 2.0+

Installation
------------

[](#installation)

Clone this repository and install dependencies:

```
git clone
cd rector-rules
composer install
```

Rules
-----

[](#rules)

### RenameFindAndGetMethodCallRector

[](#renamefindandgetmethodcallrector)

Automatically renames `find*` methods to `get*` when they return a non-nullable entity type.

#### What it does

[](#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

[](#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

[](#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

[](#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

[](#withconsecutivetocallbackrector)

Replaces deprecated PHPUnit withConsecutive method calls with willReturnCallback to ensure compatibility with PHPUnit 10+.

#### What it does

[](#what-it-does-1)

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

[](#before-1)

```
$mock = $this->createMock(SomeClass::class);
$mock->expects($this->exactly(2))
    ->method('someMethod')
    ->withConsecutive(
        ['first'],
        ['second']
    );
```

#### After

[](#after-1)

```
$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
-----

[](#usage)

### Manual Configuration

[](#manual-configuration)

Create a `rector.php` configuration file:

```
