Completed
Push — master ( 0459a0...77b634 )
by Vitaly
11:28 queued 01:43
created

Error::output()   B

Complexity

Conditions 9
Paths 48

Size

Total Lines 56
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
ccs 0
cts 25
cp 0
rs 7.1584
cc 9
eloc 22
nc 48
nop 0
crap 90

How to fix   Long Method   

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
namespace samson\core;
3
4
// TODO: Should separated to external module
5
6
//
7
// Коды ошибок
8
// Новые коды должны попадать в диапазон ( E_SAMSON_FATAL_ERROR - D_SAMSON_DEBUG )
9
//
10
11
/** Критическая ошибка в системы, после ее срабатывания система прекращает работу */
12
define( 'E_SAMSON_FATAL_ERROR', 900 );
13
/** Ошибка при работе с ресурсами системы */
14
define( 'E_SAMSON_SRC_ERROR', 901 );
15
/** Ошибка в ядре системы */
16
define( 'E_SAMSON_CORE_ERROR', 999 );
17
/** Ошибка в АктивнойЗаписи */
18
define( 'E_SAMSON_ACTIVERECORD_ERROR', 998 );
19
/** Ошибка в SQL */
20
define( 'E_SAMSON_SQL_ERROR', 997 );
21
/** Ошибка в работе SamsonCMS */
22
define( 'E_SAMSON_CMS_ERROR', 996 );
23
/** Ошибка в AUTH */
24
define( 'E_SAMSON_AUTH_ERROR', 995 );
25
/** Ошибка в компрессоре системы */
26
define( 'E_SAMSON_SNAPSHOT_ERROR', 994 );
27
/** Ошибка в компрессоре системы */
28
define( 'E_SAMSON_RENDER_ERROR', 993 );
29
30
//
31
// Коды для отладочных сообщений
32
// Новые коды должны все быть больше D_SAMSON_DEBUG(10000)
33
//
34
35
/**
36
 * Отладочное сообщение
37
 */
38
define( 'D_SAMSON_DEBUG', 10000 );
39
/**
40
 * Отладочное сообщение ActiveRecord
41
 */
42
define( 'D_SAMSON_ACTIVERECORD_DEBUG', 10001 );
43
/**
44
 * Отладочное сообщение в CMS
45
 */
46
define( 'D_SAMSON_CMS_DEBUG', 10002 );
47
/**
48
 * Отладочное сообщение в AUTH
49
 */
50
define( 'D_SAMSON_AUTH_DEBUG', 10003 );
51
52
53
/**
54
 * Обработчик ошибок SamsonPHP
55
 *
56
 * @package SamsonPHP
57
 * @author Vitaly Iegorov <[email protected]> 
58
 * @version 1.6
59
 */
60
class Error
61
{
62
	/**
63
	 * Глобальный флаг вывода отладочных сообщений
64
	 * @var boolean
65
	 */
66
	public static $OUTPUT = TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
67
	/**
68
	 * Путь к файлу с CSS представлением ошибок
69
	 * @var string
70
	 */
71
	public static $css = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
72
	/**
73
	 * Указатель на внешнюю функцию для обработки завершения работы скрипта
74
	 * @var callable
75
	 */
76
	public static $shutdown_handler;
77
	/**
78
	 * Коллекция ошибок и предупреждений для отладки и правильной работы
79
	 * приложения
80
	 */
81
	private $errors = array();
82
83
	/**
84
	 * Конструктор
85
	 */
86
	public function __construct()
87
	{
88
		// Уберем стандартный вывод ошибок PHP
89
		error_reporting(false);
90
91
		// Зарегистрируем функцию обработчик исключений
92
		//set_exception_handler( array( $this, 'exception_handler'));
93
94
		// Обработчик ошибок
95
		set_error_handler(array($this, 'handler'));
96
97
		// Зарегистрируем функцию обработчик завершения работы системы
98
		register_shutdown_function(array($this, 'shutdown'));
99
100
		// Сформируем "универсально" путь к CSS представлению ошибок
101
		if (file_exists(__SAMSON_PATH__ . 'css/error.css')) self::$css = file_get_contents(__SAMSON_PATH__ . 'css/error.css');
0 ignored issues
show
Deprecated Code introduced by
The constant __SAMSON_PATH__ has been deprecated with message: Define path to SamsonPHP framework

This class constant has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
102
	}
103
104
	/**
105
	 * Обработчик завершения работы скрипта
106
	 */
107
	public function shutdown()
108
	{
109
		// Если установлен обработчик завершения выполнения скрипта - вызовем его
110
		if (isset(self::$shutdown_handler) && (call_user_func(self::$shutdown_handler) === false)) return null;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
111
112
		//echo 'Конец';
113
114
		// Выведем все накопленные ошибки
115
		$this->output();
116
	}
117
118
	/**
119
	 * Обработчик завершения работы скрипта
120
	 */
121
	public function output()
122
	{
123
		// Получим последнюю ошибку системы
124
		$lerror = error_get_last();
125
126
		// Коды ошибок которые останавливают скрипт
127
		$fatal_codes = array(1, 4, 16, 64);
128
129
		// Если была фатальная ошибка преобразуем её для вывода
130
		if (isset($lerror) && in_array($lerror['type'], $fatal_codes)) {
131
			// Выполним обработчик ошибки и преобразование её в наше представление
132
			$this->handler($lerror['type'], $lerror['message'], $lerror['file'], $lerror['line'], null, debug_backtrace(FALSE));
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
133
		}
134
135
		// Соберем представление ошибок в HTML
136
		$html = '';
137
138
		// Выведем файл CSS стилей, если он есть! и были ошибки
139
		if (sizeof($this->errors) && isset(self::$css{0})) $html .= '<style>' . self::$css . '</style>';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
140
141
		// Индекс
142
		$index = 0;
143
144
		// Переберем ошибки которые были при выполнения скрипта
145
		foreach ($this->errors as & $error) {
146
			if ($error['class'] == '_core_debug') $html .= '<div class="_core_error _core_debug">' . $error['message'] . '</div>';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
147
			else {
148
				$id = rand(0, 999999999) . rand(0, 9999999999);
149
				// Выведем ошибку
150
				$html .= '<div class="_core_error ' . $error['class'] . '">
151
						<input type="checkbox" id="eb_' . $id . '" class="_core_error_check_box">
152
						<label for="eb_' . $id . '" class="_core_error_label">
153
						<span class="_core_error_type">' . $error['type'] . '</span>
154
						<span class="_core_error_file">' . $error['file'] . '</span>
155
						<span class="_core_error_line">, стр. ' . $error['line'] . '</span>
156
						' . $error['message'] . '</label>
157
					</div>';
158
			}
159
160
			// Удалим ошибку которую мы вывели что бы она не вылезла внизу страницы повторно
161
			unset($this->errors[$index++]);
162
		}
163
164
		// Выведем блок для завершения вывода
165
		if ($index) $html .= '<div style="clear:both;"></div>';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
166
167
		/*
168
		// Если єто отпечаток сайта - отправим ошибку на почту
169
		if( isset($GLOBALS["__CORE_SNAPSHOT"]) && $index )
170
		try { 	mail_send( '[email protected]','[email protected]', $html, 'Ошибка PHP:'.url()->build() );	}
171
		catch (Exception $e) {}
172
		*/
173
174
		// Если вывод ошибок включен
175
		if (self::$OUTPUT) echo $html;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
176
	}
177
178
	/**
179
	 * Обработчик ошибок PHP
180
	 * Сигнатура метода совпадает с требованиями PHP
181
	 *
182
	 * @param integer $errno      Код ошибки
183
	 * @param string  $errfile    Файл в котором происходит ошибка
184
	 * @param string  $errline    Строка в которой была ошибка
185
	 * @param string  $errcontext Контекст в котором произошла ошибка
186
	 */
187
	public function handler($errno, $error_msg, $errfile = NULL, $errline = NULL, $errcontext = NULL, $backtrace = NULL)
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
188
	{
189
		// Если вывод ошибок включен
190
		if (!self::$OUTPUT) return NULL;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
191
192
		// Если не передан стек вызовов получим текущий
193
		if (!isset($backtrace)) $backtrace = debug_backtrace(FALSE);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
194
195
		// Если сообщение установлено
196
		if (!isset($error_msg) || ($error_msg == 'Undefined') || (!isset($error_msg{0}))) return FALSE;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
197
198
		// Указатель на "правильный" уровень стека вызовов
199
		if (isset($backtrace[1])) $callee = &$backtrace[1];
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
200
201
		// Если ошибка вызывается из функции то укажем ее
202
		if (isset($callee['function'])) $error_msg = '<u>' . $callee['function'] . '()</u>&nbsp;' . $error_msg;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
203
204
		// Если ошибка вызывается из класса то укажем его
205
		if (isset($callee['class'])) $error_msg = '<u>' . $callee['class'] . '</u>::' . $error_msg;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
206
207
		// Если не передан файл где была ошибка
208
		if (!isset($errfile)) $errfile = isset($callee['file']) ? $callee['file'] : '';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
209
210
		// Если не передана линия где была ошибка
211
		if (!isset($errline)) $errline = isset($callee['line']) ? $callee['line'] : '';
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
212
213
		// Сформируем стек вызовов для разбора ошибки
214
		$error_msg = debug_backtrace_html($error_msg, $backtrace);
215
216
		// Описание типа
217
		$error_type = '';
0 ignored issues
show
Unused Code introduced by
$error_type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
218
219
		// CSS класс ошибки
220
		$error_css = '';
221
222
		// Определим описание типа ошибки
223
		switch ($errno) {
224
			case E_ERROR:
225
				$error_type = 'Фатальная ошибка';
226
				$error_css = '_core_fatal_error';
227
				break;
228
			case E_CORE_ERROR:
229
				$error_type = 'Ошибка ядра PHP';
230
				$error_css = '_core_fatal_error';
231
				break;
232
			case E_SAMSON_FATAL_ERROR:
233
				$error_type = 'Фатальная ошибка SamsonPHP';
234
				$error_css = '_core_fatal_error';
235
				break;
236
			case E_SAMSON_SNAPSHOT_ERROR:
237
				$error_type = 'Ошибка создания отпечатка SamsonPHP';
238
				break;
239
			case E_SAMSON_RENDER_ERROR:
240
				$error_type = 'SamsonPHP rendering error';
241
				break;
242
			case E_PARSE:
243
				$error_type = 'Ошибка парсинга';
244
				$error_css = '_core_fatal_error';
245
				break;
246
			case E_COMPILE_ERROR:
247
				$error_type = 'Ошибка компиляции';
248
				$error_css = '_core_fatal_error';
249
				break;
250
			case E_USER_ERROR:
251
				$error_type = 'Пользовательская ошибка';
252
				break;
253
			case E_WARNING:
254
				$error_type = 'Предупреждение';
255
				break;
256
			case E_CORE_WARNING:
257
				$error_type = 'Предупреждение ядра PHP';
258
				break;
259
			case E_COMPILE_WARNING:
260
				$error_type = 'Предупреждение компиляции';
261
				break;
262
			case E_USER_WARNING:
263
				$error_type = 'Пользовательское предупреждение';
264
				break;
265
			case E_NOTICE:
266
				$error_type = 'Замечание';
267
				break;
268
			case E_USER_NOTICE:
269
				$error_type = 'Пользовательское замечание';
270
				break;
271
			case E_STRICT:
272
				$error_type = 'Строгая ошибка';
273
				break;
274
			case E_RECOVERABLE_ERROR:
275
				$error_type = 'Востанавливаемая ошибка';
276
				break;
277
			case E_DEPRECATED:
278
				$error_type = 'Использование устаревших данных';
279
				break;
280
			case E_USER_DEPRECATED:
281
				$error_type = 'Старая функция';
282
				break;
283
			case E_SAMSON_CORE_ERROR:
284
				$error_type = 'Ошибка SamsonPHP';
285
				break;
286
			case E_SAMSON_ACTIVERECORD_ERROR:
287
				$error_type = 'Ошибка ActiveRecord';
288
				break;
289
			case E_SAMSON_CMS_ERROR:
290
				$error_type = 'Ошибка SamsonCMS';
291
				break;
292
			case D_SAMSON_DEBUG:
293
				$error_type = 'Отладка SamsonPHP';
294
				$error_css = '_core_debug';
295
				break;
296
			case D_SAMSON_CMS_DEBUG:
297
				$error_type = 'Отладка SamsonCMS';
298
				$error_css = '_core_debug';
299
				break;
300
			case D_SAMSON_ACTIVERECORD_DEBUG:
301
				$error_type = 'Отладка ActiveRecord';
302
				$error_css = '_core_debug';
303
				break;
304
			default:
305
				$error_type = 'Неизвестная ошибка';
306
		}
307
308
		// Сформируем ошибку в виде массива
309
		$this->errors[] = $this->toError($errno, $error_type . ':', $error_msg, $errfile, $errline, $error_css);
310
311
		// Если это фатальная ошибка - остановим выполнение скрипта
312
		if ($errno == E_SAMSON_FATAL_ERROR) die();
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
313
	}
314
315
	/**
316
	 * Преобразовать параметры ошибки в массив-ошибку
317
	 *
318
	 * @param integer $errno   Код ошибки
319
	 * @param string  $desc    Описание ошибки
320
	 * @param string  $errfile Файл в котором происходит ошибка
321
	 * @param string  $errline Строка в которой была ошибка
322
	 * @param string  $error_msg
323
	 * @param string  $class
324
	 *
325
	 * @return array Массив ошибка
326
	 */
327
	private function toError($errno, $desc, $error_msg, $errfile = NULL, $errline = NULL, $class = NULL)
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
328
	{
329
		return array(
330
				'code' => $errno,
331
				'type' => $desc,
332
				'file' => $errfile,
333
				'line' => $errline,
334
				'message' => $error_msg,
335
				'class' => $class
336
		);
337
	}
338
}
339