Completed
Push — master ( 9f0cc8...e7ce71 )
by Joao
02:39
created

src/ErrorHandler.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\DesignPattern\Singleton;
6
use ByJG\RestServer\Whoops\JsonResponseHandler;
7
use ByJG\RestServer\Whoops\PlainResponseHandler;
8
use Whoops\Handler\XmlResponseHandler;
9
use Whoops\Handler\Handler;
10
use Whoops\Run;
11
12
class ErrorHandler
13
{
14
15
    use Singleton;
16
17
    /**
18
     *
19
     * @var Run
20
     */
21
    protected $_whoops = null;
22
23
    /**
24
     *
25
     * @var Handler
26
     */
27
    protected $_handler = null;
28
29
    protected function __construct()
30
    {
31
        $this->_whoops = new Run();
32
        $this->setHandler();
33
    }
34
35
    /**
36
     * Set the proper Error Handler based on the Output of the page
37
     *
38
     * @param string $output
39
     */
40
    public function setHandler($output = null)
41
    {
42
        $this->_whoops->popHandler();
43
44
        if ($output == Output::JSON) {
45
            $this->_handler = new JsonResponseHandler();
46
        } else if ($output == Output::XML) {
47
            $this->_handler = new XmlResponseHandler();
48
        } else {
49
            $this->_handler = new PlainResponseHandler();
50
        }
51
52
        $this->_whoops->pushHandler($this->_handler);
53
    }
54
55
    /**
56
     * Set Whoops as the default error and exception handler used by PHP:
57
     */
58
    public function register()
59
    {
60
        $this->_whoops->register();
61
    }
62
63
    /**
64
     * Disable Whoops as the default error and exception handler used by PHP:
65
     */
66
    public function unregister()
67
    {
68
        $this->_whoops->unregister();
69
    }
70
71
    /**
72
     * Added extra information for debug purposes on the error handler screen
73
     *
74
     * @param string $name
75
     * @param string $value
76
     */
77
    public function addExtraInfo($name, $value)
78
    {
79
        if (method_exists($this->_handler, 'addDataTable')) {
80
            $data = $this->_handler->getDataTable();
0 ignored issues
show
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...
81
            $this->_handler->addDataTable('Info #' . (count($data) + 1), array($name => $value));
0 ignored issues
show
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...
82
        }
83
    }
84
}
85