Passed
Pull Request — master (#19)
by André
11:58
created

Json::decode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Demv\JSend;
6
7
use InvalidArgumentException;
8
9
class Json
10
{
11
    /**
12
     * Decodes the given JSON string or fails with an exception.
13
     *
14
     * @param string $json
15
     * @return array
16
     * @throws InvalidArgumentException
17
     */
18
    public static function decode(string $json): array
19
    {
20
        $data = json_decode($json, true);
21
22
        if (JSON_ERROR_NONE !== json_last_error()) {
23
            throw new InvalidArgumentException(
24
                'json_decode error: ' . json_last_error_msg()
25
            );
26
        }
27
28
        return $data;
29
    }
30
}
31