Completed
Push — master ( 683f7d...5b8e9d )
by Nicolas
02:38
created

JSON::getJsonLastErrorMsg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace Elastica;
3
4
use Elastica\Exception\JSONParseException;
5
6
/**
7
 * Elastica JSON tools.
8
 */
9
class JSON
10
{
11
    /**
12
     * Parse JSON string to an array.
13
     *
14
     * @link http://php.net/manual/en/function.json-decode.php
15
     * @link http://php.net/manual/en/function.json-last-error.php
16
     *
17
     * @param string $args,... JSON string to parse
0 ignored issues
show
Bug introduced by
There is no parameter named $args,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
18
     *
19
     * @throws JSONParseException
20
     *
21
     * @return array PHP array representation of JSON string
22
     */
23
    public static function parse($args/* inherit from json_decode */)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        // extract arguments
26
        $args = func_get_args();
27
28
        // default to decoding into an assoc array
29
        if (count($args) === 1) {
30
            $args[] = true;
31
        }
32
33
        // run decode
34
        $array = call_user_func_array('json_decode', $args);
35
36
        // turn errors into exceptions for easier catching
37
        if ($error = self::getJsonLastErrorMsg()) {
38
            throw new JSONParseException($error);
39
        }
40
41
        // output
42
        return $array;
43
    }
44
45
    /**
46
     * Convert input to JSON string with standard options.
47
     *
48
     * @link http://php.net/manual/en/function.json-encode.php
49
     * @link http://php.net/manual/en/function.json-last-error.php
50
     *
51
     * @param mixed $args,... Target to stringify
0 ignored issues
show
Bug introduced by
There is no parameter named $args,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
     *
53
     * @throws JSONParseException
54
     *
55
     * @return string Valid JSON representation of $input
56
     */
57
    public static function stringify($args/* inherit from json_encode */)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        // extract arguments
60
        $args = func_get_args();
61
62
        // run encode and output
63
        $string = call_user_func_array('json_encode', $args);
64
65
        // turn errors into exceptions for easier catching
66
        if ($error = self::getJsonLastErrorMsg()) {
67
            throw new JSONParseException($error);
68
        }
69
70
        // output
71
        return $string;
72
    }
73
74
    /**
75
     * Get Json Last Error
76
     *
77
     * @link http://php.net/manual/en/function.json-last-error.php
78
     * @link http://php.net/manual/en/function.json-last-error-msg.php
79
     * @link https://github.com/php/php-src/blob/master/ext/json/json.c#L308
80
     *
81
     * @return string
82
     */
83
    private static function getJsonLastErrorMsg()
84
    {
85
        return JSON_ERROR_NONE !== json_last_error() ? json_last_error_msg() : false;
86
    }
87
}
88