shawnveltman/livewire-3-property-updater

A package to update Livewire 2 computed properties to use the Livewire 3 syntax

1.0.5 2023-10-01 04:44 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

In Livewire 2, a computed property "foo" would be defined like this:

public function getFooProperty()
{
    return 'bar';
}

In Livewire 3, the same property would be defined like this:

#[Computed]
public function foo()
{
    return 'bar';
}

This package automates that update in your Livewire components folder.

Installation

You can install the package via composer:

composer require --dev shawnveltman/livewire-3-property-updater

You can publish the config file with:

php artisan vendor:publish --tag="livewire-3-property-updater-config"

This is the contents of the published config file:

return [
    'start_directory' => 'app/Livewire',  // Defaulting to the app directory, but users can change this.
    'disk' => 'base_path',
    'method_name_style' => 'snake_case', // StudlyCase or  snake_case
];

Two notes on this. First and foremost, the package expects you to have a disk called 'base_path' set up (typically in config/filesystems.php) which points to the base path of the app.

 'base_path' => [
            'driver' => 'local',
            'root'   => base_path(),
        ],

Second - I'm the kind of monster that GREATLY prefers snake_case for method names, so that's the default, but it's easy enough to change it to StudlyCase if that's your preference

Usage for updating computed properties

php artisan shawnveltman:livewire-3-property-updater

Check For Invalid Attempts To Set Property To Null in Livewire 3

In Livewire 3, setting a computed property to null will cause an error. After updating, you might find instances in your code where properties are explicitly set to null. This command automates the process of identifying and updating these instances to use unset instead.

When running this command, it'll scan the Livewire components in your specified directory and look for computed properties being set to null. It will replace these instances with the unset function, preventing potential errors in Livewire 3.

Usage for checking null assignments

php artisan shawnveltman:livewire-null-property-updater

Identify Dispatch Patterns in Livewire

The shawnveltman:dispatch-identifier command scans your Livewire components for specific dispatch patterns. It's especially useful when identifying or refactoring certain dispatch() method usages in Livewire.

The command filters out dispatches with named arguments and presents an output of file paths with the line numbers where the dispatch() method matches the targeted pattern.

Usage for identifying dispatch patterns

php artisan shawnveltman:dispatch-identifier

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.