Completed
Push — master ( 45600c...8f79c4 )
by Paul
10:17 queued 06:57
created

Json   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A decode() 0 12 3
1
<?php
2
3
/**
4
 * This file is part of PHPUnit Generator.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Util;
13
14
use PhpUnitGen\Exception\JsonException;
15
16
/**
17
 * Class Json.
18
 *
19
 * @author     Paul Thébaud <[email protected]>.
20
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
21
 * @license    https://opensource.org/licenses/MIT The MIT license.
22
 * @link       https://github.com/paul-thebaud/phpunit-generator
23
 * @since      Class available since Release 2.0.0.
24
 */
25
class Json
26
{
27
    /**
28
     * Decode a Json string to return an array content.
29
     *
30
     * @param mixed $string The string to decode.
31
     *
32
     * @return mixed The decoded json.
33
     *
34
     * @throws JsonException If the string can not be decoded.
35
     */
36
    public static function decode($string)
37
    {
38
        if (! is_string($string)) {
39
            throw new JsonException('Json decode parameter must be a string');
40
        }
41
42
        $result = json_decode($string, true);
43
44
        if ($result === null) {
45
            throw new JsonException('Json decode error');
46
        }
47
        return $result;
48
    }
49
}
50