Completed
Pull Request — v1 (#450)
by Denis
02:54
created

Misc   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 45.83%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 69
ccs 11
cts 24
cp 0.4583
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isLevelFatal() 0 10 1
A canSendHeaders() 0 4 2
B translateErrorCode() 0 12 5
A isAjaxRequest() 0 6 2
A isCommandLine() 0 4 1
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Util;
8
9
class Misc
10
{
11
    /**
12
	 * Can we at this point in time send HTTP headers?
13
	 *
14
	 * Currently this checks if we are even serving an HTTP request,
15
	 * as opposed to running from a command line.
16
	 *
17
	 * If we are serving an HTTP request, we check if it's not too late.
18
	 *
19
	 * @return bool
20
	 */
21 1
    public static function canSendHeaders()
22
    {
23 1
        return isset($_SERVER["REQUEST_URI"]) && !headers_sent();
24
    }
25
26
    public static function isAjaxRequest()
27
    {
28
        return (
29
            !empty($_SERVER['HTTP_X_REQUESTED_WITH'])
30
            && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
31
    }
32
33
    /**
34
     * Check, if possible, that this execution was triggered by a command line.
35
     * @return bool
36
     */
37
    public static function isCommandLine()
38
    {
39
        return PHP_SAPI == 'cli';
40
    }
41
42
    /**
43
	 * Translate ErrorException code into the represented constant.
44
	 *
45
	 * @param int $error_code
46
	 * @return string
47
	 */
48 2
    public static function translateErrorCode($error_code)
49
    {
50 2
        $constants = get_defined_constants(true);
51 2
        if (array_key_exists('Core' , $constants)) {
52 2
            foreach ($constants['Core'] as $constant => $value) {
53 2
                if (substr($constant, 0, 2) == 'E_' && $value == $error_code) {
54 1
                    return $constant;
55
                }
56 2
            }
57 1
        }
58 1
        return "E_UNKNOWN";
59
    }
60
    
61
    /**
62
     * Determine if an error level is fatal (halts execution)
63
     * 
64
     * @param int $level
65
     * @return bool
66
     */
67
    public static function isLevelFatal($level)
68
    {
69
        $errors = E_ERROR;
70
        $errors |= E_PARSE;
71
        $errors |= E_CORE_ERROR;
72
        $errors |= E_CORE_WARNING;
73
        $errors |= E_COMPILE_ERROR;
74
        $errors |= E_COMPILE_WARNING;
75
        return ($level & $errors) > 0;
76
    }
77
}
78