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 ( 21ef47...f17077 )
by Vermeulen
02:50
created

Errors::defineRenderToUse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 8.8571
1
<?php
2
3
namespace BFW\Core;
4
5
use \BFW\Application;
6
7
class Errors
8
{
9
    protected static $app = null;
10
    
11
    public function __construct(Application $app)
12
    {
13
        self::$app   = $app;
14
        
15
        $this->defineErrorHandler();
16
        $this->defineExceptionHandler();
17
    }
18
    
19 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...
20
    {
21
        $calledClass = get_called_class();
22
        $errorRender = $calledClass::getErrorRender();
23
        
24
        if ($errorRender === false) {
25
            return;
26
        }
27
        
28
        $errorHandlerArgs = $errorRender['method'];
29
        if (!empty($errorRender['class'])) {
30
            $errorHandlerArgs = [
31
                $errorRender['class'],
32
                $errorRender['method']
33
            ];
34
        }
35
36
        set_error_handler($errorHandlerArgs);
37
    }
38
    
39 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...
40
    {
41
        $calledClass     = get_called_class();
42
        $exceptionRender = $calledClass::getExceptionRender();
43
        
44
        if ($exceptionRender === false) {
45
            return;
46
        }
47
        
48
        $erxceptionHandlerArgs = $exceptionRender['method'];
49
        if (!empty($exceptionRender['class'])) {
50
            $erxceptionHandlerArgs = [
51
                $exceptionRender['class'],
52
                $exceptionRender['method']
53
            ];
54
        }
55
56
        set_exception_handler($erxceptionHandlerArgs);
57
    }
58
    
59
    protected static function getApp()
60
    {
61
        if(is_null(self::$app)) {
62
            self::$app = Application::getInstance();
63
        }
64
        
65
        return self::$app;
66
    }
67
    
68 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...
69
    {
70
        $calledClass = get_called_class();
71
        $app         = $calledClass::getApp();
72
        $renderFcts  = $app->getConfig('errorRenderFct');
73
        
74
        return self::defineRenderToUse($renderFcts);
75
    }
76
    
77 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...
78
    {
79
        $calledClass = get_called_class();
80
        $app         = $calledClass::getApp();
81
        $renderFcts  = $app->getConfig('exceptionRenderFct');
82
        
83
        return self::defineRenderToUse($renderFcts);
84
    }
85
    
86
    protected static function defineRenderToUse($renderConfig)
87
    {
88
        if($renderConfig['active'] === false) {
89
            return false;
90
        }
91
        
92
        if (PHP_SAPI === 'cli' && isset($renderConfig['cli'])) {
93
            return $renderConfig['cli'];
94
        }
95
        
96
        if(isset($renderConfig['default'])) {
97
            return $renderConfig['default'];
98
        }
99
        
100
        return false;
101
    }
102
    
103
    public static function exceptionHandler($exception)
104
    {
105
        $calledClass = get_called_class();
106
        $errorRender = $calledClass::getExceptionRender();
107
        
108
        $calledClass::callRender(
109
            $errorRender,
110
            'Fatal', 
111
            $exception->getMessage(), 
112
            $exception->getFile(), 
113
            $exception->getLine(), 
114
            $exception->getTrace()
115
        );
116
    }
117
    
118
    public static function errorHandler(
119
        $errSeverity,
120
        $errMsg,
121
        $errFile,
122
        $errLine
123
    ) {
124
        $calledClass = get_called_class();
125
        $erreurType  = $calledClass::getErrorType($errSeverity);
126
        $errorRender = $calledClass::getErrorRender();
127
        
128
        $calledClass::callRender(
129
            $errorRender,
130
            $erreurType,
131
            $errMsg,
132
            $errFile,
133
            $errLine,
134
            debug_backtrace()
135
        );
136
    }
137
    
138
    protected static function callRender(
139
        $renderInfos,
140
        $erreurType,
141
        $errMsg,
142
        $errFile,
143
        $errLine,
144
        $backtrace
145
    ) {
146
        $class  = $renderInfos['class'];
147
        $method = $renderInfos['method'];
148
        
149
        if (!empty($class)) {
150
            $class::$method(
151
                $erreurType,
152
                $errMsg,
153
                $errFile,
154
                $errLine,
155
                $backtrace
156
            );
157
            
158
            return;
159
        }
160
        
161
        $method(
162
            $erreurType,
163
            $errMsg,
164
            $errFile,
165
            $errLine,
166
            $backtrace
167
        );
168
    }
169
    
170
    protected static function getErrorType($errSeverity)
171
    {
172
        //List : http://fr2.php.net/manual/fr/function.set-error-handler.php#113567
173
        $map = [
174
            E_ERROR             => 'Fatal',
175
            E_CORE_ERROR        => 'Fatal',
176
            E_USER_ERROR        => 'Fatal',
177
            E_COMPILE_ERROR     => 'Fatal',
178
            E_RECOVERABLE_ERROR => 'Fatal',
179
            E_WARNING           => 'Warning',
180
            E_CORE_WARNING      => 'Warning',
181
            E_USER_WARNING      => 'Warning',
182
            E_COMPILE_WARNING   => 'Warning',
183
            E_PARSE             => 'Parse',
184
            E_NOTICE            => 'Notice',
185
            E_USER_NOTICE       => 'Notice',
186
            E_STRICT            => 'Strict',
187
            E_DEPRECATED        => 'Deprecated',
188
            E_USER_DEPRECATED   => 'Deprecated'
189
        ];
190
191
        $erreurType = 'Unknown';
192
        if (isset($map[$errSeverity])) {
193
            $erreurType = $map[$errSeverity];
194
        }
195
        
196
        return $erreurType;
197
    }
198
199
    public static function defaultCliErrorRender(
200
        $erreurType,
201
        $errMsg,
202
        $errFile,
203
        $errLine,
204
        $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...
205
    ) {
206
        $msgError = $erreurType.' Error : '.$errMsg.
207
            ' in '.$errFile.' at line '.$errLine;
208
        
209
        \BFW\Cli\displayMsg(
210
            $msgError,
211
            'white',
212
            'red'
213
        );
214
    }
215
216
    public static function defaultErrorRender(
217
        $erreurType,
218
        $errMsg,
219
        $errFile,
220
        $errLine,
221
        $backtrace
222
    ) {
223
        ob_clean();
224
225
        echo '
226
        <!doctype html>
227
        <html lang="fr">
228
            <head>
229
                <title>Une erreur est parmi nous !</title>
230
                <style>
231
                    html {padding:0; margin:0; background-color:#e3e3e3; font-family:sans-serif; font-size: 1em; word-wrap:break-word;}
232
                    div {position:relative; margin:auto; width:950px; border: 1px solid #a6c9e2; top: 30px; margin-bottom:10px;}
233
                    p {padding:0; margin:0;}
234
                    p.title {font-size:1.2em; background-color:#D0DCE9; padding:10px;}
235
                    p.info {padding:5px; margin-top:10px; margin-bottom:10px;}
236
                    fieldset {border:none; background-color: white;}
237
                    pre {width:910px; line-height:1.5;}
238
                </style>
239
            </head>
240
            <body>
241
                <div>
242
                    <p class="title">Niarf, a error is detected !</p>
243
                    <p class="info">'.$erreurType.' Error : <strong>'.$errMsg.'</strong> in '.$errFile.' at line '.$errLine.'</p>
244
                    <fieldset><pre>';
245
                        foreach ($backtrace as $i => $info) {
246
                            echo '#'.$i.'  '.$info['function'];
247
248
                            if (isset($info['args']) && count($info['args']) > 0) {
249
                                echo '(';
250
251
                                foreach ($info['args'] as $iArgs => $args) {
252
                                    if ($iArgs > 0) {
253
                                        echo ', ';
254
                                    }
255
256
                                    if (is_array($args) || is_object($args)) {
257
                                        echo gettype($args);
258
                                    } elseif (is_null($args)) {
259
                                        echo 'null';
260
                                    } else {
261
                                        echo htmlentities($args);
262
                                    }
263
                                }
264
265
                                echo ')';
266
                            }
267
268
                            if (isset($info['file'], $info['line'])) {
269
                                echo ' called at ['.$info['file'].' line '.$info['line'].']';
270
                            }
271
                            echo "\n\n";
272
                        }
273
                    echo '</pre></fieldset>
274
                </div>
275
            <body>
276
        </html>
277
        ';
278
279
        ob_flush();
280
        exit;
281
    }
282
}
283