ErrorHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setHandler() 0 6 1
A register() 0 4 1
A unregister() 0 4 1
A addExtraInfo() 0 7 2
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\DesignPattern\Singleton;
6
use ByJG\RestServer\Whoops\PlainResponseHandler;
7
use Whoops\Handler\Handler;
8
use Whoops\Run;
9
10
class ErrorHandler
11
{
12
13
    use Singleton;
14
15
    /**
16
     *
17
     * @var Run
18
     */
19
    protected $whoops = null;
20
21
    /**
22
     *
23
     * @var Handler
24
     */
25
    protected $handler = null;
26
27
    protected function __construct()
28
    {
29
        $this->whoops = new Run();
30
        $this->setHandler(new PlainResponseHandler());
31
    }
32
33
    /**
34
     * Set the proper Error Handler based on the Output of the page
35
     *
36
     * @param Handler $handler
37
     */
38
    public function setHandler(Handler $handler)
39
    {
40
        $this->whoops->popHandler();
41
        $this->handler = $handler;
42
        $this->whoops->pushHandler($this->handler);
0 ignored issues
show
Documentation introduced by Joao Gilberto Magalhães
$this->handler is of type object<Whoops\Handler\Handler>, but the function expects a callable.

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...
43
    }
44
45
    /**
46
     * Set Whoops as the default error and exception handler used by PHP:
47
     */
48
    public function register()
49
    {
50
        $this->whoops->register();
51
    }
52
53
    /**
54
     * Disable Whoops as the default error and exception handler used by PHP:
55
     */
56
    public function unregister()
57
    {
58
        $this->whoops->unregister();
59
    }
60
61
    /**
62
     * Added extra information for debug purposes on the error handler screen
63
     *
64
     * @param string $name
65
     * @param string $value
66
     */
67
    public function addExtraInfo($name, $value)
68
    {
69
        if (method_exists($this->handler, 'addDataTable')) {
70
            $data = $this->handler->getDataTable();
0 ignored issues
show
Bug introduced by Joao Gilberto Magalhaes
It seems like you code against a specific sub-type and not the parent class Whoops\Handler\Handler as the method getDataTable() does only exist in the following sub-classes of Whoops\Handler\Handler: ByJG\RestServer\Whoops\JsonResponseHandler, ByJG\RestServer\Whoops\PlainResponseHandler. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
71
            $this->handler->addDataTable('Info #' . (count($data) + 1), array($name => $value));
0 ignored issues
show
Bug introduced by Joao Gilberto Magalhaes
It seems like you code against a specific sub-type and not the parent class Whoops\Handler\Handler as the method addDataTable() does only exist in the following sub-classes of Whoops\Handler\Handler: ByJG\RestServer\Whoops\JsonResponseHandler, ByJG\RestServer\Whoops\PlainResponseHandler, Whoops\Handler\PrettyPageHandler. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
72
        }
73
    }
74
}
75