driftingly / rector-laravel

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

Livewire set and first rule for QueryString to Url attributes #222

Closed peterfox closed 6 days ago

peterfox commented 1 month ago

Changes

Why

Livewire 3.0 is an official package for Laravel and a popular one. Livewire 3.0 introduced some changes to components from past versions. It makes sense for those rules to still live within the Laravel package.

Example of Rule use

The rule will perform a simple conversion. It avoids applying if there's a mismatch between what's in the array and the properties existing.

use Livewire\Component;

class MyComponent extends Component
{
    public string $something = '';

    public string $another = '';

    protected $queryString = [
        'something',
        'another',
    ];
}

becomes

use Livewire\Component;

class MyComponent extends Component
{
    #[\Livewire\Attributes\Url]
    public string $something = '';

    #[\Livewire\Attributes\Url]
    public string $another = '';
}
MrPunyapal commented 1 month ago

what will happen in this case?

public $foo;
public $search = '';
public $page = 1;

protected $queryString = [
    'foo',
    'search' => ['except' => ''],
    'page' => ['except' => 1],
];
peterfox commented 1 month ago

@MrPunyapal currently it will ignore it. Do you know what the translation of that would be with the attribute? I forgot that was an option available

MrPunyapal commented 1 month ago

That feature is no longer available.

So first do key to value.

- 'property' => ['except' => 'something'],
+ 'property' 

Then just the same as what we did previously

Maybe this helps: https://livewire.laravel.com/docs/upgrading#url-query-string

peterfox commented 1 month ago

Okay cool. That should be simple enough to resolve 👍

peterfox commented 4 weeks ago

@MrPunyapal I've now adapted for the scenario mentioned where the array's key might be the property.

MrPunyapal commented 4 weeks ago

I guess we missed something? 🤔

The whole queryString property isn't deprecated 🤔

https://livewire.laravel.com/docs/upgrading#url-query-string

peterfox commented 3 weeks ago

Doesn't really matter if it's not deprecated. The rule is still useful for those who want to convert from Livewire 2 to 3 as it covers most of the scenarios. I guess the only thing that should be done is to ignore any instances where 'keep' => true is used.

MrPunyapal commented 3 weeks ago

@peterfox I haven't found much in documentation 😃

Then after looking into code i think we need to handle more than one case? 🤔 Attribute: https://github.com/livewire/livewire/blob/main/src%2FFeatures%2FSupportQueryString%2FBaseUrl.php#L12-L18

Legacy support: https://github.com/livewire/livewire/blob/main/src%2FFeatures%2FSupportQueryString%2FSupportQueryString.php#L20-L26

peterfox commented 3 weeks ago

Okay cool, so it's probably best to create the attributes with the parameters if set.

MrPunyapal commented 3 weeks ago

@MrPunyapal currently it will ignore it. Do you know what the translation of that would be with the attribute? I forgot that was an option available

Yeah revert to this for now 🤔

peterfox commented 1 week ago

@MrPunyapal new version of the rule. Now covers transforming cases of 'as' and 'except' to be converted with the attributes into arguments.

MrPunyapal commented 1 week ago

looks good