Completed
Push — master ( 21baa1...09e8b4 )
by Russell
13s queued 11s
created

SentrySeverity::from_error()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 4.8666
c 0
b 0
f 0
cc 18
nc 18
nop 1

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
/**
4
 * Class: SentrySeverity.
5
 *
6
 * @author  Russell Michell 2019 <[email protected]>
7
 * @package phptek/sentry
8
 */
9
10
namespace PhpTek\Sentry\Adaptor;
11
12
use Sentry\Severity;
13
14
/**
15
 * SentrySeverity provides static methods that process or refine incoming severities
16
 * as integers (from PHP's severity constants, or as strings from userland config).
17
 */
18
class SentrySeverity
19
{
20
    /**
21
     * Maps PHP's internal error-types into those suited to {@link Severity}.
22
     *
23
     * @param  mixed int|string $severity The incoming level from userland code or
24
     *                                    PHP itself.
25
     * @return string
26
     */
27
    public static function process_severity($severity) : string
28
    {
29
        // Stringified PHP severities out of \debug_backtrace() like "notice"
30
        if (is_string($severity)) {
31
            $level = self::from_error($severity);
32
        // De-facto PHP severities as constants (ints) like E_NOTICE
33
        } elseif (is_numeric($severity)) {
34
            $level = Severity::fromError($severity);
35
        } else {
36
            // "Other"
37
            $level = Severity::ERROR;
38
        }
39
40
        return strtolower($level);
41
    }
42
43
    /**
44
     * Almost an exact replica of {@link Severity::fromError()}, except we're
45
     * dealing with string values passed to us from upstream processes.
46
     *
47
     * @param  string $severity An incoming severity.
48
     * @return string
49
     */
50
    public static function from_error(string $severity) : string
51
    {
52
        $severity = strtolower($severity);
53
54
        switch ($severity) {
55
            case 'deprecated':
56
            case 'user_deprecated':
57
            case 'warning':
58
            case 'user_warning':
59
                return Severity::WARNING;
60
            case 'error':
61
            case 'parse':
62
            case 'coreerror':
63
            case 'corwarning': // Possibly misspelling
64
            case 'corewarning':
65
            case 'compilerrror': // Possibly misspelling
66
            case 'compilerror':
67
            case 'compilewarning':
68
                return Severity::FATAL;
69
            case 'recoverablerror':
70
            case 'user_error':
71
                return Severity::ERROR;
72
            case 'notice':
73
            case 'user_notice':
74
            case 'strict':
75
                return Severity::INFO;
76
            default:
77
                // It's an error until proven otherwise
78
                return Severity::ERROR;
79
        }
80
    }
81
82
}
83