Passed
Push — develop ( 5def3e...6cdbbb )
by Paul
02:27
created

Json::decode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Util;
4
5
use PhpUnitGen\Exception\JsonException;
6
7
/**
8
 * Class Json.
9
 *
10
 * @author     Paul Thébaud <[email protected]>.
11
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
12
 * @license    https://opensource.org/licenses/MIT The MIT license.
13
 * @link       https://github.com/paul-thebaud/phpunit-generator
14
 * @since      Class available since Release 2.0.0.
15
 */
16
class Json
17
{
18
    /**
19
     * Decode a Json string to return an array content.
20
     *
21
     * @param mixed $string The string to decode.
22
     *
23
     * @return array The decoded array.
24
     *
25
     * @throws JsonException If the string can not be decoded.
26
     */
27
    public static function decode($string): array
28
    {
29
        if (! is_string($string)) {
30
            throw new JsonException('Json decode parameter must be a string');
31
        }
32
33
        $array = json_decode($string, true);
34
35
        if ($array !== null) {
36
            $error = error_get_last();
37
            throw new JsonException($error['message']);
38
        }
39
        return $array;
40
    }
41
}
42