YellowScreenOfDeath::renderException()   F
last analyzed

Complexity

Conditions 17
Paths 768

Size

Total Lines 154
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 67
c 0
b 0
f 0
nc 768
nop 6
dl 0
loc 154
rs 2.3769

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
/**
4
 * Koch Framework
5
 * Jens-André Koch © 2005 - onwards.
6
 *
7
 * This file is part of "Koch Framework".
8
 *
9
 * License: GNU/GPL v2 or any later version, see LICENSE file.
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace Koch\Exception\Renderer;
26
27
use Koch\Exception\Errorhandler;
28
use Koch\Exception\Exception;
29
30
class YellowScreenOfDeath
0 ignored issues
show
Coding Style introduced by
YellowScreenOfDeath does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
31
{
32
    /**
33
     * Renders a Koch Framework Exception.
34
     */
35
    public static function renderException($message, $string, $code, $file, $line, $trace)
0 ignored issues
show
Unused Code introduced by
The parameter $string 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...
Coding Style introduced by
renderException uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
renderException uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
36
    {
37
        ob_start();
38
39
        /*
40
         * @todo add backlink to the exception codes list
41
         */
42
        if ($code > 0) {
43
            $code = '(#' . $code . ')';
44
        } else {
45
            $code = '';
46
        }
47
48
        // Header
49
        $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"';
50
        $html .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
51
        $html .= '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">';
52
        $html .= '<head>';
53
        $html .= '<title>Koch Framework Exception ' . $code . ' - ' . $message . '</title>';
54
        $html .= '<link rel="stylesheet" href="' . WWW_ROOT_THEMES_CORE . 'css/error.css" type="text/css" />';
55
        $html .= '</head>';
56
57
        // Body
58
        $html .= '<body>';
59
60
        // Fieldset
61
        $html .= '<fieldset id="top" class="error_yellow">';
62
63
        // Errorlogo
64
        $html .= '<div style="float: left; margin: 5px; margin-right: 25px; padding: 20px;">';
65
        $html .= '<img src="' . WWW_ROOT_THEMES_CORE . 'images/Clansuite-Toolbar-Icon-64-exception.png" ';
66
        $html .= 'style="border: 2px groove #000000;" alt="Clansuite Exception Icon" /></div>';
67
68
        // Fieldset Legend
69
        $html .= '<legend>Koch Framework Exception</legend>';
70
71
        // Exception Table
72
        $html .= '<table width="80%"><tr><td>';
73
74
        /*
75
         * Panel 1
76
         *
77
         * Exception Message and File
78
         */
79
80
        $html .= '<div id="panel1" class="panel">';
81
        $html .= '<h3>Exception ' . $code . '</h3><h4>' . $message . '</h4>';
82
        $html .= '<strong>' . Errorhandler::getFileLink($file, $line) . '.</strong>';
83
        $html .= '</div>';
84
85
        /*
86
         * Panel 2
87
         *
88
         * Debug Backtrace
89
         */
90
        if (defined('DEBUG') and DEBUG === 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
            // lets get the backtrace as html table
92
            $html .= Errorhandler::getDebugBacktrace($trace);
93
        }
94
95
        /*
96
         * Panel 3
97
         *
98
         * Server Environment Informations
99
         */
100
        if (defined('DEBUG') and DEBUG === 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
101
            $html .= '<div id="panel3" class="panel">';
102
            $html .= '<h3>Server Environment</h3>';
103
            $html .= '<table width="95%">';
104
            $html .= '<tr><td><strong>Date: </strong></td><td>' . date('r') . '</td></tr>';
105
            $html .= '<tr><td><strong>Remote: </strong></td><td>' . $_SERVER['REMOTE_ADDR'] . '</td></tr>';
106
            $html .= '<tr><td><strong>Request: </strong></td><td>index.php?' . $_SERVER['QUERY_STRING'] . '</td></tr>';
107
            $html .= '<tr><td><strong>PHP: </strong></td><td>' . PHP_VERSION . ' ' . PHP_EXTRA_VERSION . '</td></tr>';
108
            $html .= '<tr><td><strong>Server: </strong></td><td>' . $_SERVER['SERVER_SOFTWARE'] . '</td></tr>';
109
            $html .= '<tr><td><strong>Agent: </strong></td><td>' . $_SERVER['HTTP_USER_AGENT'] . '</td></tr>';
110
            $html .= '<tr><td><strong>Application: </strong></td>';
111
            $html .= '<td>' . APPLICATION_VERSION . ' ' . APPLICATION_VERSION_STATE;
112
            $html .= ' (' . APPLICATION_VERSION_NAME . ')</td></tr>';
113
            $html .= '</table></div>';
114
        }
115
116
        /*
117
         * Panel 4
118
         *
119
         * Additional Information
120
         */
121
        if (empty(self::$exception_template) === false) {
122
            $html .= '<div id="panel4" class="panel">';
123
            $html .= '<h3>Additional Information & Solution Suggestion</h3>';
124
            $html .= self::$exception_template . '</div>';
125
        }
126
127
        /*
128
         * Panel 5
129
         *
130
         * Rapid Development
131
         */
132
        $placeholders = [];
133
        // assign placeholders for replacements in the html
134
        if (strpos($message, 'action_')) {
135
            $placeholders['actionname'] = substr($message, strpos($message, 'action_'));
136
        } elseif (strpos($message, 'module_')) {
137
            $placeholders['classname'] = substr($message, strpos($message, 'module_'));
138
        }
139
140
        if (empty($_GET['mod']) === false) {
141
            $placeholders['modulename'] = (string) stripslashes($_GET['mod']);
142
        } else {
143
            $placeholders['modulename'] = '';
144
        }
145
146
        // add development helper template to exceptions
147
        if (defined('DEVELOPMENT') and DEVELOPMENT === 1 and defined('RAPIDDEVTPL') and RAPIDDEVTPL === 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
148
            $html .= '<div id="panel5" class="panel">';
149
            $html .= '<h3>Rapid Application Development</h3>';
150
            $html .= Exception::getExceptionDevelopmentTemplate($placeholders) . '</div>';
151
        }
152
153
        /*
154
         * Panel 6
155
         *
156
         * Backlink to Bugtracker with Exceptionmessage
157
         * @link http://trac.clansuite.com/newticket
158
         */
159
        $html .= Errorhandler::getBugtrackerBacklinks($message, $file, $line, $trace);
160
161
        // close all html element table
162
        $html   .= '</table>';
163
164
        /*
165
         * Panel 7
166
         *
167
         * Footer with Support-Backlinks
168
         */
169
        $html  .= Errorhandler::getSupportBacklinks($this);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
The call to Errorhandler::getSupportBacklinks() has too many arguments starting with $this.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
170
171
        // close all html elements: fieldset, body+page
172
        $html   .= '</fieldset>';
173
        $html   .= '</body></html>';
174
175
        // save session before exit - but only if this is not a pdo exception
176
        // that would trigger a fatal error, when trying to write to the db during session save
177
        if ((bool) session_id() and false === strpos($message, 'SQLSTATE')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
178
            session_write_close();
179
        }
180
181
        // clear all output buffers
182
        if (ob_get_length()) {
183
            ob_end_clean();
184
        }
185
186
        // Output the errormessage
187
        return $html;
188
    }
189
190
    /**
191
     * Renders a Koch Framework Error.
192
     *
193
     * @param int    $errno
194
     * @param string $errstr
195
     * @param string $errfile
196
     * @param string $errline
197
     * @param int    $errline
198
     * @param string $errcontext
199
     * @param string $errorname
200
     */
201
    public static function renderError($errno, $errorname, $errstr, $errfile, $errline, $errcontext)
0 ignored issues
show
Coding Style introduced by
renderError uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
202
    {
203
        // shorten errorfile string by removing the root path
204
        $errfile_short     = str_replace(APPLICATION_PATH, '', $errfile);
0 ignored issues
show
Coding Style introduced by
$errfile_short does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
205
        $short_errorstring = \Koch\Functions\Functions::shortenString($errfile, 70, '...');
0 ignored issues
show
Coding Style introduced by
$short_errorstring does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
206
207
        // Header
208
        $html = '<html><head>';
209
        $html .= '<title>Koch Framework Error</title>';
210
        $html .= '<link rel="stylesheet" href="' . WWW_ROOT_THEMES_CORE . 'css/error.css" type="text/css" />';
211
        $html .= '</head>';
212
213
        // Body
214
        $html .= '<body>';
215
216
        // Fieldset with Legend
217
        $html .= '<fieldset id="top" class="error_red">';
218
        $html .= '<legend>Koch Framework Error</legend>';
219
220
        // Add Errorlogo
221
        $html .= '<div style="float: left; margin: 5px; margin-right: 25px; padding: 20px;">';
222
        $html .= '<img src="' . WWW_ROOT_THEMES_CORE . 'images/Clansuite-Toolbar-Icon-64-error.png"';
223
        $html .= ' style="border: 2px groove #000000;"/></div>';
224
225
        // Open Error Table
226
        $html .= '<table width="80%"><tr><td>';
227
228
        // Panel 1 - Errormessage
229
        $html .= '<div id="panel1" class="panel">';
230
        $html .= '<h3>Error - ' . $errorname . ' (' . $errno . ')</h3> ';
231
        $html .= '<p style="font-weight: bold;">' . $errstr . '</p>';
232
        $html .= '<p>in file "<span style="font-weight: bold;">' . $errfile_short . '</span>"';
0 ignored issues
show
Coding Style introduced by
$errfile_short does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
233
        $html .= ' on line #<span style="font-weight: bold;">' . $errline . '.</span></p>';
234
        $html .= '</div>';
235
236
        // Panel 2 - Error Context
237
        $html .= '<div id="panel2" class="panel">';
238
        $html .= '<h3>Context</h3>';
239
        $html .= '<p><span class="small">You are viewing the source code of the file "';
240
        $html .= $errfile . '" around line ' . $errline . '.</span></p>';
241
        $html .= Errorhandler::getErrorContext($errfile, $errline, 8) . '</div>';
242
243
        // Panel 3 - Debug Backtracing
244
        $html .= Errorhandler::getDebugBacktrace($short_errorstring);
0 ignored issues
show
Coding Style introduced by
$short_errorstring does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
245
246
        // Panel 4 - Environmental Informations at Errortime
247
        $html .= '<div id="panel4" class="panel">';
248
        $html .= '<h3>Server Environment</h3>';
249
        $html .= '<p><table width="95%">';
250
        $html .= '<tr><td colspan="2"></td></tr>';
251
        $html .= '<tr><td><strong>Date: </strong></td><td>' . date('r') . '</td></tr>';
252
        $html .= '<tr><td><strong>Remote: </strong></td><td>' . $_SERVER['REMOTE_ADDR'] . '</td></tr>';
253
        $html .= '<tr><td><strong>Request: </strong></td><td>' . htmlentities($_SERVER['QUERY_STRING'], ENT_QUOTES);
254
        $html .= '</td></tr>';
255
        $html .= '<tr><td><strong>PHP: </strong></td><td>' . PHP_VERSION . ' ' . PHP_EXTRA_VERSION . '</td></tr>';
256
        $html .= '<tr><td><strong>Server: </strong></td><td>' . $_SERVER['SERVER_SOFTWARE'] . '</td></tr>';
257
        $html .= '<tr><td><strong>Agent: </strong></td><td>' . $_SERVER['HTTP_USER_AGENT'] . '</td></tr>';
258
        $html .= '<tr><td><strong>Clansuite: </strong></td><td>';
259
        $html .= APPLICATION_VERSION . ' ' . APPLICATION_VERSION_STATE . ' (' . APPLICATION_VERSION_NAME . ')';
260
        $html .= '</td></tr>';
261
        $html .= '</table></p></div>';
262
263
        // Panel 5 - Backlink to Bugtracker with Errormessage -> http://trac.clansuite.com/newticket
264
        $html .= Errorhandler::getBugtrackerBacklinks($errorname, $errfile, $errline, $errcontext);
265
266
        // Close Error Table
267
        $html .= '</table>';
268
269
        // Add Footer with Support-Backlinks
270
        $html .= Errorhandler::getSupportBacklinks();
271
272
        // Close all html elements
273
        $html .= '</fieldset><br /><br />';
274
        $html .= '</body></html>';
275
276
        return $html;
277
    }
278
}
279