GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Handler::render()   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
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
 /**
4
  * Handler class.
5
  *
6
  * Provides basic error and exception handling for your application.
7
  * It captures and handles all unhandled exceptions and errors.
8
  *
9
  * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
10
  * @author     Omar El Gabry <[email protected]>
11
  */
12
13
class Handler{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
15
    /**
16
     * Constructor
17
     *
18
     */
19
    private function __construct(){}
20
21
    /**
22
     * Register the error and exception handlers.
23
     * Must be called at the beginning of your application
24
     *
25
     * @return void
26
     */
27
    public static function register(){
28
29
        // turn off all error reporting as well,
30
        // because we will take care of it
31
        error_reporting(0);
32
33
        set_error_handler(__CLASS__ . "::handleError");
34
        set_exception_handler(__CLASS__ .'::handleException');
35
        register_shutdown_function(__CLASS__ ."::handleFatalError" );
36
    }
37
38
     /**
39
      * Handle fatal errors
40
      *
41
      * @return void
42
      */
43
    public static function handleFatalError(){
44
45
        if (PHP_SAPI === 'cli') { return; }
46
        $error = error_get_last();
47
48
        if (!is_array($error)) { return; }
49
50
        $fatals = [E_USER_ERROR, E_ERROR, E_PARSE];
51
52
        if (!in_array($error['type'], $fatals, true)) {
53
            return;
54
        }
55
56
        // self::handleError($error['type'], $error['message'], $error['file'], $error['line'], null);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
        self::handleException(new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']));
0 ignored issues
show
Documentation introduced by
new \ErrorException($err...file'], $error['line']) is of type object<ErrorException>, but the function expects a object<Throwable>.

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...
58
    }
59
60
    /**
61
     * Handle errors
62
     *
63
     * @return void
64
     * @throws ErrorException
65
     */
66
    public static function handleError($errno, $errmsg, $filename, $linenum, $vars){
0 ignored issues
show
Unused Code introduced by
The parameter $vars is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
        throw new ErrorException($errmsg, 0, $errno, $filename, $linenum);
68
    }
69
70
    /**
71
     * Handle & log exceptions
72
     *
73
     * @param  Throwable  $e
74
     * @return void
75
     * @see http://php.net/manual/en/function.set-exception-handler.php
76
     */
77
    public static function handleException($e) {
78
        Logger::Log(get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
79
        self::render($e)->send();
80
    }
81
82
    /**
83
     * display system error page as result of an error or exception
84
     *
85
     * @param  Throwable  $e
86
     * @return Response
87
     */
88
    private static  function render($e){
89
90
        if($e->getCode() === 400){
91
            return (new ErrorsController())->error(400);
92
        }
93
        
94
        return (new ErrorsController())->error(500);
95
    }
96
97
    /**
98
     * Map an error code to error text
99
     *
100
     * @param int $errno
101
     * @return string error text
102
     */
103
    private static function errorType($errno){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
104
105
        // define an assoc array of error string
106
        $errortype = array (
107
            E_ERROR              => 'Error',
108
            E_WARNING            => 'Warning',
109
            E_PARSE              => 'Parsing Error',
110
            E_NOTICE             => 'Notice',
111
            E_CORE_ERROR         => 'Core Error',
112
            E_CORE_WARNING       => 'Core Warning',
113
            E_COMPILE_ERROR      => 'Compile Error',
114
            E_COMPILE_WARNING    => 'Compile Warning',
115
            E_USER_ERROR         => 'User Error',
116
            E_USER_WARNING       => 'User Warning',
117
            E_USER_NOTICE        => 'User Notice',
118
            E_STRICT             => 'Runtime Notice',
119
            E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
120
        );
121
122
        return $errortype[$errno];
123
    }
124
125
}
126