Issues (32)

Listener/DeprecationListener.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Ekino New Relic bundle.
7
 *
8
 * (c) Ekino - Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\NewRelicBundle\Listener;
15
16
use Ekino\NewRelicBundle\Exception\DeprecationException;
17
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
18
19
class DeprecationListener
20
{
21
    private $isRegistered = false;
22
    private $interactor;
23
24
    public function __construct(NewRelicInteractorInterface $interactor)
25
    {
26
        $this->interactor = $interactor;
27
    }
28
29
    public function register(): void
30
    {
31
        if ($this->isRegistered) {
32
            return;
33
        }
34
        $this->isRegistered = true;
35
36
        $prevErrorHandler = \set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler) {
0 ignored issues
show
The assignment to $prevErrorHandler is dead and can be removed.
Loading history...
37
            if (E_USER_DEPRECATED === $type) {
38
                $this->interactor->noticeThrowable(new DeprecationException($msg, 0, $type, $file, $line));
39
            }
40
41
            return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
42
        });
43
    }
44
45
    public function unregister(): void
46
    {
47
        if (!$this->isRegistered) {
48
            return;
49
        }
50
        $this->isRegistered = false;
51
        \restore_error_handler();
52
    }
53
}
54