Skip to content

[Notifier] Fix extensibility of Notification class #46172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ public function __construct(string $subject = '', array $channels = [])
$this->channels = $channels;
}

/**
* @return static
*/
public static function fromThrowable(\Throwable $exception, array $channels = []): self
{
$parts = explode('\\', \get_class($exception));

$notification = new self(sprintf('%s: %s', array_pop($parts), $exception->getMessage()), $channels);
$notification = new static(sprintf('%s: %s', array_pop($parts), $exception->getMessage()), $channels);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change assumes that the child class has the same constructor signature as its parent which is not necessarily the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I missed that. Should we then just add a method exception? Like this:

    /**
     * @return $this
     */
    public function exception(\Throwable $exception): self
    {
        $parts = explode('\\', \get_class($exception));

        $this->subject = sprintf('%s: %s', array_pop($parts), $exception->getMessage());
        if (class_exists(FlattenException::class)) {
            $this->exception = $exception instanceof FlattenException ? $exception : FlattenException::createFromThrowable($exception);
        }
        $this->exceptionAsString = $this->computeExceptionAsString($exception);

        return $this;
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea. But we would need to target 6.2 with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andersonamuller Your suggestion seems good to me. Can you close this PR and open another one on 6.2 with the new exception() method?

if (class_exists(FlattenException::class)) {
$notification->exception = $exception instanceof FlattenException ? $exception : FlattenException::createFromThrowable($exception);
}
Expand Down