ExceptionListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 29
ccs 9
cts 11
cp 0.8182
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastException() 0 3 1
A clearLastException() 0 4 2
A setException() 0 3 2
A getSubscribedEvents() 0 5 1
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