TomasVotruba / bladestan

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

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

Open williamdes opened 2 months ago

williamdes commented 2 months 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 2 months 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 2 months 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 2 months 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 :)