TomasVotruba / bladestan

PHPStan analysis for Blade templates
https://tomasvotruba.com/blog/introducing-bladestan-phpstan-analysis-of-blade-templates/
MIT License
293 stars 14 forks source link

The array shape of view() is not picked up if given as a variable #107

Closed williamdes closed 2 months ago

williamdes commented 2 months ago
        $data = [
            // ...
            'user' => $userBloc->toArray(),
        ];

        $response = view('viewName', $data)->render();

It says

  185    Undefined variable: $user                                                              
         rendered in: mandat/partials/viewName.blade.php:3 

But if I use:

        $response = view('viewName', [
            // ...
            'user' => $userBloc->toArray(),
        ])->render();

There is no more error

williamdes commented 2 months ago

Same with this pattern


        return $this->to('admin@xxxx.xx')
            ->subject('Title here:' . $this->ressourceId)
            ->view('email.payinFailed')
            ->with([
                'payinId' => $this->ressourceId,
            ]);
  41     Variable $payinId might not be defined.             
         rendered in: email/payinFailed.blade.php:13  
williamdes commented 2 months ago

@TomasVotruba I am willing to do a one time sponsoring of $50 to get this issue fixed as I really need to have phpstan working for my blade templates (incoming refactoring) Is that okay for you ?

AJenbo commented 2 months ago

Same with this pattern

        return $this->to('admin@xxxx.xx')
            ->subject('Title here:' . $this->ressourceId)
            ->view('email.payinFailed')
            ->with([
                'payinId' => $this->ressourceId,
            ]);

This one might be simple to solve since it's supported on the global view() function: https://github.com/TomasVotruba/bladestan/blob/53e514cd891ed059a7f8259edd6f5165fe4a23f6/tests/Rules/Fixture/laravel-view-function.php#L22

TomasVotruba commented 2 months ago

@williamdes Thanks :+1: Make sure you sponsor @AJenbo, he's the main contributor last couple of years :muscle:

AJenbo commented 2 months ago

I'm not really setup to take sponsor ships and I maintain it though my employer so not sure how that would work. If any one else is interested in implementing the feature and receiving the sponsor ship then I'm at least happy to do review and support of it.

williamdes commented 2 months ago

I'm not really setup to take sponsor ships and I maintain it though my employer so not sure how that would work. If any one else is interested in implementing the feature and receiving the sponsor ship then I'm at least happy to do review and support of it.

Definitely check if you can setup GH sponsors or send me a PayPal link for a donation 👍👍💪

AJenbo commented 2 months ago

Sorry this conflicts with my employment.

williamdes commented 2 months ago

Sorry this conflicts with my employment.

That's okay, I totally understand

AJenbo commented 2 months ago

What's your horizon on needing this ?

williamdes commented 2 months ago

What's your horizon on needing this ?

I would say this week of the next one I did not find a phpdoc hack to workaround this bug :/ Definitely feeling terrible for putting deadlines on an open source project..

AJenbo commented 2 months ago

If no one picks up the bounty I might take a stab at it in the next 2-3 weeks.

mrhn commented 2 months ago

Met @AJenbo at a conference as he is my old coworker, someone talked about PHPParsing, this was mentioned and i gave it a try with this PR. There was some new challenges compared to some of the other analyzers they made, so we probably need some changes to make it more "non hacky".

But there is hope out there and the testcase i made passed, similar to the first example you had, if we get that PR moving i would gladly take a look at the other example you presented.

williamdes commented 2 months ago

Awesome @mrhn Let me know when you want that I test the PR, even in draft state I made a separate baseline file so it's easy to spot un handled cases

AJenbo commented 2 months ago

Same with this pattern

        return $this->to('admin@xxxx.xx')
            ->subject('Title here:' . $this->ressourceId)
            ->view('email.payinFailed')
            ->with([
                'payinId' => $this->ressourceId,
            ]);
  41     Variable $payinId might not be defined.             
         rendered in: email/payinFailed.blade.php:13  

With out knowing what class type this code appears in we won't really know what the issue might be here or how to solve it. I'm guessing it's a Illuminate\Mail\Mailable? Have you tried changing the code to this:

        $this->to('admin@xxxx.xx')
            ->subject('Title here:' . $this->ressourceId);
        return $this->view('email.payinFailed')
            ->with([
                'payinId' => $this->ressourceId,
            ]);

If that works then it's more likely that the return value of to() or subject() are not properly hinted in Laravel/Larastan and so the call to view() looks like it's not a known blade function.

AJenbo commented 2 months ago

Fixed by https://github.com/TomasVotruba/bladestan/pull/108

(support for Illuminate\Mail\Mailable::view()->with() will be tracked separately)