Completed
Push — master ( 3afa74...c8e68d )
by Bernhard
02:34
created

JsonError::getErrorMessage()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 35.9648

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 30
ccs 9
cts 22
cp 0.4091
rs 5.2653
cc 11
eloc 23
nc 11
nop 1
crap 35.9648

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
 * This file is part of the Webmozart JSON package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Json;
13
14
/**
15
 * @since  1.0
16
 *
17
 * @author Bernhard Schussek <[email protected]>
18
 */
19
class JsonError
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
20
{
21
    /**
22
     * User-land implementation of `json_last_error_msg()` for PHP < 5.5.
23
     *
24
     * @return string The last JSON error message.
25
     */
26 5
    public static function getLastErrorMessage()
27
    {
28 5
        return self::getErrorMessage(json_last_error());
29
    }
30
31
    /**
32
     * Returns the error message of a JSON error code.
33
     *
34
     * @param int $error The error code.
35
     *
36
     * @return string The error message.
37
     */
38 5
    public static function getErrorMessage($error)
39
    {
40
        switch ($error) {
41 5
            case JSON_ERROR_NONE:
42
                return 'JSON_ERROR_NONE';
43 5
            case JSON_ERROR_DEPTH:
44 2
                return 'JSON_ERROR_DEPTH';
45 3
            case JSON_ERROR_STATE_MISMATCH:
46
                return 'JSON_ERROR_STATE_MISMATCH';
47 3
            case JSON_ERROR_CTRL_CHAR:
48
                return 'JSON_ERROR_CTRL_CHAR';
49 3
            case JSON_ERROR_SYNTAX:
50
                return 'JSON_ERROR_SYNTAX';
51 3
            case JSON_ERROR_UTF8:
52 3
                return 'JSON_ERROR_UTF8';
53
        }
54
55
        if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
56
            switch ($error) {
57
                case JSON_ERROR_RECURSION:
58
                    return 'JSON_ERROR_RECURSION';
59
                case JSON_ERROR_INF_OR_NAN:
60
                    return 'JSON_ERROR_INF_OR_NAN';
61
                case JSON_ERROR_UNSUPPORTED_TYPE:
62
                    return 'JSON_ERROR_UNSUPPORTED_TYPE';
63
            }
64
        }
65
66
        return 'JSON_ERROR_UNKNOWN';
67
    }
68
69
    private function __construct()
70
    {
71
    }
72
}
73