Completed
Push — master ( 4bfd5a...f9d52a )
by Bernhard
03:48
created

JsonError   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 44%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 13
c 2
b 2
f 0
lcom 0
cbo 0
dl 0
loc 54
ccs 11
cts 25
cp 0.44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastErrorMessage() 0 4 1
A __construct() 0 3 1
C getErrorMessage() 0 30 11
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 9
    public static function getLastErrorMessage()
27
    {
28 9
        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 9
    public static function getErrorMessage($error)
39
    {
40
        switch ($error) {
41 9
            case JSON_ERROR_NONE:
42
                return 'JSON_ERROR_NONE';
43 9
            case JSON_ERROR_DEPTH:
44 4
                return 'JSON_ERROR_DEPTH';
45 5
            case JSON_ERROR_STATE_MISMATCH:
46
                return 'JSON_ERROR_STATE_MISMATCH';
47 5
            case JSON_ERROR_CTRL_CHAR:
48
                return 'JSON_ERROR_CTRL_CHAR';
49 5
            case JSON_ERROR_SYNTAX:
50
                return 'JSON_ERROR_SYNTAX';
51 5
            case JSON_ERROR_UTF8:
52 5
                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