Completed
Push — master ( 918554...e03db7 )
by Oleg
04:19 queued 36s
created

FatalError   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 7
c 3
b 1
f 1
lcom 1
cbo 0
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A handle() 0 21 4
A doCli() 0 4 1
B doRun() 0 24 1
1
<?php /** MicroFatalError */
2
3
namespace Micro\Base;
4
5
/**
6
 * Class FatalError
7
 * @package Micro\Base
8
 */
9
class FatalError
10
{
11
    protected static $number = 0;
12
    protected static $message = '';
13
    protected static $file = '';
14
    protected static $line = 0;
15
    protected static $context = [];
16
    protected static $trace = [];
17
18
    /**
19
     * Register FatalError handler
20
     */
21
    public static function register()
22
    {
23
        set_error_handler(['\Micro\Base\FatalError', 'handle']);
24
    }
25
26
    /**
27
     * Fatal error handle
28
     *
29
     * @param int $number
30
     * @param string $message
31
     * @param string $file
32
     * @param int $line
33
     * @param array $context
34
     */
35
    public static function handle($number = 0, $message = '', $file = '', $line = 0, $context = [])
36
    {
37
        self::$context = $context;
38
        self::$message = $message;
39
        self::$number = $number;
40
        self::$trace = debug_backtrace();
41
        self::$file = $file;
42
        self::$line = $line;
43
44
        $level = ob_get_level();
45
        if ($level > 0) {
46
            for ($i = ob_get_level(); $i >= 0; $i--) {
47
                ob_clean();
48
            }
49
        }
50
51
//        ob_clean();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
//        ob_end_clean();
53
54
        print('cli' === php_sapi_name() ? static::doCli() : static::doRun());
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    protected static function doCli()
61
    {
62
        return static::$number . ' - ' . static::$message . ' on ' . static::$file . ':' . static::$line;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    protected static function doRun()
69
    {
70
        $str = '<div class="error" style="width: 100%;">';
71
        $str .= '<h2>FatalError ' . static::$number . ' - ' . static::$message . ' on ' . static::$file . ':' . static::$line . '</h2>';
72
73
        $str .= '<table width="100%" style="width: 100%">';
74
        $str .= '<tr>';
75
        $str .= '<th width="100px">Context</th>';
76
        $str .= '<td style="vertical-align: top; height: 300px">';
77
        $str .= '<textarea disabled style="width:100%; height: 100%">' . print_r(static::$context,
78
                true) . '</textarea>';
79
        $str .= '</td>';
80
        $str .= '</tr>';
81
        $str .= '<tr>';
82
        $str .= '<th width="100px">Debug trace</th>';
83
        $str .= '<td style="vertical-align: top; height: 300px">';
84
        $str .= '<textarea disabled style="width: 100%; height: 100%">' . print_r(static::$trace, true) . '</textarea>';
85
        $str .= '</td>';
86
        $str .= '</tr>';
87
        $str .= '</table>';
88
        $str .= '</div>';
89
90
        return $str;
91
    }
92
}
93