ErrorHandler::getStrategy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Team: jungle
5
 * User: Roma Baranenko
6
 * Contacts: <[email protected]>
7
 * Date: 18.01.18
8
 * Time: 18:46
9
 */
10
11
namespace Symfony\Component\Debug\Extension;
12
13
14
use Exception;
15
use Symfony\Component\Debug\BufferingLogger;
16
use Symfony\Component\Debug\ErrorHandler as BaseErrorHandler;
17
use Symfony\Component\Debug\Extension\Strategies\BaseStrategy;
18
use Symfony\Component\Debug\Extension\Strategies\SerialStrategy;
19
use Symfony\Component\Debug\Extension\Api\ErrorHandlerInterface;
20
use Symfony\Component\Debug\Extension\Exceptions\ErrorHandlerException;
21
22
/**
23
 * Class ErrorHandler
24
 * @package Symfony\Component\Debug\Extension
25
 */
26
class ErrorHandler extends BaseErrorHandler {
27
28
    /**
29
     * @var BaseStrategy $strategy
30
     */
31
    private $strategy;
32
33
34
    /**
35
     * @return BaseStrategy
36
     * @throws ErrorHandlerException
37
     */
38
    public function getStrategy() {
39
40
        if (null === $this->strategy) {
41
            throw new ErrorHandlerException('Error handler strategy undefined.');
42
        }
43
44
        return $this->strategy;
45
    }
46
47
    /**
48
     * @param BaseStrategy $strategy
49
     */
50
    public function setStrategy(BaseStrategy $strategy) {
51
        $this->strategy = $strategy;
52
    }
53
54
    /**
55
     * @param ErrorHandlerInterface $errorHandler
56
     * @param string $type
57
     * @return int
58
     * @throws ErrorHandlerException
59
     */
60
    public function registerHandler(ErrorHandlerInterface $errorHandler, $type = '') {
61
        return $this->getStrategy()->registerHandler($errorHandler, $type);
62
    }
63
64
    /**
65
     * @param Exception $exception
66
     * @return bool
67
     */
68
    public function handle(Exception $exception) {
69
        return $this->strategy->handle($exception);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function handleException($exception, array $error = null) {
76
77
        $this->handle($exception);
78
79
        parent::handleException($exception, $error);
80
    }
81
82
    /**
83
     * ErrorHandler constructor.
84
     * @param BaseStrategy|null $strategy
85
     * {@inheritdoc}
86
     */
87
    public function __construct(BaseStrategy $strategy = null, BufferingLogger $bootstrappingLogger = null) {
88
        parent::__construct($bootstrappingLogger);
89
90
        (null !== $strategy) && $this->setStrategy($strategy);
91
    }
92
93
94
    /**
95
     * @param ErrorHandler|null $handler
96
     * @param bool $replace
97
     * @return ErrorHandler
98
     */
99
    public static function registerExtension(ErrorHandler $handler = null, $replace = true) {
100
101
        (null === $handler) && $handler = new self(new SerialStrategy());
102
103
        /** @var ErrorHandler $handler */
104
        $handler = BaseErrorHandler::register($handler, $replace);
0 ignored issues
show
Documentation introduced by
$handler is of type object<Symfony\Component...Extension\ErrorHandler>, but the function expects a null|object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
106
        return $handler;
107
    }
108
}
109