Passed
Push — master ( 583ddf...23269c )
by Alexis
03:03 queued 12s
created

ExceptionListener::clearLastException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
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::MASTER_REQUEST === $event->getRequestType()) {
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

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

46
        if (/** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
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