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 ( 6c903e...d263d3 )
by Vermeulen
02:03
created

Errors::defineExceptionHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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