Find the best Rector rule to solve your problem. Searching through 1010 rules.
Found 56 rules:
Changes model getRouteKeyName() method to use the RouteKey attribute
use Illuminate\Database\Eloquent\Model;
+#[\Illuminate\Database\Eloquent\Attributes\RouteKey('slug')]
class Post extends Model
{
- public function getRouteKeyName(): string
- {
- return 'slug';
- }
}
Changes the delay property to use the Delay attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Delay;
+#[Delay(10)]
final class ProcessPodcast implements ShouldQueue
{
- public $delay = 10;
}
Changes model timestamps = false property to use the WithoutTimestamps attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;
+#[WithoutTimestamps]
class EventLog extends Model
{
- public $timestamps = false;
}
Changes model incrementing = false property to use the WithoutIncrementing attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\WithoutIncrementing;
+#[WithoutIncrementing]
class User extends Model
{
- public $incrementing = false;
}
Changes the aliases property to use the Aliases attribute
use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Aliases;
+#[Aliases(['email:send'])]
class SendEmails extends Command
{
- protected $aliases = ['email:send'];
}
Changes model dateFormat property to use the DateFormat attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\DateFormat;
+#[DateFormat('U')]
class User extends Model
{
- protected $dateFormat = 'U';
}
Changes model visible property to use the Visible attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Visible;
+#[Visible(['name', 'email'])]
class User extends Model
{
- protected $visible = [
- 'name',
- 'email',
- ];
}
Changes model table-related properties to use the Table attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Table;
+#[Table(name: 'users', key: 'user_id', keyType: 'string', incrementing: false)]
class User extends Model
{
- protected $table = 'users';
-
- protected $primaryKey = 'user_id';
-
- protected $keyType = 'string';
-
- protected $incrementing = false;
}
Changes model fillable property to use the fillable attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Fillable;
+#[Fillable(['name', 'email'])]
class User extends Model
{
- protected $fillable = [
- 'name',
- 'email',
- ];
}
Changes model empty guarded property to use the Unguarded attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Unguarded;
+#[Unguarded]
class User extends Model
{
- protected $guarded = [];
}
Changes model guarded property to use the guarded attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Guarded;
+#[Guarded(['is_admin'])]
class User extends Model
{
- protected $guarded = [
- 'is_admin',
- ];
}
Changes the hidden property to use the Hidden attribute on console commands
use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Hidden;
+#[Hidden]
class SendEmails extends Command
{
- protected $hidden = true;
}
Changes the help property to use the Help attribute
use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Help;
+#[Help('This command sends emails to all users')]
class SendEmails extends Command
{
- protected $help = 'This command sends emails to all users';
}
Changes model appends property to use the appends attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Appends;
+#[Appends(['full_name'])]
class User extends Model
{
- protected $appends = [
- 'full_name',
- ];
}
Changes the backoff property to use the Backoff attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Backoff;
+#[Backoff(3)]
final class ProcessPodcast implements ShouldQueue
{
- public $backoff = 3;
}
Changes the maxExceptions property to use the MaxExceptions attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\MaxExceptions;
+#[MaxExceptions(3)]
final class ProcessPodcast implements ShouldQueue
{
- public $maxExceptions = 3;
}
Changes the signature property to use the Signature attribute
use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Signature;
+#[Signature('mail:send {user}')]
class SendEmails extends Command
{
- protected $signature = 'mail:send {user}';
}
Changes the timeout property to use the Timeout attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Timeout;
+#[Timeout(120)]
final class ProcessPodcast implements ShouldQueue
{
- public $timeout = 120;
}
Changes the connection property to use the Connection attribute on queue jobs
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Connection;
+#[Connection('redis')]
final class ProcessPodcast implements ShouldQueue
{
- public $connection = 'redis';
}
Changes the collects property to use the Collects attribute
use App\Http\Resources\UserResource;
+use Illuminate\Http\Resources\Attributes\Collects;
use Illuminate\Http\Resources\Json\JsonResource;
+#[Collects(UserResource::class)]
class UserCollection extends JsonResource
{
- protected $collects = UserResource::class;
}
Changes the queue property to use the Queue attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Queue;
+#[Queue('podcasts')]
final class ProcessPodcast implements ShouldQueue
{
- public $queue = 'podcasts';
}
Changes the failOnTimeout property to use the FailOnTimeout attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\FailOnTimeout;
+#[FailOnTimeout(true)]
final class ProcessPodcast implements ShouldQueue
{
- public $failOnTimeout = true;
}
Changes the uniqueFor property to use the UniqueFor attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\UniqueFor;
+#[UniqueFor(1800)]
final class ProcessPodcast implements ShouldQueue
{
- public $uniqueFor = 1800;
}
Changes model connection property to use the Connection attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Connection;
+#[Connection('sqlite')]
class User extends Model
{
- protected $connection = 'sqlite';
}
Changes model touches property to use the touches attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Touches;
+#[Touches(['posts'])]
class User extends Model
{
- protected $touches = [
- 'posts',
- ];
}
Changes the stopOnFirstFailure property to use the StopOnFirstFailure attribute
use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Foundation\Http\Attributes\StopOnFirstFailure;
+#[StopOnFirstFailure]
class StorePostRequest extends FormRequest
{
- protected $stopOnFirstFailure = true;
}
Changes the preserveKeys property to use the PreserveKeys attribute
+use Illuminate\Http\Resources\Attributes\PreserveKeys;
use Illuminate\Http\Resources\Json\JsonResource;
+#[PreserveKeys]
class UserResource extends JsonResource
{
- protected $preserveKeys = true;
}
Changes model hidden property to use the hidden attribute
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Hidden;
+#[Hidden(['password'])]
class User extends Model
{
- protected $hidden = [
- 'password',
- ];
}
Changes the tries property to use the Tries attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Tries;
+#[Tries(3)]
final class ProcessPodcast implements ShouldQueue
{
- public $tries = 3;
}
Changes the description property to use the Description attribute
use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Description;
+#[Description('Send marketing emails to users')]
class SendEmails extends Command
{
- protected $description = 'Send marketing emails to users';
}
Changes the errorBag property to use the ErrorBag attribute
use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Foundation\Http\Attributes\ErrorBag;
+#[ErrorBag('custom')]
class StorePostRequest extends FormRequest
{
- protected $errorBag = 'custom';
}
Changes model scope methods to use the scope attribute
class User extends Model
{
- public function scopeActive($query)
+ #[\Illuminate\Database\Eloquent\Attributes\Scope]
+ protected function active($query)
{
return $query->where('active', 1);
}
}
Drop the specified abstract class from the bind method and replace it with a closure that returns the abstract class.
-$this->app->bind(SomeClass::class, function (): SomeClass {
+$this->app->bind(function (): SomeClass {
return new SomeClass();
});
Changes model newCollection method to use the CollectedBy attribute
use Illuminate\Database\Eloquent\Model;
-use Illuminate\Database\Eloquent\Collection;
use App\Collections\UserCollection;
+use Illuminate\Database\Eloquent\Attributes\CollectedBy;
+#[CollectedBy(UserCollection::class)]
class User extends Model
{
- /**
- * @param array<int, \Illuminate\Database\Eloquent\Model> $models
- * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model>
- */
- public function newCollection(array $models = []): Collection
- {
- return new UserCollection($models);
- }
}
Changes the deleteWhenMissingModels property to use the DeleteWhenMissingModels attribute
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
+#[DeleteWhenMissingModels]
final class ProcessPodcast implements ShouldQueue
{
- public $deleteWhenMissingModels = true;
}
refactors calls with the pre Laravel 11 methods for blueprint geometry columns
-$blueprint->point('coordinates')->spatialIndex();
+$blueprint->geometry('coordinates', 'point')->spatialIndex();
Replace assertSee with assertSeeHtml when testing HTML with escape set to false
-$response->assertSee("<li>foo</li>", false);
+$response->assertSeeHtml("<li>foo</li>");
Replace Dispatchable, InteractsWithQueue, Queueable, and SerializesModels traits with the Queueable trait
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SomeJob
{
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+ use \Illuminate\Foundation\Queue\Queueable;
}
Refactor Model $casts property with casts() method
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
- protected $casts = [
- 'age' => 'integer',
- ];
+ protected function casts(): array
+ {
+ return [
+ 'age' => 'integer',
+ ];
+ }
}
Convert DB Expression __toString() calls to getValue() method calls.
use Illuminate\Support\Facades\DB;
-$string = DB::raw('select 1')->__toString();
+$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar());
Replace withoutJobs, withoutEvents and withoutNotifications with Facade fake
-$this->withoutJobs();
-$this->withoutEvents();
-$this->withoutNotifications();
+\Illuminate\Support\Facades\Bus::fake();
+\Illuminate\Support\Facades\Event::fake();
+\Illuminate\Support\Facades\Notification::fake();
Dispatch non ShouldQueue jobs to dispatchSync
-dispatch(new SomeJob());
-Bus::dispatch(new SomeJob());
-$this->dispatch(new SomeJob());
+dispatch_sync(new SomeJob());
+Bus::dispatchSync(new SomeJob());
+$this->dispatchSync(new SomeJob());
Convert DB Expression string casts to getValue() method calls.
use Illuminate\Support\Facades\DB;
-$string = (string) DB::raw('select 1');
+$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar());
Replace assertTimesSent with assertSentTimes
-Notification::assertTimesSent(1, SomeNotification::class);
+Notification::assertSentTimes(SomeNotification::class, 1);
Unify Model $dates property with $casts
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $casts = [
- 'age' => 'integer',
+ 'age' => 'integer', 'birthday' => 'datetime',
];
-
- protected $dates = ['birthday'];
}
Replace expectJobs and expectEvents methods in tests
use Illuminate\Foundation\Testing\TestCase;
class SomethingTest extends TestCase
{
public function testSomething()
{
- $this->expectsJobs([\App\Jobs\SomeJob::class, \App\Jobs\SomeOtherJob::class]);
- $this->expectsEvents(\App\Events\SomeEvent::class);
- $this->doesntExpectEvents(\App\Events\SomeOtherEvent::class);
+ \Illuminate\Support\Facades\Bus::fake([\App\Jobs\SomeJob::class, \App\Jobs\SomeOtherJob::class]);
+ \Illuminate\Support\Facades\Event::fake([\App\Events\SomeEvent::class, \App\Events\SomeOtherEvent::class]);
$this->get('/');
+
+ \Illuminate\Support\Facades\Bus::assertDispatched(\App\Jobs\SomeJob::class);
+ \Illuminate\Support\Facades\Bus::assertDispatched(\App\Jobs\SomeOtherJob::class);
+ \Illuminate\Support\Facades\Event::assertDispatched(\App\Events\SomeEvent::class);
+ \Illuminate\Support\Facades\Event::assertNotDispatched(\App\Events\SomeOtherEvent::class);
}
}
Replace $this->faker with the fake() helper function in Factories
class UserFactory extends Factory
{
public function definition()
{
return [
- 'name' => $this->faker->name,
- 'email' => $this->faker->unique()->safeEmail,
+ 'name' => fake()->name,
+ 'email' => fake()->unique()->safeEmail,
];
}
}
Adds the @extends annotation to Factories.
use Illuminate\Database\Eloquent\Factories\Factory;
+/**
+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
+ */
class UserFactory extends Factory
{
protected $model = \App\Models\User::class;
}
Migrate to the new Model attributes syntax
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
- public function getFirstNameAttribute($value)
- {
- return ucfirst($value);
- }
-
- public function setFirstNameAttribute($value)
+ protected function firstName(): \Illuminate\Database\Eloquent\Casts\Attribute
{
- $this->attributes['first_name'] = strtolower($value);
- $this->attributes['first_name_upper'] = strtoupper($value);
+ return \Illuminate\Database\Eloquent\Casts\Attribute::make(get: function ($value) {
+ return ucfirst($value);
+ }, set: function ($value) {
+ return ['first_name' => strtolower($value), 'first_name_upper' => strtoupper($value)];
+ });
}
}
Add parent::register(); call to register() class method in child of Illuminate\Foundation\Support\Providers\EventServiceProvider
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
public function register()
{
+ parent::register();
}
}
Change deprecated $defer = true; to Illuminate\Contracts\Support\DeferrableProvider interface
use Illuminate\Support\ServiceProvider;
+use Illuminate\Contracts\Support\DeferrableProvider;
-final class SomeServiceProvider extends ServiceProvider
+final class SomeServiceProvider extends ServiceProvider implements DeferrableProvider
{
- /**
- * @var bool
- */
- protected $defer = true;
}
Refactor whereDate() queries to include both date and time comparisons with Carbon
use Illuminate\Database\Query\Builder;
final class SomeClass
{
public function run(Builder $query)
{
- $query->whereDate('created_at', '<', Carbon::now());
+ $dateTime = Carbon::now();
+ $query->whereDate('created_at', '<=', $dateTime);
+ $query->whereTime('created_at', '<=', $dateTime);
}
}
Change "redirect" call with 301 to "permanentRedirect"
class SomeClass
{
public function run()
{
- Illuminate\Routing\Route::redirect('/foo', '/bar', 301);
+ Illuminate\Routing\Route::permanentRedirect('/foo', '/bar');
}
}
Add new $guard argument to Illuminate\Auth\Events\Login
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);
}
}
Add "$this->mockConsoleOutput = false"; to console tests that work with output content
use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\TestCase;
final class SomeTest extends TestCase
{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->mockConsoleOutput = false;
+ }
+
public function test(): void
{
$this->assertEquals('content', \trim((new Artisan())::output()));
}
}
Add parent::boot(); call to boot() class method in child of Illuminate\Database\Eloquent\Model
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function boot()
{
+ parent::boot();
}
}