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.
Completed
Push — 3.0 ( 7c2f4b...3ca115 )
by Vermeulen
02:17
created

Errors::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace BFW\Core;
4
5
/**
6
 * Class used to have a personnal message/page for errors and exceptions
7
 */
8
class Errors
9
{
10
    /**
11
     * @var \BFW\Application $app : L'instance d'Application
12
     */
13
    protected static $app = null;
14
    
15
    /**
16
     * Constructeur
17
     */
18
    public function __construct()
19
    {
20
        //Find and create the handler for errors
21
        $this->defineErrorHandler();
22
        
23
        //Find and create the handler for exceptions
24
        $this->defineExceptionHandler();
25
    }
26
    
27
    /**
28
     * Find and create the handler for errors
29
     * 
30
     * @return void
31
     */
32
    protected function defineErrorHandler()
33
    {
34
        //Find the correct class to call (return the child class if extended)
35
        $calledClass = get_called_class();
36
        $errorRender = $calledClass::getErrorRender();
37
        
38
        //If not render to use
39
        if ($errorRender === false) {
40
            return;
41
        }
42
43
        //add the handler for errors
44
        set_error_handler([$this, 'errorHandler']);
45
    }
46
    
47
    /**
48
     * Find and create the handler for exceptions
49
     * 
50
     * @return type
51
     */
52
    protected function defineExceptionHandler()
53
    {
54
        //Find the correct class to call (return the child class if extended)
55
        $calledClass     = get_called_class();
56
        $exceptionRender = $calledClass::getExceptionRender();
57
        
58
        //If not render to use
59
        if ($exceptionRender === false) {
60
            return;
61
        }
62
        
63
        //add the handler for exceptions
64
        set_exception_handler([$this, 'exceptionHandler']);
65
    }
66
    
67
    /**
68
     * get the error render from config for cli or default
69
     * 
70
     * @return boolean|array Render infos
71
     *      Boolean : false if no render to use
72
     *      Array   : Infos from config
73
     */
74
    public static function getErrorRender()
75
    {
76
        $app        = \BFW\Application::getInstance();
77
        $renderFcts = $app->getConfig('errorRenderFct');
78
        
79
        return self::defineRenderToUse($renderFcts);
80
    }
81
    
82
    /**
83
     * get the exception render from config for cli or default
84
     * 
85
     * @return boolean|array Render infos
86
     *      Boolean : false if no render to use
87
     *      Array   : Infos from config
88
     */
89
    public static function getExceptionRender()
90
    {
91
        $app        = \BFW\Application::getInstance();
92
        $renderFcts = $app->getConfig('exceptionRenderFct');
93
        
94
        return self::defineRenderToUse($renderFcts);
95
    }
96
    
97
    /**
98
     * Find the render to use with the config
99
     * If cli render is not define, it's use the default render.
100
     * 
101
     * @param array $renderConfig : Render infos from config
102
     * 
103
     * @return boolean|array : Render to use
104
     *      Boolean : false is no enable or no render define
105
     *      Array : The render used
106
     */
107
    protected static function defineRenderToUse($renderConfig)
108
    {
109
        //Check enabled
110
        if ($renderConfig['active'] === false) {
111
            return false;
112
        }
113
        
114
        //The cli render if cli mode
115
        if (PHP_SAPI === 'cli' && isset($renderConfig['cli'])) {
116
            return $renderConfig['cli'];
117
        }
118
        
119
        //The default render or cli if cli mode and no cli render configured
120
        if (isset($renderConfig['default'])) {
121
            return $renderConfig['default'];
122
        }
123
        
124
        return false;
125
    }
126
    
127
    /**
128
     * The default exception handler included in BFW
129
     * 
130
     * @param \Exception $exception : Exception informations
131
     * 
132
     * @return void
133
     */
134
    public static function exceptionHandler($exception)
135
    {
136
        //Get the current class (childs class if extended)
137
        $calledClass = get_called_class();
138
        $errorRender = $calledClass::getExceptionRender();
139
        
140
        //Call the "callRender" method for this class (or child class)
141
        $calledClass::callRender(
142
            $errorRender,
143
            'Exception Uncaught', 
144
            $exception->getMessage(), 
145
            $exception->getFile(), 
146
            $exception->getLine(), 
147
            $exception->getTrace()
148
        );
149
    }
150
    
151
    /**
152
     * The default error handler included in BFW
153
     * 
154
     * @param type $errSeverity : Error severity
155
     * @param type $errMsg : Error message
156
     * @param type $errFile : File where the error is triggered
157
     * @param type $errLine : Line where the error is triggered
158
     * 
159
     * @return void
160
     */
161
    public static function errorHandler(
162
        $errSeverity,
163
        $errMsg,
164
        $errFile,
165
        $errLine
166
    ) {
167
        //Get the current class (childs class if extended)
168
        $calledClass = get_called_class();
169
        $erreurType  = $calledClass::getErrorType($errSeverity);
170
        $errorRender = $calledClass::getErrorRender();
171
        
172
        //Call the "callRender" method for this class (or child class)
173
        $calledClass::callRender(
174
            $errorRender,
175
            $erreurType,
176
            $errMsg,
177
            $errFile,
178
            $errLine,
179
            debug_backtrace()
180
        );
181
    }
182
    
183
    /**
184
     * Call the personnal class-method or function declared on config where
185
     * an exception or an error is triggered.
186
     * 
187
     * @param array $renderInfos : Infos from config
188
     * @param type $erreurType : Error severity
189
     * @param type $errMsg : Error/exception message
190
     * @param type $errFile : File where the error/exception is triggered
191
     * @param type $errLine : Line where the error/exception is triggered
192
     * @param type $backtrace : Error/exception backtrace
193
     * 
194
     * @return void
195
     */
196
    protected static function callRender(
197
        $renderInfos,
198
        $erreurType,
199
        $errMsg,
200
        $errFile,
201
        $errLine,
202
        $backtrace
203
    ) {
204
        $calledClass = get_called_class();
205
        $calledClass::saveIntoPhpLog($erreurType, $errMsg, $errFile, $errLine);
206
        
207
        $class  = $renderInfos['class'];
208
        $method = $renderInfos['method'];
209
        
210
        //If is a class, call "$class::$method" (compatibility 5.x)
211
        if (!empty($class)) {
212
            $class::$method(
213
                $erreurType,
214
                $errMsg,
215
                $errFile,
216
                $errLine,
217
                $backtrace
218
            );
219
            
220
            return;
221
        }
222
        
223
        //If is not a class, it's a function.
224
        $method(
225
            $erreurType,
226
            $errMsg,
227
            $errFile,
228
            $errLine,
229
            $backtrace
230
        );
231
    }
232
    
233
    protected static function saveIntoPhpLog(
234
        $errType,
235
        $errMsg,
236
        $errFile,
237
        $errLine
238
    ) {
239
        error_log(
240
            'Error detected : '
241
            .$errType.' '.$errMsg
242
            .' at '.$errFile.':'.$errLine
243
        );
244
    }
245
    
246
    /**
247
     * Map array to have a human readable severity.
248
     * 
249
     * @see http://fr2.php.net/manual/fr/function.set-error-handler.php#113567
250
     * 
251
     * @param int $errSeverity : The error severity with PHP constant
252
     * 
253
     * @return string
254
     */
255
    protected static function getErrorType($errSeverity)
256
    {
257
        $map = [
258
            E_ERROR             => 'Fatal',
259
            E_CORE_ERROR        => 'Fatal',
260
            E_USER_ERROR        => 'Fatal',
261
            E_COMPILE_ERROR     => 'Fatal',
262
            E_RECOVERABLE_ERROR => 'Fatal',
263
            E_WARNING           => 'Warning',
264
            E_CORE_WARNING      => 'Warning',
265
            E_USER_WARNING      => 'Warning',
266
            E_COMPILE_WARNING   => 'Warning',
267
            E_PARSE             => 'Parse',
268
            E_NOTICE            => 'Notice',
269
            E_USER_NOTICE       => 'Notice',
270
            E_STRICT            => 'Strict',
271
            E_DEPRECATED        => 'Deprecated',
272
            E_USER_DEPRECATED   => 'Deprecated'
273
        ];
274
275
        //Default value if the error is not found in the map array
276
        $erreurType = 'Unknown';
277
        
278
        //Search in map array
279
        if (isset($map[$errSeverity])) {
280
            $erreurType = $map[$errSeverity];
281
        }
282
        
283
        return $erreurType;
284
    }
285
286
    /**
287
     * The default cli render in BFW
288
     * 
289
     * @param type $erreurType : Error severity
290
     * @param type $errMsg : Error/exception message
291
     * @param type $errFile : File where the error/exception is triggered
292
     * @param type $errLine : Line where the error/exception is triggered
293
     * @param type $backtrace : Error/exception backtrace
294
     * 
295
     * @return void
296
     */
297
    public static function defaultCliErrorRender(
298
        $erreurType,
299
        $errMsg,
300
        $errFile,
301
        $errLine,
302
        $backtrace
0 ignored issues
show
Unused Code introduced by
The parameter $backtrace 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...
303
    ) {
304
        //Create the cli message
305
        $msgError = $erreurType.' Error : '.$errMsg.
306
            ' in '.$errFile.' at line '.$errLine;
307
        
308
        //Display the message with displayMsg function
309
        \BFW\Helpers\Cli::displayMsg(
310
            $msgError,
311
            'white',
312
            'red'
313
        );
314
    }
315
316
    /**
317
     * The default render in BFW
318
     * 
319
     * @param type $erreurType : Error severity
320
     * @param type $errMsg : Error/exception message
321
     * @param type $errFile : File where the error/exception is triggered
322
     * @param type $errLine : Line where the error/exception is triggered
323
     * @param type $backtrace : Error/exception backtrace
324
     * 
325
     * @return void
326
     */
327
    public static function defaultErrorRender(
328
        $erreurType,
329
        $errMsg,
330
        $errFile,
331
        $errLine,
332
        $backtrace
333
    ) {
334
        ob_clean();
335
336
        echo '
337
        <!doctype html>
338
        <html lang="fr">
339
            <head>
340
                <title>A error is detected !</title>
341
                <style>
342
                    html {padding:0; margin:0; background-color:#e3e3e3; font-family:sans-serif; font-size: 1em; word-wrap:break-word;}
343
                    div {position:relative; margin:auto; width:950px; border: 1px solid #a6c9e2; top: 30px; margin-bottom:10px;}
344
                    p {padding:0; margin:0;}
345
                    p.title {font-size:1.2em; background-color:#D0DCE9; padding:10px;}
346
                    p.info {padding:5px; margin-top:10px; margin-bottom:10px;}
347
                    fieldset {border:none; background-color: white;}
348
                    pre {width:910px; line-height:1.5;}
349
                </style>
350
            </head>
351
            <body>
352
                <div>
353
                    <p class="title">Niarf, a error is detected !</p>
354
                    <p class="info">'.$erreurType.' Error : <strong>'.$errMsg.'</strong> in '.$errFile.' at line '.$errLine.'</p>
355
                    <fieldset><pre>';
356
                        foreach ($backtrace as $i => $info) {
357
                            echo '#'.$i.'  '.$info['function'];
358
359
                            if (isset($info['args']) && count($info['args']) > 0) {
360
                                echo '(';
361
362
                                foreach ($info['args'] as $iArgs => $args) {
363
                                    if ($iArgs > 0) {
364
                                        echo ', ';
365
                                    }
366
367
                                    if (is_array($args) || is_object($args)) {
368
                                        echo gettype($args);
369
                                    } elseif (is_null($args)) {
370
                                        echo 'null';
371
                                    } else {
372
                                        echo htmlentities($args);
373
                                    }
374
                                }
375
376
                                echo ')';
377
                            }
378
379
                            if (isset($info['file'], $info['line'])) {
380
                                echo ' called at ['.$info['file'].' line '.$info['line'].']';
381
                            }
382
                            echo "\n\n";
383
                        }
384
                    echo '</pre></fieldset>
385
                </div>
386
            <body>
387
        </html>
388
        ';
389
390
        ob_flush();
391
        exit;
392
    }
393
}
394