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

IDE Helpers Prototype

Open Wulfheart opened this issue 3 years ago • 14 comments

Problem

Given the following action:

class SendTeamReportEmail
{
    use AsAction;

    public function handle(Team $team, bool $fullReport): void
    {
        // ...
    }
}

When I'm trying to e.g. dispatch a job via SendTeamReportEmail::dispatch() I don't get method signature.

image

Therefore I wrote a package which analyzes all actions in a project and creates an ide-helper file like https://github.com/barryvdh/laravel-ide-helper.

Installation

composer require --dev wulfheart/laravel-actions-ide-helper

Usage

php artisan ide-helper:actions

Result

image

Limitations

It is just a prototype. Therefore the package currently has some limitations.

Ignoring the decorator functions

class SendTeamReportEmail
{
    use AsAction;

    public function handle(Team $team, bool $fullReport): void
    {
        // Prepare report and send it to all $team->users.
    }

    public function asJob(Team $team): void
    {
        $this->handle($team, true);
    }
}

If I understand the inner workings of Laravel Actions correctly this action has now some other parameters applied if it is dispatched as Job. Currently it assumes the parameters of the handle function. I will have to fix this.

No default value

If a parameter has default value (e.g. bool $fullReport = true) the default value doesn't get recognized. This will get fixed later.

Opinion

@lorisleiva What is your opinion on this? Do you think this could be an addition to Laravel Actions or a separate package to improve the developer experience?

Wulfheart avatar May 25 '21 21:05 Wulfheart

Update: The package now supports autocompletion for some decorator functions in an action.

image

Furthermore are the different decorators in an action recognized and the parameters of the accessor funtions like makeJob() are set properly. Default types are working now.

Wulfheart avatar May 26 '21 21:05 Wulfheart

Hey Alex 👋

I really like this! I currently fix this for the run method manually by adding a @method docblock on the class itself that reflects the handle method parameters and return type.

Having something that generates it for me for all methods supported by Laravel Actions is really great!

I think it sit well as a third-party package though because we can't assume everyone is using PHPStorm but I will be using it for sure.

If you're up for it, maybe you could add a new page on the Laravel Actions documentation under the "Guide" section that acts as a little tutorial to install and use your package?

Thank you for your work on improving the LA ecosystem I truly appreciate it.

P.S.: I'll catch up with some of the other issues soon. I've been quite busy with freelance work lately but it's certainly on my To-Do list.

lorisleiva avatar May 30 '21 16:05 lorisleiva

I think it sit well as a third-party package though because we can't assume everyone is using PHPStorm but I will be using it for sure.

Packages like laravel-ide-helper also work with vscode.

If you're up for it, maybe you could add a new page on the Laravel Actions documentation under the "Guide" section that acts as a little tutorial to install and use your package?

Yes, will open a PR later.

Thank you for your work on improving the LA ecosystem I truly appreciate it.

Thank you for the kind words. I really like LA because it gives me flexibility.

Wulfheart avatar May 30 '21 16:05 Wulfheart

Packages like laravel-ide-helper also work with vscode.

Oh I did not know that. It's up to you really. I'm all up for adding wulfheart/laravel-actions-ide-helper to Laravel Actions' dependencies in order to make it easier for users to generate these IDE helpers.

lorisleiva avatar May 30 '21 17:05 lorisleiva

Ah, the minimum requirement is PHP 8, therefore I'm just going to reference it in the docs.

Wulfheart avatar May 30 '21 19:05 Wulfheart

Ah no worries. I'll probably drop support for 7.4 in a future release so I'll keep that in mind when I do. 🙂

lorisleiva avatar May 31 '21 08:05 lorisleiva

@lorisleiva Just published a new version. Even though I wrote tests for it it would be nice if you could take a look at it if you encounter any problems with it. Therefore, I am going to reopen this issue, but feel free to close it if it is out of your current scope.

Wulfheart avatar Feb 19 '22 21:02 Wulfheart

Hey Alex, Thanks for that. I don't have time to fully test that at the moment (just started a new job haha) but you've been so helpful with this package I really trust your judgment. Feel free to open up a PR if you want Laravel Actions to ship with this IDE helper (if you think it should).

lorisleiva avatar Feb 21 '22 11:02 lorisleiva