Completed
Pull Request — master (#2)
by ARCANEDEV
03:07
created

JsonErrorHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
dl 0
loc 98
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
ccs 12
cts 13
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handleDecodeErrors() 0 6 1
A handleEncodeErrors() 0 6 2
A makeException() 0 11 2
1
<?php namespace Arcanedev\Json;
2
3
class JsonErrorHandler
4
{
5
    /* ------------------------------------------------------------------------------------------------
6
     |  Properties
7
     | ------------------------------------------------------------------------------------------------
8
     */
9
    /**
10
     * Handled errors list.
11
     *
12
     * @var array
13
     */
14
    protected static $errors  = [
15
        'decode'  => [
16
            'default' => Exceptions\Decode\DecodeException::class,
17
18
            'handled' => [
19
                JSON_ERROR_DEPTH          => Exceptions\Decode\DecodeDepthException::class,
20
                JSON_ERROR_STATE_MISMATCH => Exceptions\Decode\StateMismatchException::class,
21
                JSON_ERROR_CTRL_CHAR      => Exceptions\Decode\ControlCharacterException::class,
22
                JSON_ERROR_SYNTAX         => Exceptions\Decode\SyntaxException::class,
23
                JSON_ERROR_UTF8           => Exceptions\Decode\UTF8Exception::class,
24
            ],
25
        ],
26
27
        'encode'  => [
28
            'default' => Exceptions\Encode\EncodeException::class,
29
30
            'handled' => [
31
                JSON_ERROR_DEPTH                 => Exceptions\Encode\EncodeDepthException::class,
32
                JSON_ERROR_RECURSION             => Exceptions\Encode\RecursionException::class,
33
                JSON_ERROR_INF_OR_NAN            => Exceptions\Encode\InfiniteOrNotANumberException::class,
34
                JSON_ERROR_UNSUPPORTED_TYPE      => Exceptions\Encode\UnsupportedTypeException::class,
35
                JSON_ERROR_INVALID_PROPERTY_NAME => Exceptions\Encode\InvalidPropertyNameException::class,
36
            ],
37
        ],
38
    ];
39
40
    /* ------------------------------------------------------------------------------------------------
41
     |  Main Functions
42
     | ------------------------------------------------------------------------------------------------
43
     */
44
    /**
45
     * Handle the decode json errors.
46
     *
47
     * @param  int    $error
48
     * @param  int    $options
49
     * @param  array  $replaces
50
     *
51
     * @throws Exceptions\JsonException|Exceptions\Decode\DecodeException
52
     */
53 33
    public static function handleDecodeErrors($error, $options, array $replaces = [])
54
    {
55 33
        unset($options); // Use the unused argument
56
57 33
        throw static::makeException('decode', $error, $replaces);
0 ignored issues
show
Bug introduced by
Since makeException() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of makeException() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
58
    }
59
60
    /**
61
     * Handle the encode json errors.
62
     *
63
     * @param  int    $error
64
     * @param  int    $options
65
     * @param  array  $replaces
66
     *
67
     * @throws Exceptions\JsonException|Exceptions\Encode\EncodeException
68
     */
69 30
    public static function handleEncodeErrors($error, $options = 0, array $replaces = [])
70
    {
71 30
        if (($options & JSON_PARTIAL_OUTPUT_ON_ERROR) === 0) {
72 30
            throw static::makeException('encode', $error, $replaces);
0 ignored issues
show
Bug introduced by
Since makeException() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of makeException() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
73
        }
74
    }
75
76
    /* ------------------------------------------------------------------------------------------------
77
     |  Other Functions
78
     | ------------------------------------------------------------------------------------------------
79
     */
80
    /**
81
     * Make an exception.
82
     *
83
     * @param  string  $type
84
     * @param  int     $error
85
     * @param  array   $replaces
86
     *
87
     * @return Exceptions\JsonException|Exceptions\Decode\DecodeException|Exceptions\Encode\EncodeException
88
     */
89 63
    private static function makeException($type, $error, array $replaces = [])
90
    {
91
        /** @var  \Arcanedev\Json\Exceptions\JsonException  $ex */
92 63
        $ex = array_key_exists($error, self::$errors[$type]['handled'])
93 59
            ? new self::$errors[$type]['handled'][$error]
94 63
            : new self::$errors[$type]['default'];
95
96 63
        $ex->replace($replaces);
97
98 63
        return $ex;
99
    }
100
}
101