driftingly / rector-laravel

Rector upgrades rules for Laravel
http://getrector.org
MIT License
469 stars 48 forks source link

Adds ReplaceServiceContainerCallArgRector #189

Closed peterfox closed 4 months ago

peterfox commented 4 months ago

Changes

Adds a new ReplaceServiceContainerCallArgRector rule Adds a ReplaceServiceContainerCallArg value object Adds tests Updates the docs

Why

A neat little rule here which can be used in a set in the future to remove uses of string alias' to the service container.

-app('encrypter')->encrypt('...');
-\Illuminate\Support\Facades\Application::make('encrypter')->encrypt('...');
+app(Illuminate\Contracts\Encryption\Encrypter::class)->encrypt('...');
+\Illuminate\Support\Facades\Application::make(Illuminate\Contracts\Encryption\Encrypter::class)->encrypt('...');

Configuration of the rule is as simple as:

new ReplaceServiceContainerCallArg(
    'encrypter',
    new ClassConstFetch(
        new FullyQualified('Illuminate\Contracts\Encryption\Encrypter'),
        'class'
    ),
),

You can also do it the other way around if you for some reason want to swap a Class const for a string.

driftingly commented 4 months ago

This is great. Thanks @peterfox Feel free to merge when ready 🚀