Rector upgrades rules for Laravel

Overview

Rector Rules for Laravel

See available Laravel rules

Install

This package is already part of rector/rector package, so it works out of the box.

All you need to do is install the main package, and you're good to go:

composer require rector/rector --dev

Use Sets

To add a set to your config, use Rector\Laravel\Set\LaravelSetList class and pick one of constants:

use Rector\Laravel\Set\LaravelSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->import(LaravelSetList::LARAVEL_60);
};

Read a First Book About Rector

Are you curious, how Rector works internally, how to create your own rules and test them and why Rector was born? In May 2021 we've released the very first book: Rector - The Power of Automated Refactoring.

By buying a book you directly support maintainers who are working on Rector.


Support

Rector is a tool that we develop and share for free, so anyone can automate their refactoring. But not everyone has dozens of hours to understand complexity of abstract-syntax-tree in their own time. That's why we provide commercial support - to save your time.

Would you like to apply Rector on your code base but don't have time for the struggle with your project? Hire us to get there faster.

Comments
  • Create sets for laravel upgrade rules

    Create sets for laravel upgrade rules

    I check all the rules and upgrade for the laravel 7 to 8 and 8 to 9. I got the rules can we make a set for this laravel upgrade? if you agree then I will do that.

    opened by hirenkeraliya 6
  • Add new code refactor rule

    Add new code refactor rule

    This PR adds a new rule RedirectBackToBackHelperRector to refactor the code around redirect back.

    Most of the Laravel developers redirect to the previous location with the code like this:

    return redirect()->back()->with('...');
    

    But there is a Laravel helper called back that can be called directly and the redirect() call is not needed. The above line can be rewritten as:

    return back()->with('...');
    

    This is my first PR with Rector. Please feel free to share your honest feedback. I am new to AST and related stuff. The Rector book helped me a lot in learning it. Thanks for making this tool.

    opened by hirenkeraliya 6
  • [Laravel] Add RemoveDumpDataDeadCodeRector

    [Laravel] Add RemoveDumpDataDeadCodeRector

    This PR adds a new rule RemoveDumpDataDeadCodeRector to remove the unnecessary dump data code.

    We are Humans And Human makes mistake sometimes we miss removing dump data code like dd or dump function calls.

    That's why I added this new rector class to remove code that the developer forgot to remove.

    opened by hirenkeraliya 4
  • Revert RectorConfig->rule usage on config/config

    Revert RectorConfig->rule usage on config/config

    To avoid error like:

    
                                                                                                                            
     [ERROR] Expected an instance of this class or to this class among his parents                                          
             Rector\Core\Contract\Rector\RectorInterface. Got: string in                                                    
             /Users/samsonasik/www/rector-symfony/config/config.php (which is being imported from                           
             "/Users/samsonasik/www/rector-symfony/rector.php").      
    
    opened by samsonasik 4
  • Add AddGenericReturnTypeToRelationsRector

    Add AddGenericReturnTypeToRelationsRector

    This PR adds a new rector called AddGenericReturnTypeToRelationsRector Which adds generic return type docblocks to Laravel relations in models.

    Laravel relations are not generic. But Larastan (PHPStan extension for Laravel) makes them generic to be able to do more accurate analysis on relations. So this rector is not really useful for normal Laravel users, but only for Larastan users. I hope you'll still accept it though.

    I tried to implement it in a way that it will add @return doc tag if

    • the method has a relation return type (There are other rectors to add missing return types)
    • the method doesn't have any comments at all
    • the method has comments but no existing @return
    • the method only has 1 statement in body and it's return keyword. (this is how almost all Laravel relations are implemented)

    From my local testing the only error I'm getting is about cognitive complexity. I don't know how I can reduce it more. Any tips for that would be great!

    opened by canvural 4
  • Add set for Laravel legacy factories to classes

    Add set for Laravel legacy factories to classes

    Definition

    Before: https://laravel.com/docs/7.x/database-testing#writing-factories After: https://laravel.com/docs/8.x/database-testing#defining-model-factories

    Usage

    Before: https://laravel.com/docs/7.x/database-testing#using-factories After: https://laravel.com/docs/8.x/database-testing#creating-models-using-factories

    Tasks

    • [x] Convert factory definition to class.
    • [x] Use the static factory method instead of global factory function.
    • [x] Applying states by specify the name of each state to simply call the state methods

    Missing jobs:

    • Remove classmap block from the autoload section and add the new namespaced class directory mappings in the composer.json file.
    • Add the Database\Factories namespace to those classes.

    I think the NormalizeNamespaceByPSR4ComposerAutoloadRector can handle these missing jobs well, so these jobs have not been processed for the time being.

    opened by zingimmick 4
  • RouteActionCallableRector with Named Routes

    RouteActionCallableRector with Named Routes

    This currently doesn't work for named routes. I searched through possible configuration options but couldn't find anything.

    Is it possible to do this currently?

    enhancement 
    opened by bradsi 3
  • AnonymousMigrationsRector should check if class is abstract

    AnonymousMigrationsRector should check if class is abstract

    If a migration class is abstract it is meant to be used by other migrations and can not be used as a migration on it's own. I'm willing to provide a PR, but I'm not sure how to check if a class is abstract.

    opened by gisostallenberg 3
  • Can't execute rector-laravel

    Can't execute rector-laravel

    Dear Team,

    I installed rector and rector-laravel reffering this site. And these commands are I executed;

    vagrant@homestead:~/code/laravel-project$ php -v
    PHP 8.0.25 (cli) (built: Oct 28 2022 18:02:51) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v4.0.25, Copyright (c) Zend Technologies
        with Zend OPcache v8.0.25, Copyright (c), by Zend Technologies
    
    vagrant@homestead:~/code$ cd laravel-project/
    vagrant@homestead:~/code/laravel-project$ mkdir --parents tools/rector
    vagrant@homestead:~/code/laravel-project$ composer req --working-dir=tools/rector rector/rector
    ・・・
    vagrant@homestead:~/code/laravel-project$ tools/rector/vendor/bin/rector --version
    Rector 0.14.8
    vagrant@homestead:~/code/laravel-project$ tools/rector/vendor/bin/rector init
    

    Then, edit laravel-project/rector.php :

    declare(strict_types=1);
    
    use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
    use Rector\Config\RectorConfig;
    use Rector\Set\ValueObject\LevelSetList;
    
    return static function (RectorConfig $rectorConfig): void {
        $rectorConfig->paths([
            __DIR__ . '/Flow',
            __DIR__ . '/app',
            __DIR__ . '/bootstrap',
            __DIR__ . '/config',
            __DIR__ . '/database',
            __DIR__ . '/public',
            __DIR__ . '/resources',
            __DIR__ . '/routes',
            __DIR__ . '/storage',
            __DIR__ . '/tests',
            __DIR__ . '/tools',
        ]);
    
        // register a single rule
        $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
    
        // define sets of rules
            $rectorConfig->sets([
                LevelSetList::UP_TO_PHP_80
            ]);
    };
    

    Execute rector.php tools/rector/vendor/bin/rector process --dry-run →run successfully

    Install rector-laravel composer req --working-dir=tools/rector driftingly/rector-laravel Edit laravel-project/rector.php again

    declare(strict_types=1);
    
    use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
    use Rector\Config\RectorConfig;
    use Rector\Set\ValueObject\LevelSetList;
    use RectorLaravel\Set\LaravelSetList;
    
    return static function (RectorConfig $rectorConfig): void {
        $rectorConfig->paths([
            __DIR__ . '/Flow',
            __DIR__ . '/app',
            __DIR__ . '/bootstrap',
            __DIR__ . '/config',
            __DIR__ . '/database',
            __DIR__ . '/public',
            __DIR__ . '/resources',
            __DIR__ . '/routes',
            __DIR__ . '/storage',
            __DIR__ . '/tests',
            __DIR__ . '/tools',
        ]);
    
        // register a single rule
        $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
    
        // define sets of rules
            $rectorConfig->sets([
                UP_TO_LARAVEL_70
            ]);
    };
    

    Execute;

    vagrant@homestead:~/code/laravel-project$ tools/rector/vendor/bin/rector process --dry-run
    [ERROR] Class "Rector\Larave\Set\LaravelSetList" not found
    

    Why this error occured?

    Thanks, Kate.

    opened by KasumiHumphreys 2
  • L8 rules: Seeders are now namespaced

    L8 rules: Seeders are now namespaced

    Hi there,

    After joining a meet-up on Rector (PHP-WVL) I was very curious about the package. I'm now using it to upgrade a L7 to L8 and noticed that there's probably a rule missing, although I'm not sure as it could be that I'm not familiar enough with rector.

    The high impact change is that Seeder classes need namespacing from L8 forward. Is it possible that this rules needs to be added still, if so how can I help?

    opened by coreation 2
  • Add OptionalToNullsafeOperatorRector

    Add OptionalToNullsafeOperatorRector

    Replaces usage of optional() with the nullsafe operator.

    Ref: rfc, https://github.com/laravel/laravel/pull/5670, https://github.com/laravel/framework/pull/38868

    -optional($user)->getKey();
    -optional($user)->id;
    +$user?->getKey();
    +$user?->id;
    
    opened by zingimmick 2
  • Support updating an existing return type with generics

    Support updating an existing return type with generics

    Only supported if the existing PHPDoc return type is the same as the native return type. This way we ensure that we don't change types.

    Not updating of another type was already covered with a test. I added a test for the case when we can update the PHPDoc with only the generics.

    opened by rene-bos 1
  • Rector 0.15.0 - Laravel60|70 - ParamTypeDeclarationRector throws error

    Rector 0.15.0 - Laravel60|70 - ParamTypeDeclarationRector throws error

    Hey,

    Using:

    • Rector 0.15.0
    • Rector-Laravel 0.14.1

    I use the set

    • LaravelLevelSetList::UP_TO_LARAVEL_90,

    When running rector, I get the following error:

    • [ERROR] Use specific rules to infer params instead. This rule was split info many small ones.

    Seems to refer to the following file in rector-laravel:

    • https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel60.php#L26
    • This one might also break: https://github.com/driftingly/rector-laravel/blob/main/config/sets/laravel70.php#L21-L47

    I did some small digging, looks like 0.15.0 of rector now throws an error.

    • https://github.com/rectorphp/rector/blob/0.15.0/rules/TypeDeclaration/Rector/FunctionLike/ParamTypeDeclarationRector.php#L65

    Current solution is to use older version of rector, but thought I'd report this.

    opened by sidneyprins 1
  • Migrate to simplified accessors and mutators in Laravel 9

    Migrate to simplified accessors and mutators in Laravel 9

    Helloo 😃

    Not sure if this Rule is essential, or even doable, but this is my first attempt at moving old accessors and mutators to the new syntax introduced in Laravel 9. I've got it working fine with Accessors, tested them in real-life projects, and got good results. Mutators are a bit more challenging to migrate, and there's some added complexity since we need to put them into the same method now. The test case 'migrate-mutators.php.inc' is currently failing in a weird way, and the code still needs a lot of refactoring.

    Any thoughts, advice, or help is greatly appreciated. Thank you.

    opened by GeniJaho 1
  • Requirement for PHP 8.1

    Requirement for PHP 8.1

    Just wondering why this dependency requires a greater version of PHP (>=8.1) than Rector (^7.2|^8.0) does? It makes it more problematic to use on older versions of PHP. Thanks

    opened by u01jmg3 0
Releases(0.14.2)
  • 0.14.2(Jan 6, 2023)

    What's Changed

    • fix(RequestStaticValidateToInjectRector): check extends method by @lxShaDoWxl in https://github.com/driftingly/rector-laravel/pull/68
    • Fix AddGenericReturnTypeToRelationsRector on Rector 0.15.2 by @rene-bos in https://github.com/driftingly/rector-laravel/pull/74

    New Contributors

    • @lxShaDoWxl made their first contribution in https://github.com/driftingly/rector-laravel/pull/68
    • @rene-bos made their first contribution in https://github.com/driftingly/rector-laravel/pull/74

    Full Changelog: https://github.com/driftingly/rector-laravel/compare/0.14.1...0.14.2

    Source code(tar.gz)
    Source code(zip)
  • 0.14.1(Nov 5, 2022)

    What's Changed

    • Add rector-debugging to require-dev by @samsonasik in https://github.com/driftingly/rector-laravel/pull/62
    • [Laravel] Add named route support on RouteActionCallableRector by @samsonasik in https://github.com/driftingly/rector-laravel/pull/61
    • [Testing] Update test to use yieldFilesFromDirectory by @samsonasik in https://github.com/driftingly/rector-laravel/pull/63
    • Migrate Rector Laravel to the community by @driftingly in https://github.com/driftingly/rector-laravel/pull/65
    • Update composer to Rector 0.14.7 by @driftingly in https://github.com/driftingly/rector-laravel/pull/66

    Full Changelog: https://github.com/driftingly/rector-laravel/compare/0.14.0...0.14.1

    Source code(tar.gz)
    Source code(zip)
Rector upgrades rules for Nette

Rector Rules for Nette See available Nette rules Install composer require rector/rector-nette Use Sets To add a set to your config, use Rector\Nette\S

RectorPHP 21 Nov 7, 2022
Rector upgrades rules for Symfony Framework

Rector Rules for Symfony See available Symfony rules Install This package is already part of rector/rector package, so it works out of the box. All yo

Rector 107 Dec 31, 2022
PHPStan Rules for Rector developers

Rector PHPStan Rules PHPStan rules for Rector and projects that maintain Rector rulesets. Install composer require rector/phpstan-rules --dev Do you u

RectorPHP 12 Dec 22, 2022
Rector rules for Nette to Symfony migration

Rector Nette to Symfony Do you need to migrate from Nette to Symfony? You can ↓ How we Migrated 54 357-lines Application from Nette to Symfony in 2 Pe

Rector 8 May 30, 2022
Instant Upgrades and Instant Refactoring of any PHP 5.3+ code

Rector - Speedup Your PHP Development Rector helps you with 2 areas - major code changes and in daily work. Do you have a legacy code base? Do you wan

RectorPHP 6.5k Jan 8, 2023
phpcs-security-audit is a set of PHP_CodeSniffer rules that finds vulnerabilities and weaknesses related to security in PHP code

phpcs-security-audit v3 About phpcs-security-audit is a set of PHP_CodeSniffer rules that finds vulnerabilities and weaknesses related to security in

Floe design + technologies 655 Jan 3, 2023
Automagically generate UML diagrams of your Laravel code.

Laravel UML Diagram Generator Automagically generate UML diagrams of your Laravel code. Installation To install LTU via composer, run the command: com

Andy Abi Haidar 93 Jan 1, 2023
A fake mailer for Laravel Applications for testing mail.

MailThief MailThief is a fake mailer for Laravel applications (5.0+) that makes it easy to test mail without actually sending any emails. Note: Due to

Tighten 688 Nov 29, 2022
A toolbar for Laravel Telescope, based on the Symfony Web Profiler.

Laravel Telescope Toolbar Extends Laravel Telescope to show a powerful Toolbar See https://github.com/laravel/telescope Install First install Telescop

Fruitcake 733 Dec 30, 2022
A rate limiter for Laravel

Laravel Throttle Laravel Throttle was created by, and is maintained by Graham Campbell, and is a rate limiter for Laravel. Feel free to check out the

Graham Campbell 673 Dec 30, 2022
A package to help you clean up your controllers in laravel

?? Laravel Terminator ?? ?? "Tell, don't ask principle" for your laravel controllers What this package is good for ? Short answer : This package helps

Iman 241 Oct 10, 2022
Rector upgrades rules for CakePHP

Rector Rules for CakePHP See available CakePHP rules Install composer require rector/rector-cakephp Use Sets To add a set to your config, use Rector\C

RectorPHP 19 Oct 27, 2022
Rector upgrades rules for Nette

Rector Rules for Nette See available Nette rules Install composer require rector/rector-nette Use Sets To add a set to your config, use Rector\Nette\S

RectorPHP 21 Nov 7, 2022
Rector upgrades rules for PHPUnit

Rector Rules for PHPUnit See available PHPUnit rules Install composer require rector/rector-phpunit Use Sets To add a set to your config, use Rector\P

RectorPHP 34 Dec 27, 2022
Rector upgrades rules for Doctrine

Rector Rules for Doctrine See available Doctrine rules Install This package is already part of rector/rector package, so it works out of the box.

Rector 37 Nov 7, 2022
Rector upgrades rules for Symfony Framework

Rector Rules for Symfony See available Symfony rules Install This package is already part of rector/rector package, so it works out of the box. All yo

Rector 107 Dec 31, 2022
Rector - Instant Upgrades and Automated Refactoring

Rector - Instant Upgrades and Automated Refactoring This repository (rectorphp/rector-src) is for development Rector only. Head to rectorphp/rector fo

Rector 64 Dec 27, 2022
PHPStan Rules for Rector developers

Rector PHPStan Rules PHPStan rules for Rector and projects that maintain Rector rulesets. Install composer require rector/phpstan-rules --dev Do you u

RectorPHP 12 Dec 22, 2022
Rector rules for Nette to Symfony migration

Rector Nette to Symfony Do you need to migrate from Nette to Symfony? You can ↓ How we Migrated 54 357-lines Application from Nette to Symfony in 2 Pe

Rector 8 May 30, 2022
Rector Rules for BEAR.Sunday

Rector Rules for BEAR.Sunday The rector/rector rules for BEAR.Sunday.

BEAR.Sunday 2 Mar 30, 2022