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 ( 1aaa8a...5b7375 )
by Vermeulen
02:33
created

Errors::defineExceptionHandler()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 19
Ratio 79.17 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 19
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace BFW\Core;
4
5
use \BFW\Application;
6
7
/**
8
 * Class used to have a personnal message/page for errors and exceptions
9
 */
10
class Errors
11
{
12
    /**
13
     * @var \BFW\Application $app : L'instance d'Application
14
     */
15
    protected static $app = null;
16
    
17
    /**
18
     * Constructeur
19
     * 
20
     * @param \BFW\Application $app : L'instance d'Application à utiliser
21
     */
22
    public function __construct(Application $app)
23
    {
24
        self::$app = $app;
25
        
26
        //Find and create the handler for errors
27
        $this->defineErrorHandler();
28
        
29
        //Find and create the handler for exceptions
30
        $this->defineExceptionHandler();
31
    }
32
    
33
    /**
34
     * Find and create the handler for errors
35
     * 
36
     * @return void
37
     */
38 View Code Duplication
    protected function defineErrorHandler()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        //Find the correct class to call (return the child class if extended)
41
        $calledClass = get_called_class();
42
        $errorRender = $calledClass::getErrorRender();
43
        
44
        //If not render to use
45
        if ($errorRender === false) {
46
            return;
47
        }
48
        
49
        //Define the arg for set_error_render
50
        //Only "method" if function, of array this class if method.
51
        $errorHandlerArgs = $errorRender['method'];
52
        if (!empty($errorRender['class'])) {
53
            $errorHandlerArgs = [
54
                $errorRender['class'],
55
                $errorRender['method']
56
            ];
57
        }
58
59
        //add the handler for errors
60
        set_error_handler($errorHandlerArgs);
61
    }
62
    
63
    /**
64
     * Find and create the handler for exceptions
65
     * 
66
     * @return type
67
     */
68 View Code Duplication
    protected function defineExceptionHandler()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        //Find the correct class to call (return the child class if extended)
71
        $calledClass     = get_called_class();
72
        $exceptionRender = $calledClass::getExceptionRender();
73
        
74
        //If not render to use
75
        if ($exceptionRender === false) {
76
            return;
77
        }
78
        
79
        //Define the arg for set_exception_handler
80
        //Only "method" if function, of array this class if method.
81
        $erxceptionHandlerArgs = $exceptionRender['method'];
82
        if (!empty($exceptionRender['class'])) {
83
            $erxceptionHandlerArgs = [
84
                $exceptionRender['class'],
85
                $exceptionRender['method']
86
            ];
87
        }
88
89
        //add the handler for exceptions
90
        set_exception_handler($erxceptionHandlerArgs);
91
    }
92
    
93
    /**
94
     * Get the Application instance
95
     * It's a method to allow override
96
     * 
97
     * @return \BFW\Application
98
     */
99
    protected static function getApp()
100
    {
101
        if (is_null(self::$app)) {
102
            self::$app = Application::getInstance();
103
        }
104
        
105
        return self::$app;
106
    }
107
    
108
    /**
109
     * get the error render from config for cli or default
110
     * 
111
     * @return boolean|array Render infos
112
     *      Boolean : false if no render to use
113
     *      Array   : Infos from config
114
     */
115 View Code Duplication
    public static function getErrorRender()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $calledClass = get_called_class();
118
        $app         = $calledClass::getApp();
119
        $renderFcts  = $app->getConfig('errorRenderFct');
120
        
121
        return self::defineRenderToUse($renderFcts);
122
    }
123
    
124
    /**
125
     * get the exception render from config for cli or default
126
     * 
127
     * @return boolean|array Render infos
128
     *      Boolean : false if no render to use
129
     *      Array   : Infos from config
130
     */
131 View Code Duplication
    public static function getExceptionRender()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $calledClass = get_called_class();
134
        $app         = $calledClass::getApp();
135
        $renderFcts  = $app->getConfig('exceptionRenderFct');
136
        
137
        return self::defineRenderToUse($renderFcts);
138
    }
139
    
140
    /**
141
     * Find the render to use with the config
142
     * If cli render is not define, it's use the default render.
143
     * 
144
     * @param array $renderConfig : Render infos from config
145
     * 
146
     * @return boolean|array : Render to use
147
     *      Boolean : false is no enable or no render define
148
     *      Array : The render used
149
     */
150
    protected static function defineRenderToUse($renderConfig)
151
    {
152
        //Check enabled
153
        if ($renderConfig['active'] === false) {
154
            return false;
155
        }
156
        
157
        //The cli render if cli mode
158
        if (PHP_SAPI === 'cli' && isset($renderConfig['cli'])) {
159
            return $renderConfig['cli'];
160
        }
161
        
162
        //The default render or cli if cli mode and no cli render configured
163
        if (isset($renderConfig['default'])) {
164
            return $renderConfig['default'];
165
        }
166
        
167
        return false;
168
    }
169
    
170
    /**
171
     * The default exception handler included in BFW
172
     * 
173
     * @param \Exception $exception : Exception informations
174
     * 
175
     * @return void
176
     */
177
    public static function exceptionHandler($exception)
178
    {
179
        //Get the current class (childs class if extended)
180
        $calledClass = get_called_class();
181
        $errorRender = $calledClass::getExceptionRender();
182
        
183
        //Call the "callRender" method for this class (or child class)
184
        $calledClass::callRender(
185
            $errorRender,
186
            'Fatal', 
187
            $exception->getMessage(), 
188
            $exception->getFile(), 
189
            $exception->getLine(), 
190
            $exception->getTrace()
191
        );
192
    }
193
    
194
    /**
195
     * The default error handler included in BFW
196
     * 
197
     * @param type $errSeverity : Error severity
198
     * @param type $errMsg : Error message
199
     * @param type $errFile : File where the error is triggered
200
     * @param type $errLine : Line where the error is triggered
201
     * 
202
     * @return void
203
     */
204
    public static function errorHandler(
205
        $errSeverity,
206
        $errMsg,
207
        $errFile,
208
        $errLine
209
    ) {
210
        //Get the current class (childs class if extended)
211
        $calledClass = get_called_class();
212
        $erreurType  = $calledClass::getErrorType($errSeverity);
213
        $errorRender = $calledClass::getErrorRender();
214
        
215
        //Call the "callRender" method for this class (or child class)
216
        $calledClass::callRender(
217
            $errorRender,
218
            $erreurType,
219
            $errMsg,
220
            $errFile,
221
            $errLine,
222
            debug_backtrace()
223
        );
224
    }
225
    
226
    /**
227
     * Call the personnal class-method or function declared on config where
228
     * an exception or an error is triggered.
229
     * 
230
     * @param array $renderInfos : Infos from config
231
     * @param type $erreurType : Error severity
232
     * @param type $errMsg : Error/exception message
233
     * @param type $errFile : File where the error/exception is triggered
234
     * @param type $errLine : Line where the error/exception is triggered
235
     * @param type $backtrace : Error/exception backtrace
236
     * 
237
     * @return void
238
     */
239
    protected static function callRender(
240
        $renderInfos,
241
        $erreurType,
242
        $errMsg,
243
        $errFile,
244
        $errLine,
245
        $backtrace
246
    ) {
247
        $class  = $renderInfos['class'];
248
        $method = $renderInfos['method'];
249
        
250
        //If is a class, call "$class::$method" (compatibility 5.x)
1 ignored issue
show
Unused Code Comprehensibility introduced by
37% 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...
251
        if (!empty($class)) {
252
            $class::$method(
253
                $erreurType,
254
                $errMsg,
255
                $errFile,
256
                $errLine,
257
                $backtrace
258
            );
259
            
260
            return;
261
        }
262
        
263
        //If is not a class, it's a function.
264
        $method(
265
            $erreurType,
266
            $errMsg,
267
            $errFile,
268
            $errLine,
269
            $backtrace
270
        );
271
    }
272
    
273
    /**
274
     * Map array to have a human readable severity.
275
     * 
276
     * @see http://fr2.php.net/manual/fr/function.set-error-handler.php#113567
277
     * 
278
     * @param int $errSeverity : The error severity with PHP constant
279
     * 
280
     * @return string
281
     */
282
    protected static function getErrorType($errSeverity)
283
    {
284
        $map = [
285
            E_ERROR             => 'Fatal',
286
            E_CORE_ERROR        => 'Fatal',
287
            E_USER_ERROR        => 'Fatal',
288
            E_COMPILE_ERROR     => 'Fatal',
289
            E_RECOVERABLE_ERROR => 'Fatal',
290
            E_WARNING           => 'Warning',
291
            E_CORE_WARNING      => 'Warning',
292
            E_USER_WARNING      => 'Warning',
293
            E_COMPILE_WARNING   => 'Warning',
294
            E_PARSE             => 'Parse',
295
            E_NOTICE            => 'Notice',
296
            E_USER_NOTICE       => 'Notice',
297
            E_STRICT            => 'Strict',
298
            E_DEPRECATED        => 'Deprecated',
299
            E_USER_DEPRECATED   => 'Deprecated'
300
        ];
301
302
        //Default value if the error is not found in the map array
303
        $erreurType = 'Unknown';
304
        
305
        //Search in map array
306
        if (isset($map[$errSeverity])) {
307
            $erreurType = $map[$errSeverity];
308
        }
309
        
310
        return $erreurType;
311
    }
312
313
    /**
314
     * The default cli render in BFW
315
     * 
316
     * @param type $erreurType : Error severity
317
     * @param type $errMsg : Error/exception message
318
     * @param type $errFile : File where the error/exception is triggered
319
     * @param type $errLine : Line where the error/exception is triggered
320
     * @param type $backtrace : Error/exception backtrace
321
     * 
322
     * @return void
323
     */
324
    public static function defaultCliErrorRender(
325
        $erreurType,
326
        $errMsg,
327
        $errFile,
328
        $errLine,
329
        $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...
330
    ) {
331
        //Create the cli message
332
        $msgError = $erreurType.' Error : '.$errMsg.
333
            ' in '.$errFile.' at line '.$errLine;
334
        
335
        //Display the message with displayMsg function
336
        \BFW\Cli\displayMsg(
337
            $msgError,
338
            'white',
339
            'red'
340
        );
341
    }
342
343
    /**
344
     * The default render in BFW
345
     * 
346
     * @param type $erreurType : Error severity
347
     * @param type $errMsg : Error/exception message
348
     * @param type $errFile : File where the error/exception is triggered
349
     * @param type $errLine : Line where the error/exception is triggered
350
     * @param type $backtrace : Error/exception backtrace
351
     * 
352
     * @return void
353
     */
354
    public static function defaultErrorRender(
355
        $erreurType,
356
        $errMsg,
357
        $errFile,
358
        $errLine,
359
        $backtrace
360
    ) {
361
        ob_clean();
362
363
        echo '
364
        <!doctype html>
365
        <html lang="fr">
366
            <head>
367
                <title>Une erreur est parmi nous !</title>
368
                <style>
369
                    html {padding:0; margin:0; background-color:#e3e3e3; font-family:sans-serif; font-size: 1em; word-wrap:break-word;}
370
                    div {position:relative; margin:auto; width:950px; border: 1px solid #a6c9e2; top: 30px; margin-bottom:10px;}
371
                    p {padding:0; margin:0;}
372
                    p.title {font-size:1.2em; background-color:#D0DCE9; padding:10px;}
373
                    p.info {padding:5px; margin-top:10px; margin-bottom:10px;}
374
                    fieldset {border:none; background-color: white;}
375
                    pre {width:910px; line-height:1.5;}
376
                </style>
377
            </head>
378
            <body>
379
                <div>
380
                    <p class="title">Niarf, a error is detected !</p>
381
                    <p class="info">'.$erreurType.' Error : <strong>'.$errMsg.'</strong> in '.$errFile.' at line '.$errLine.'</p>
382
                    <fieldset><pre>';
383
                        foreach ($backtrace as $i => $info) {
384
                            echo '#'.$i.'  '.$info['function'];
385
386
                            if (isset($info['args']) && count($info['args']) > 0) {
387
                                echo '(';
388
389
                                foreach ($info['args'] as $iArgs => $args) {
390
                                    if ($iArgs > 0) {
391
                                        echo ', ';
392
                                    }
393
394
                                    if (is_array($args) || is_object($args)) {
395
                                        echo gettype($args);
396
                                    } elseif (is_null($args)) {
397
                                        echo 'null';
398
                                    } else {
399
                                        echo htmlentities($args);
400
                                    }
401
                                }
402
403
                                echo ')';
404
                            }
405
406
                            if (isset($info['file'], $info['line'])) {
407
                                echo ' called at ['.$info['file'].' line '.$info['line'].']';
408
                            }
409
                            echo "\n\n";
410
                        }
411
                    echo '</pre></fieldset>
412
                </div>
413
            <body>
414
        </html>
415
        ';
416
417
        ob_flush();
418
        exit;
419
    }
420
}
421