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

Conversation

andersonamuller
Copy link
Contributor

Q A
Branch? 5.4
Bug fix? no
New feature? ???
Deprecations? no
Tickets
License MIT
Doc PR

Without using static it kills the extensibility of the Notification class as the exception and exceptionAsString properties are private and other methods such as EmailMessage::fromNotification use that to customize the message to send to channels.

Without using static it kills the extensibility of the `Notification` class as the `exception` and `exceptionAsString` properties are private and other methods such as `EmailMessage::fromNotification` use that to customize the message to send to channels.
@carsonbot carsonbot added this to the 5.4 milestone Apr 26, 2022
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?

@carsonbot carsonbot changed the title Fix extensibility of Notification class [Notifier] Fix extensibility of Notification class Apr 26, 2022
@OskarStark OskarStark changed the title [Notifier] Fix extensibility of Notification class [Notifier] Fix extensibility of Notification class Jul 22, 2022
@fabpot
Copy link
Member

fabpot commented Jul 23, 2022

Closing in favor of #47038

@fabpot fabpot closed this Jul 23, 2022
fabpot added a commit that referenced this pull request Jul 23, 2022
This PR was merged into the 6.2 branch.

Discussion
----------

[Notifier] Add Notification::exception()

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #46172 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | -

Commits
-------

2db50f6 [Notifier] Add Notification::exception()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants