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.

Output   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 104
c 2
b 0
f 0
dl 0
loc 247
rs 9.92
wmc 31

5 Methods

Rating   Name   Duplication   Size   Complexity  
A shutdownHandler() 0 7 2
B sendError() 0 57 10
A getFilePath() 0 15 5
A __construct() 0 6 2
C errorHandler() 0 94 12
1
<?php
2
/**
3
 * This file is part of the O2System Reactor 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\Reactor\Http;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Gear\Trace;
0 ignored issues
show
Bug introduced by
The type O2System\Gear\Trace was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use O2System\Spl\Exceptions\Abstracts\AbstractException;
20
use O2System\Spl\Exceptions\ErrorException;
21
22
/**
23
 * Class Output
24
 * @package O2System\Reactor\Http
25
 */
26
class Output extends \O2System\Kernel\Http\Output
27
{
28
    /**
29
     * Output::__construct
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
35
        if(services()->has('csrfProtection')) {
36
            $this->addHeader('X-CSRF-TOKEN', services()->get('csrfProtection')->getToken());
37
        }
38
    }
39
40
    // ------------------------------------------------------------------------
41
42
    /**
43
     * Output::shutdownHandler
44
     *
45
     * Kernel defined shutdown handler function.
46
     *
47
     * @return void
48
     * @throws \O2System\Spl\Exceptions\ErrorException
49
     */
50
    public function shutdownHandler()
51
    {
52
        parent::shutdownHandler();
53
54
        // Execute Shutdown Service
55
        if (services()->has('shutdown')) {
56
            shutdown()->execute();
57
        }
58
59
    }
60
61
    // ------------------------------------------------------------------------
62
63
    /**
64
     * Output::getFilePath
65
     *
66
     * @param string $filename
67
     *
68
     * @return string
69
     */
70
    public function getFilePath($filename)
71
    {
72
        if (modules()) {
0 ignored issues
show
Bug introduced by
The function modules was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
        if (/** @scrutinizer ignore-call */ modules()) {
Loading history...
73
            $filePaths = modules()->getDirs('Views');
74
        } else {
75
            $filePaths = array_reverse($this->filePaths);
76
        }
77
78
        foreach ($filePaths as $filePath) {
79
            if (is_file($filePath . $filename . '.phtml')) {
80
                return $filePath . $filename . '.phtml';
81
                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...
82
            } elseif (is_file($filePath . 'errors' . DIRECTORY_SEPARATOR . $filename . '.phtml')) {
83
                return $filePath . 'errors' . DIRECTORY_SEPARATOR . $filename . '.phtml';
84
                break;
85
            }
86
        }
87
    }
88
89
    // ------------------------------------------------------------------------
90
91
    /**
92
     * Output::errorHandler
93
     *
94
     * Kernel defined error handler function.
95
     *
96
     * @param int    $errorSeverity The first parameter, errno, contains the level of the error raised, as an integer.
97
     * @param string $errorMessage  The second parameter, errstr, contains the error message, as a string.
98
     * @param string $errorFile     The third parameter is optional, errfile, which contains the filename that the error
99
     *                              was raised in, as a string.
100
     * @param string $errorLine     The fourth parameter is optional, errline, which contains the line number the error
101
     *                              was raised at, as an integer.
102
     * @param array  $errorContext  The fifth parameter is optional, errcontext, which is an array that points to the
103
     *                              active symbol table at the point the error occurred. In other words, errcontext will
104
     *                              contain an array of every variable that existed in the scope the error was triggered
105
     *                              in. User error handler must not modify error context.
106
     *
107
     * @return bool If the function returns FALSE then the normal error handler continues.
108
     * @throws ErrorException
109
     */
110
    public function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine, $errorContext = [])
111
    {
112
        $isFatalError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errorSeverity) === $errorSeverity);
113
114
        if (strpos($errorFile, 'parser') !== false) {
115
            if (function_exists('parser')) {
116
                if (services()->has('presenter')) {
117
                    $vars = presenter()->getArrayCopy();
0 ignored issues
show
Bug introduced by
The function presenter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

117
                    $vars = /** @scrutinizer ignore-call */ presenter()->getArrayCopy();
Loading history...
118
                    extract($vars);
119
                }
120
121
                $errorFile = str_replace(PATH_ROOT, DIRECTORY_SEPARATOR, parser()->getSourceFilePath());
0 ignored issues
show
Bug introduced by
The constant O2System\Reactor\Http\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
122
                $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

122
                $error = new ErrorException($errorMessage, $errorSeverity, $errorFile, /** @scrutinizer ignore-type */ $errorLine, $errorContext);
Loading history...
123
124
                $filePath = $this->getFilePath('error');
125
126
                ob_start();
127
                include $filePath;
128
                $htmlOutput = ob_get_contents();
129
                ob_end_clean();
130
131
                echo $htmlOutput;
132
133
                return true;
134
            }
135
        }
136
137
        // When the error is fatal the Kernel will throw it as an exception.
138
        if ($isFatalError) {
139
            throw new ErrorException($errorMessage, $errorSeverity, $errorLine, $errorLine, $errorContext);
140
        }
141
142
        // Should we ignore the error? We'll get the current error_reporting
143
        // level and add its bits with the severity bits to find out.
144
        if (($errorSeverity & error_reporting()) !== $errorSeverity) {
145
            return false;
146
        }
147
148
        $error = new ErrorException($errorMessage, $errorSeverity, $errorFile, $errorLine, $errorContext);
149
150
        // Logged the error
151
        if(services()->has('logger')) {
152
            logger()->error(
153
                implode(
154
                    ' ',
155
                    [
156
                        '[ ' . $error->getStringSeverity() . ' ] ',
157
                        $error->getMessage(),
158
                        $error->getFile() . ':' . $error->getLine(),
159
                    ]
160
                )
161
            );
162
        }
163
164
        // Should we display the error?
165
        if (str_ireplace(['off', 'none', 'no', 'false', 'null'], 0, ini_get('display_errors')) == 1) {
166
            if (is_ajax()) {
167
                $this->setContentType('application/json');
168
                $this->statusCode = 500;
169
                $this->reasonPhrase = 'Internal Server Error';
170
171
                $this->send(implode(
172
                    ' ',
173
                    [
174
                        '[ ' . $error->getStringSeverity() . ' ] ',
175
                        $error->getMessage(),
176
                        $error->getFile() . ':' . $error->getLine(),
177
                    ]
178
                ));
179
                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...
180
            }
181
182
            if (services()->has('presenter')) {
183
                if (presenter()->theme) {
184
                    presenter()->theme->load();
185
                }
186
187
                $vars = presenter()->getArrayCopy();
188
                extract($vars);
189
            }
190
191
            $filePath = $this->getFilePath('error');
192
193
            ob_start();
194
            include $filePath;
195
            $htmlOutput = ob_get_contents();
196
            ob_end_clean();
197
198
            if (services()->has('presenter')) {
199
                $htmlOutput = presenter()->assets->parseSourceCode($htmlOutput);
200
            }
201
202
            echo $htmlOutput;
203
            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...
204
        }
205
    }
206
207
    // ------------------------------------------------------------------------
208
209
    /**
210
     * Output::sendError
211
     *
212
     * @param int               $code
213
     * @param null|array|string $vars
214
     * @param array             $headers
215
     */
216
    public function sendError($code = 204, $vars = null, $headers = [])
217
    {
218
        $languageKey = $code . '_' . error_code_string($code);
219
220
        $error = [
221
            'code'    => $code,
222
            'title'   => language()->getLine($languageKey . '_TITLE'),
223
            'message' => language()->getLine($languageKey . '_MESSAGE'),
224
        ];
225
226
        $this->statusCode = $code;
227
        $this->reasonPhrase = $error[ 'title' ];
228
229
        if (is_string($vars)) {
230
            $vars = ['message' => $vars];
231
        } elseif (is_array($vars) and empty($vars[ 'message' ])) {
232
            $vars[ 'message' ] = $error[ 'message' ];
233
        }
234
235
        if (isset($vars[ 'message' ])) {
236
            $error[ 'message' ] = $vars[ 'message' ];
237
        }
238
239
        if (is_ajax() or $this->mimeType !== 'text/html') {
240
            $this->statusCode = $code;
241
            $this->reasonPhrase = $error[ 'title' ];
242
            $this->send($vars);
243
244
            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...
245
        }
246
247
        $this->sendHeaders($headers);
248
249
        if (services()->has('presenter')) {
250
            presenter()->initialize();
0 ignored issues
show
Bug introduced by
The function presenter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

250
            /** @scrutinizer ignore-call */ 
251
            presenter()->initialize();
Loading history...
251
252
            if (presenter()->theme) {
253
                presenter()->theme->load();
254
            }
255
256
            $vars = presenter()->getArrayCopy();
257
            extract($vars);
258
        }
259
260
        extract($error);
261
262
        ob_start();
263
        include $this->getFilePath('error-code');
264
        $htmlOutput = ob_get_contents();
265
        ob_end_clean();
266
267
        if (services()->has('presenter')) {
268
            $htmlOutput = presenter()->assets->parseSourceCode($htmlOutput);
269
        }
270
271
        echo $htmlOutput;
272
        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...
273
    }
274
}