ErrorCodes::codeToString()   C
last analyzed

Complexity

Conditions 16
Paths 16

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 16

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 27
cts 27
cp 1
rs 5.0151
cc 16
eloc 28
nc 16
nop 1
crap 16

How to fix   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
namespace Jasny\ErrorHandler;
4
5
use Psr\Log\LogLevel;
6
7
/**
8
 * Trait for using E_* error codes
9
 */
10
trait ErrorCodes
11
{
12
    /**
13
     * Get the log level for an error code
14
     * 
15
     * @param int $code  E_* error code
16
     * @return string
17
     */
18 64
    protected function getLogLevel($code = null)
19
    {
20
        switch ($code) {
21 64
            case E_STRICT:
22 60
            case E_DEPRECATED:
23 58
            case E_USER_DEPRECATED:
24 8
                return LogLevel::INFO;
25
            
26 56
            case E_NOTICE:
27 52
            case E_USER_NOTICE:
28 6
                return LogLevel::NOTICE;
29
                
30 50
            case E_WARNING:
31 44
            case E_CORE_WARNING:
32 42
            case E_COMPILE_WARNING:
33 40
            case E_USER_WARNING:
34 14
                return LogLevel::WARNING;
35
            
36 36
            case E_PARSE:
37 32
            case E_CORE_ERROR:
38 30
            case E_COMPILE_ERROR:
39 8
                return LogLevel::CRITICAL;
40
            
41
            default:
42 28
                return LogLevel::ERROR;
43
        }
44
    }
45
    
46
    /**
47
     * Turn an error code into a string
48
     * 
49
     * @param int $code
50
     * @return string
51
     */
52 52
    protected function codeToString($code)
53
    {
54
        switch ($code) {
55 52
            case E_ERROR:
56 48
            case E_USER_ERROR:
57 44
            case E_RECOVERABLE_ERROR:
58 14
                return 'Fatal error';
59 38
            case E_WARNING:
60 32
            case E_USER_WARNING:
61 10
                return 'Warning';
62 28
            case E_PARSE:
63 4
                return 'Parse error';
64 24
            case E_NOTICE:
65 20
            case E_USER_NOTICE:
66 6
                return 'Notice';
67 18
            case E_CORE_ERROR:
68 2
                return 'Core error';
69 16
            case E_CORE_WARNING:
70 2
                return 'Core warning';
71 14
            case E_COMPILE_ERROR:
72 2
                return 'Compile error';
73 12
            case E_COMPILE_WARNING:
74 2
                return 'Compile warning';
75 10
            case E_STRICT:
76 4
                return 'Strict standards';
77 6
            case E_DEPRECATED:
78 4
            case E_USER_DEPRECATED:
79 4
                return 'Deprecated';
80
        }
81
        
82 2
        return 'Unknown error';
83
    }
84
}
85
86