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.
Passed
Push — master ( a1835d...10e84a )
by
unknown
02:48
created

Output::sendError()   B

Complexity

Conditions 10
Paths 42

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 34
nc 42
nop 3
dl 0
loc 57
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Http;
15
16
// ------------------------------------------------------------------------
17
use O2System\Gear\Trace;
18
use O2System\Spl\Exceptions\Abstracts\AbstractException;
19
use O2System\Spl\Exceptions\ErrorException;
20
21
/**
22
 * Class Output
23
 * @package O2System\Framework\Http
24
 */
25
class Output extends \O2System\Kernel\Http\Output
26
{
27
    /**
28
     * Output::shutdownHandler
29
     *
30
     * Kernel defined shutdown handler function.
31
     *
32
     * @return void
33
     * @throws \O2System\Spl\Exceptions\ErrorException
34
     */
35
    public function shutdownHandler()
36
    {
37
        parent::shutdownHandler();
38
39
        // Execute Shutdown Service
40
        if (services()->has('shutdown')) {
41
            shutdown()->execute();
42
        }
43
44
    }
45
46
    // ------------------------------------------------------------------------
47
48
    /**
49
     * Output::getFilePath
50
     *
51
     * @param string $filename
52
     *
53
     * @return string
54
     */
55
    public function getFilePath($filename)
56
    {
57
        if (modules()) {
58
            $filePaths = modules()->getDirs('Views');
0 ignored issues
show
Bug introduced by
The method getDirs() does not exist on O2System\Framework\Conta...s\DataStructures\Module. Did you maybe mean getDir()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            $filePaths = modules()->/** @scrutinizer ignore-call */ getDirs('Views');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        } else {
60
            $filePaths = array_reverse($this->filePaths);
61
        }
62
63
        foreach ($filePaths as $filePath) {
64
            if (is_file($filePath . $filename . '.phtml')) {
65
                return $filePath . $filename . '.phtml';
66
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
67
            } elseif (is_file($filePath . 'errors' . DIRECTORY_SEPARATOR . $filename . '.phtml')) {
68
                return $filePath . 'errors' . DIRECTORY_SEPARATOR . $filename . '.phtml';
69
                break;
70
            }
71
        }
72
    }
73
74
    // ------------------------------------------------------------------------
75
76
    /**
77
     * Output::errorHandler
78
     *
79
     * Kernel defined error handler function.
80
     *
81
     * @param int    $errorSeverity The first parameter, errno, contains the level of the error raised, as an integer.
82
     * @param string $errorMessage  The second parameter, errstr, contains the error message, as a string.
83
     * @param string $errorFile     The third parameter is optional, errfile, which contains the filename that the error
84
     *                              was raised in, as a string.
85
     * @param string $errorLine     The fourth parameter is optional, errline, which contains the line number the error
86
     *                              was raised at, as an integer.
87
     * @param array  $errorContext  The fifth parameter is optional, errcontext, which is an array that points to the
88
     *                              active symbol table at the point the error occurred. In other words, errcontext will
89
     *                              contain an array of every variable that existed in the scope the error was triggered
90
     *                              in. User error handler must not modify error context.
91
     *
92
     * @return bool If the function returns FALSE then the normal error handler continues.
93
     * @throws ErrorException
94
     */
95
    public function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine, $errorContext = [])
96
    {
97
        $isFatalError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errorSeverity) === $errorSeverity);
98
99
        if (strpos($errorFile, 'parser') !== false) {
100
            if (function_exists('parser')) {
101
                if (services()->has('presenter')) {
102
                    $vars = presenter()->getArrayCopy();
103
                    extract($vars);
104
                }
105
106
                $errorFile = str_replace(PATH_ROOT, DIRECTORY_SEPARATOR, parser()->getSourceFilePath());
0 ignored issues
show
Bug introduced by
The constant O2System\Framework\Http\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
107
                $error = new ErrorException($errorMessage, $errorSeverity, $errorFile, $errorLine, $errorContext);
0 ignored issues
show
Bug introduced by
$errorLine of type string is incompatible with the type integer expected by parameter $line of O2System\Spl\Exceptions\...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                $error = new ErrorException($errorMessage, $errorSeverity, $errorFile, /** @scrutinizer ignore-type */ $errorLine, $errorContext);
Loading history...
108
109
                $filePath = $this->getFilePath('error');
110
111
                ob_start();
112
                include $filePath;
113
                $htmlOutput = ob_get_contents();
114
                ob_end_clean();
115
116
                echo $htmlOutput;
117
118
                return true;
119
            }
120
        }
121
122
        // When the error is fatal the Kernel will throw it as an exception.
123
        if ($isFatalError) {
124
            throw new ErrorException($errorMessage, $errorSeverity, $errorLine, $errorLine, $errorContext);
125
        }
126
127
        // Should we ignore the error? We'll get the current error_reporting
128
        // level and add its bits with the severity bits to find out.
129
        if (($errorSeverity & error_reporting()) !== $errorSeverity) {
130
            return false;
131
        }
132
133
        $error = new ErrorException($errorMessage, $errorSeverity, $errorFile, $errorLine, $errorContext);
134
135
        // Logged the error
136
        logger()->error(
137
            implode(
138
                ' ',
139
                [
140
                    '[ ' . $error->getStringSeverity() . ' ] ',
141
                    $error->getMessage(),
142
                    $error->getFile() . ':' . $error->getLine(),
143
                ]
144
            )
145
        );
146
147
        // Should we display the error?
148
        if (str_ireplace(['off', 'none', 'no', 'false', 'null'], 0, ini_get('display_errors')) == 1) {
149
            if (is_ajax()) {
150
                $this->setContentType('application/json');
151
                $this->statusCode = 500;
152
                $this->reasonPhrase = 'Internal Server Error';
153
154
                $this->send(implode(
155
                    ' ',
156
                    [
157
                        '[ ' . $error->getStringSeverity() . ' ] ',
158
                        $error->getMessage(),
159
                        $error->getFile() . ':' . $error->getLine(),
160
                    ]
161
                ));
162
                exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
163
            }
164
165
            if (services()->has('presenter')) {
166
                if (presenter()->theme) {
167
                    presenter()->theme->load();
168
                }
169
170
                $vars = presenter()->getArrayCopy();
171
                extract($vars);
172
            }
173
174
            $filePath = $this->getFilePath('error');
175
176
            ob_start();
177
            include $filePath;
178
            $htmlOutput = ob_get_contents();
179
            ob_end_clean();
180
181
            if (services()->has('presenter')) {
182
                $htmlOutput = presenter()->assets->parseSourceCode($htmlOutput);
183
            }
184
185
            echo $htmlOutput;
186
            exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
187
        }
188
    }
189
190
    // ------------------------------------------------------------------------
191
192
    /**
193
     * Output::exceptionHandler
194
     *
195
     * Kernel defined exception handler function.
196
     *
197
     * @param \Exception|\Error|\O2System\Spl\Exceptions\Abstracts\AbstractException $exception Throwable exception.
198
     *
199
     * @return void
200
     */
201
    public function exceptionHandler($exception)
202
    {
203
        if (is_ajax()) {
204
            $this->statusCode = 500;
205
            $this->reasonPhrase = 'Internal Server Error';
206
207
            $this->send(implode(
208
                ' ',
209
                [
210
                    ($exception->getCode() != 0 ? '[ ' . $exception->getCode() . ']' : ''),
211
                    $exception->getMessage(),
212
                    $exception->getFile() . ':' . $exception->getLine(),
213
                ]
214
            ));
215
        } elseif ($exception instanceof AbstractException) {
216
217
            if (presenter()->theme) {
218
                presenter()->theme->load();
219
            }
220
221
            $vars = presenter()->getArrayCopy();
222
            extract($vars);
223
224
            ob_start();
225
            include $this->getFilePath('exception');
226
            $htmlOutput = ob_get_contents();
227
            ob_end_clean();
228
229
            if (services()->has('presenter')) {
230
                $htmlOutput = presenter()->assets->parseSourceCode($htmlOutput);
231
            }
232
233
            echo $htmlOutput;
234
            exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
235
        } elseif ($exception instanceof \Exception || $exception instanceof \Error) {
0 ignored issues
show
introduced by
$exception is always a sub-type of Error.
Loading history...
236
237
            $exceptionClassName = get_class_name($exception);
238
            $header = language()->getLine('E_HEADER_' . $exceptionClassName);
239
            $description = language()->getLine('E_DESCRIPTION_' . $exceptionClassName);
240
            $trace = new Trace($exception->getTrace());
241
242
            if (services()->has('presenter')) {
243
                if (presenter()->theme) {
244
                    presenter()->theme->load();
245
                }
246
247
                $vars = presenter()->getArrayCopy();
248
                extract($vars);
249
            }
250
251
            ob_start();
252
            include $this->getFilePath('exception-spl');
253
            $htmlOutput = ob_get_contents();
254
            ob_end_clean();
255
256
            if (services()->has('presenter')) {
257
                $htmlOutput = presenter()->assets->parseSourceCode($htmlOutput);
258
            }
259
260
            echo $htmlOutput;
261
            exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
262
        }
263
    }
264
265
    // ------------------------------------------------------------------------
266
267
    /**
268
     * Output::sendError
269
     *
270
     * @param int               $code
271
     * @param null|array|string $vars
272
     * @param array             $headers
273
     */
274
    public function sendError($code = 204, $vars = null, $headers = [])
275
    {
276
        $languageKey = $code . '_' . error_code_string($code);
277
278
        $error = [
279
            'code'    => $code,
280
            'title'   => language()->getLine($languageKey . '_TITLE'),
281
            'message' => language()->getLine($languageKey . '_MESSAGE'),
282
        ];
283
284
        $this->statusCode = $code;
285
        $this->reasonPhrase = $error[ 'title' ];
286
287
        if (is_string($vars)) {
288
            $vars = ['message' => $vars];
289
        } elseif (is_array($vars) and empty($vars[ 'message' ])) {
290
            $vars[ 'message' ] = $error[ 'message' ];
291
        }
292
293
        if (isset($vars[ 'message' ])) {
294
            $error[ 'message' ] = $vars[ 'message' ];
295
        }
296
297
        if (is_ajax() or $this->mimeType !== 'text/html') {
298
            $this->statusCode = $code;
299
            $this->reasonPhrase = $error[ 'title' ];
300
            $this->send($vars);
301
302
            exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
303
        }
304
305
        $this->sendHeaders($headers);
306
307
        if (services()->has('presenter')) {
308
            presenter()->initialize();
309
310
            if (presenter()->theme) {
311
                presenter()->theme->load();
312
            }
313
314
            $vars = presenter()->getArrayCopy();
315
            extract($vars);
316
        }
317
318
        extract($error);
319
320
        ob_start();
321
        include $this->getFilePath('error-code');
322
        $htmlOutput = ob_get_contents();
323
        ob_end_clean();
324
325
        if (services()->has('presenter')) {
326
            $htmlOutput = presenter()->assets->parseSourceCode($htmlOutput);
327
        }
328
329
        echo $htmlOutput;
330
        exit(EXIT_ERROR);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
331
    }
332
}