Rector instantly upgrades and refactors the PHP code of your application. This package is a Rector extension developed by the Laravel community.
Installation
You can install the RectorLaravel package as a dependency:
composer require driftingly/rector-laravel --dev
Use Sets
To add a set to your config, use RectorLaravel\Set\LaravelSetList
class and pick one of the constants:
use RectorLaravel\Set\LaravelSetList;
use Rector\Config\RectorConfig;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
LaravelSetList::LARAVEL_90
]);
};
Laravel Rules for Rector
AddArgumentDefaultValueRector
Adds default value for arguments in defined methods.
configure it!
class: RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector
use Rector\Config\RectorConfig;
use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector;
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(AddArgumentDefaultValueRector::class, [
AddArgumentDefaultValueRector::ADDED_ARGUMENTS => [
new AddArgumentDefaultValue('SomeClass', 'someMethod', 0, false),
],
]);
};
class SomeClass
{
- public function someMethod($value)
+ public function someMethod($value = false)
{
}
}
AddGenericReturnTypeToRelationsRector
Add generic return type to relations in the child of Illuminate\Database\Eloquent\Model
class: RectorLaravel\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector
use App\Account;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Model
{
+ /** @return HasMany<Account> */
public function accounts(): HasMany
{
return $this->hasMany(Account::class);
}
}
AddGuardToLoginEventRector
Add new $guard
argument to Illuminate\Auth\Events\Login
class: RectorLaravel\Rector\New_\AddGuardToLoginEventRector
use Illuminate\Auth\Events\Login;
final class SomeClass
{
public function run(): void
{
- $loginEvent = new Login('user', false);
+ $guard = config('auth.defaults.guard');
+ $loginEvent = new Login($guard, 'user', false);
}
}
AddParentRegisterToEventServiceProviderRector
Add parent::register();
call to register()
class method in child of Illuminate\Foundation\Support\Providers\EventServiceProvider
class: RectorLaravel\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
public function register()
{
+ parent::register();
}
}
There are 25 Laravel Rector Rules in this package. You can see all these rules on Github.
0 comments:
Post a Comment
Thanks