Skip to content

Commit

Permalink
Add migration rules for Laravel 5.3 (#261)
Browse files Browse the repository at this point in the history
* Migrate the deprecated lists method in Laravel 5.3

* Migrate even more deprecations Laravel 5.3
  • Loading branch information
GeniJaho authored Oct 26, 2024
1 parent da04b05 commit cefbadd
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
33 changes: 33 additions & 0 deletions config/sets/laravel53.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,46 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
use Rector\Removing\Rector\Class_\RemoveTraitUseRector;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
use Rector\Transform\ValueObject\StaticCallToFuncCall;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');

$rectorConfig
->ruleWithConfiguration(RemoveTraitUseRector::class, [
// see https://laravel.com/docs/5.3/upgrade
'Illuminate\Foundation\Auth\Access\AuthorizesResources',
]);

// https://laravel.com/docs/5.3/upgrade#5.2-deprecations
$rectorConfig
->ruleWithConfiguration(RenameMethodRector::class, [
new MethodCallRename('Illuminate\Support\Collection', 'lists', 'pluck'),
new MethodCallRename('Illuminate\Database\Query\Builder', 'lists', 'pluck'),
new MethodCallRename('Illuminate\Database\Eloquent\Collection', 'withHidden', 'makeVisible'),
new MethodCallRename('Illuminate\Database\Eloquent\Model', 'withHidden', 'makeVisible'),
]);

$rectorConfig
->ruleWithConfiguration(RemoveInterfacesRector::class, [
'Illuminate\Contracts\Bus\SelfHandling',
]);

$rectorConfig
->ruleWithConfiguration(RenameClassRector::class, [
'Illuminate\Database\Eloquent\ScopeInterface' => 'Illuminate\Database\Eloquent\Scope',
'Illuminate\View\Expression' => 'Illuminate\Support\HtmlString',
]);

$rectorConfig
->ruleWithConfiguration(StaticCallToFuncCallRector::class, [
new StaticCallToFuncCall('Illuminate\Support\Str', 'randomBytes', 'random_bytes'),
new StaticCallToFuncCall('Illuminate\Support\Str', 'equals', 'hash_equals'),
]);
};
51 changes: 51 additions & 0 deletions tests/Sets/Laravel53/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace RectorLaravel\Tests\Sets\Laravel53;

(new \Illuminate\Support\Collection())->lists('id');
(new \Illuminate\Database\Eloquent\Builder())->lists('id');
(new \Illuminate\Database\Query\Builder())->lists('id');
(new \Illuminate\Database\Eloquent\Collection())->withHidden([]);
(new \Illuminate\Database\Eloquent\Model())->withHidden([]);
\Illuminate\Support\Str::randomBytes(16);
\Illuminate\Support\Str::equals('knownString', 'userInput');

class SomeJob implements \Illuminate\Contracts\Bus\SelfHandling
{
}

class SomeScope implements \Illuminate\Database\Eloquent\ScopeInterface
{
}

class SomeView extends \Illuminate\View\Expression
{
}

?>
-----
<?php

namespace RectorLaravel\Tests\Sets\Laravel53;

(new \Illuminate\Support\Collection())->pluck('id');
(new \Illuminate\Database\Eloquent\Builder())->pluck('id');
(new \Illuminate\Database\Query\Builder())->pluck('id');
(new \Illuminate\Database\Eloquent\Collection())->makeVisible([]);
(new \Illuminate\Database\Eloquent\Model())->makeVisible([]);
\random_bytes(16);
\hash_equals('knownString', 'userInput');

class SomeJob
{
}

class SomeScope implements \Illuminate\Database\Eloquent\Scope
{
}

class SomeView extends \Illuminate\Support\HtmlString
{
}

?>
31 changes: 31 additions & 0 deletions tests/Sets/Laravel53/Laravel53Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Sets\Laravel53;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class Laravel53Test extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
9 changes: 9 additions & 0 deletions tests/Sets/Laravel53/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../config/sets/laravel53.php');
};

0 comments on commit cefbadd

Please sign in to comment.