TomasVotruba / bladestan

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

Support using with() for providing $data to Mailable::view() #111

Open williamdes opened 1 week ago

williamdes commented 1 week ago

Test case: https://github.com/TomasVotruba/bladestan/pull/109

As reported in https://github.com/TomasVotruba/bladestan/pull/108#issuecomment-2309072935

<?php

namespace App\Jobs\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class PayinFailedForUser extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * @var string
     */
    private $ressourceId;

    /**
     * Create a new message instance.
     */
    public function __construct(string $ressourceId)
    {
        $this->ressourceId = $ressourceId;
        $this->connection = config('app.queue_driver_email');
    }

    /**
     * Build the message.
     *
     * @return self
     */
    public function build()
    {

        $this->withSymfonyMessage(function (\Symfony\Component\Mime\Email $message) {
            $message->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'xxxxx-production');
            $message->getHeaders()->addTextHeader('X-SES-MESSAGE-TAGS', 'foo=bar, email_type=payin_failed');
        });

        return $this->to('admin@example.eu')
            ->subject('[Payin] n°' . $this->ressourceId)
            ->view('email.toUser.payinFailed')
            ->with([
                'payinId' => $this->ressourceId,
            ]);
    }
}

The baseline item is


        -
            message: "#^Variable \\$payinId might not be defined\\.$#"
            count: 3
            path: app/Jobs/Mail/PayinFailedForUser.php

This was the official way to do emails in Laravel 8, still works in Laravel 10. See: https://laravel.com/docs/8.x/mail#using-the-from-method

Related:

williamdes commented 1 week ago

Moving view and with into a function fixes it


    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'email.toUser.payinFailed',
            with: [
                'payinId' => $this->ressourceId,
            ],
        );
    }

Added in Laravel by: https://github.com/laravel/framework/pull/44462

I don't think Bladestan knows how to analyze Illuminate\Mail\Mailables\Content so this will just result in the template being ignored.

Indeed Added support in #114

AJenbo commented 1 week ago

I don't think Bladestan knows how to analyze Illuminate\Mail\Mailables\Content so this will just result in the template being ignored.

AJenbo commented 1 week ago

Here is what you can do instead and still have it analyzed:

        return $this->to('admin@example.eu')
            ->subject('[Payin] n°' . $this->ressourceId)
            ->html(view('email.toUser.payinFailed')
            ->with([
                'payinId' => $this->ressourceId,
            ]));

That's just 6 extra chars :)