Completed
Push — master ( cc10a3...2fa76f )
by Harry
03:37
created

functions.php ➔ json_decode()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6.7441

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 4
dl 0
loc 25
ccs 4
cts 9
cp 0.4444
crap 6.7441
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Guzzle HTTP JSON-RPC
5
 *
6
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
12
 * @link http://github.com/graze/guzzle-jsonrpc
13
 */
14
15
namespace Graze\GuzzleHttp\JsonRpc;
16
17
use InvalidArgumentException;
18
19
/**
20
 * Wrapper for JSON decode that implements error detection with helpful
21
 * error messages.
22
 *
23
 * @param string $json    JSON data to parse
24
 * @param bool $assoc     When true, returned objects will be converted
25
 *                        into associative arrays.
26
 * @param int    $depth   User specified recursion depth.
27
 * @param int    $options Bitmask of JSON decode options.
28
 *
29
 * @throws InvalidArgumentException if the JSON cannot be parsed.
30
 *
31
 * @return mixed
32
 *
33
 * @link http://www.php.net/manual/en/function.json-decode.php
34
 *
35
 * @copyright Copyright (c) 2011-2015 Michael Dowling, https://github.com/mtdowling <[email protected]>
36
 * @license MIT https://github.com/guzzle/guzzle/blob/5.3/LICENSE
37
 */
38
function json_decode($json, $assoc = false, $depth = 512, $options = 0)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
39
{
40 25
    static $jsonErrors = [
41
         JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded',
42
         JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch',
43
         JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found',
44
         JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON',
45
         JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded',
46
    ];
47
48
    // Patched support for decoding empty strings for PHP 7+
49 25
    $data = \json_decode($json == "" ? "{}" : $json, $assoc, $depth, $options);
50
51 25
    if (JSON_ERROR_NONE !== json_last_error()) {
52
        $last = json_last_error();
53
        throw new InvalidArgumentException(
54
            'Unable to parse JSON data: '
55
            . (isset($jsonErrors[$last])
56
                ? $jsonErrors[$last]
57
                : 'Unknown error')
58
        );
59
    }
60
61 25
    return $data;
62
}
63
64
/**
65
 * Wrapper for json_encode that includes character escaping by default.
66
 *
67
 * @param  mixed          $data
68
 * @param  bool        $escapeChars
69
 *
70
 * @return string|bool
71
 */
72
function json_encode($data, $escapeChars = true)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
73
{
74
    $options =
75 3
        JSON_HEX_AMP  |
76 3
        JSON_HEX_APOS |
77 3
        JSON_HEX_QUOT |
78 3
        JSON_HEX_TAG  |
79 3
        JSON_UNESCAPED_UNICODE |
80 3
        JSON_UNESCAPED_SLASHES;
81
82 3
    return \json_encode($data, $escapeChars ? $options : 0);
83
}
84