rector-laravel icon indicating copy to clipboard operation
rector-laravel copied to clipboard

Livewire set and first rule for QueryString to Url attributes

Open peterfox opened this issue 1 year ago • 10 comments

Changes

  • adds a Livewire 3.0 set to the repository.
  • adds a new rule for Livewire to convert query strings to using Url attributes.

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 = '';
}

peterfox avatar Jun 22 '24 17:06 peterfox

what will happen in this case?

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

protected $queryString = [
    'foo',
    'search' => ['except' => ''],
    'page' => ['except' => 1],
];

MrPunyapal avatar Jun 24 '24 14:06 MrPunyapal

@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

peterfox avatar Jun 24 '24 16:06 peterfox

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

MrPunyapal avatar Jun 24 '24 19:06 MrPunyapal

Okay cool. That should be simple enough to resolve 👍

peterfox avatar Jun 24 '24 20:06 peterfox

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

peterfox avatar Jun 26 '24 22:06 peterfox

I guess we missed something? 🤔

The whole queryString property isn't deprecated 🤔

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

MrPunyapal avatar Jun 27 '24 02:06 MrPunyapal

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.

peterfox avatar Jun 27 '24 08:06 peterfox

@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

MrPunyapal avatar Jun 27 '24 13:06 MrPunyapal

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

peterfox avatar Jun 27 '24 21:06 peterfox

@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 🤔

MrPunyapal avatar Jun 28 '24 00:06 MrPunyapal

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

peterfox avatar Jul 12 '24 10:07 peterfox

looks good

MrPunyapal avatar Jul 17 '24 15:07 MrPunyapal