route icon indicating copy to clipboard operation
route copied to clipboard

Root exception class (and possibly, its children) not compatible with PHP7+

Open uuf6429 opened this issue 1 year ago • 0 comments

https://github.com/thephpleague/route/blob/b2d82a8b212787f10244e6fa8f94c939560d23bd/src/Http/Exception.php#L30

This breaks on PHP 7+ when a non-\Exception throwable is thrown (e.g. TypeError; see also the type hierarchy).

For best compatibility, the type declaration should be removed. If needs be, ?\Exception / ?\Throwable could be defined via PHPDoc.

Since it's removing a restriction, I don't think this should be considered as a BC break.


On a side note, not sure why it needs to keep a copy of the message: https://github.com/thephpleague/route/blob/b2d82a8b212787f10244e6fa8f94c939560d23bd/src/Http/Exception.php#L20 and https://github.com/thephpleague/route/blob/b2d82a8b212787f10244e6fa8f94c939560d23bd/src/Http/Exception.php#L35


An effective stopgap solution for my use-case:

class InternalServerErrorException extends \League\Route\Http\Exception
{
    protected $status = 500;
    protected $message = 'Internal Server Error';

    /**
     * @throws \ReflectionException
     * @noinspection MagicMethodsValidityInspection
     * @noinspection PhpMissingParentConstructorInspection
     */
    public function __construct(Throwable $previous = null)
    {
        $reflector = new \ReflectionMethod(\Exception::class, '__construct');
        $reflector->invoke($this, $this->message, 0, $previous);
    }
}

uuf6429 avatar Sep 07 '22 10:09 uuf6429