PHP code example of simtel / rector-rules

1. Go to this page and download the library: Download simtel/rector-rules library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simtel / rector-rules example snippets


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]);
    }
}

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]);
    }
}

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

$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);
        }
    });



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);
};