ExceptionListener::setException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Liip/FunctionalTestBundle
7
 *
8
 * (c) Lukas Kahwe Smith <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Liip\FunctionalTestBundle\EventListener;
15
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
18
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20
use Symfony\Component\HttpKernel\Event\RequestEvent;
21
use Symfony\Component\HttpKernel\HttpKernelInterface;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24 1
if (class_exists(ExceptionEvent::class) && !class_exists(GetResponseForExceptionEvent::class)) {
25
    class_alias(ExceptionEvent::class, GetResponseForExceptionEvent::class);
26
}
27
28 1
if (class_exists(RequestEvent::class) && !class_exists(GetResponseEvent::class)) {
29
    class_alias(RequestEvent::class, GetResponseEvent::class);
30
}
31
32
final class ExceptionListener implements EventSubscriberInterface
33
{
34
    /**
35
     * @var \Throwable|null
36
     */
37
    private $lastException;
38
39 1
    public function setException(GetResponseForExceptionEvent $event): void
40
    {
41 1
        $this->lastException = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException();
0 ignored issues
show
Bug introduced by
The method getException() does not exist on Symfony\Component\HttpKernel\Event\ExceptionEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->lastException = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->/** @scrutinizer ignore-call */ getException();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42 1
    }
43
44 1
    public function clearLastException(GetResponseEvent $event): void
45
    {
46 1
        if (HttpKernelInterface::MAIN_REQUEST === $event->getRequestType()) {
47 1
            $this->lastException = null;
48
        }
49 1
    }
50
51
    public function getLastException(): ?\Throwable
52
    {
53
        return $this->lastException;
54
    }
55
56 1
    public static function getSubscribedEvents(): array
57
    {
58
        return [
59 1
            KernelEvents::EXCEPTION => ['setException', 99999],
60
            KernelEvents::REQUEST => ['clearLastException', 99999],
61
        ];
62
    }
63
}
64