Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
created

lib/Elastica/JSON.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Elastica;
4
5
use Elastica\Exception\JSONParseException;
6
7
/**
8
 * Elastica JSON tools.
9
 */
10
class JSON
11
{
12
    /**
13
     * Parse JSON string to an array.
14
     *
15
     * @see http://php.net/manual/en/function.json-decode.php
16
     * @see http://php.net/manual/en/function.json-last-error.php
17
     *
18
     * @param string $args,... JSON string to parse
0 ignored issues
show
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...
19
     *
20
     * @throws JSONParseException
21
     *
22
     * @return array PHP array representation of JSON string
23
     */
24
    public static function parse($args/* inherit from json_decode */)
0 ignored issues
show
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...
25
    {
26
        // extract arguments
27
        $args = func_get_args();
28
29
        // default to decoding into an assoc array
30
        if (1 === count($args)) {
31
            $args[] = true;
32
        }
33
34
        // run decode
35
        $array = call_user_func_array('json_decode', $args);
36
37
        // turn errors into exceptions for easier catching
38
        if ($error = self::getJsonLastErrorMsg()) {
39
            throw new JSONParseException($error);
40
        }
41
42
        // output
43
        return $array;
44
    }
45
46
    /**
47
     * Convert input to JSON string with standard options.
48
     *
49
     * @see http://php.net/manual/en/function.json-encode.php
50
     * @see http://php.net/manual/en/function.json-last-error.php
51
     *
52
     * @param mixed $args,... Target to stringify
0 ignored issues
show
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...
53
     *
54
     * @throws JSONParseException
55
     *
56
     * @return string Valid JSON representation of $input
57
     */
58
    public static function stringify($args/* inherit from json_encode */)
0 ignored issues
show
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...
59
    {
60
        // extract arguments
61
        $args = func_get_args();
62
63
        // run encode and output
64
        $string = call_user_func_array('json_encode', $args);
65
66
        // turn errors into exceptions for easier catching
67
        if ($error = self::getJsonLastErrorMsg()) {
68
            throw new JSONParseException($error);
69
        }
70
71
        // output
72
        return $string;
73
    }
74
75
    /**
76
     * Get Json Last Error.
77
     *
78
     * @see http://php.net/manual/en/function.json-last-error.php
79
     * @see http://php.net/manual/en/function.json-last-error-msg.php
80
     * @see https://github.com/php/php-src/blob/master/ext/json/json.c#L308
81
     *
82
     * @return string
83
     */
84
    private static function getJsonLastErrorMsg()
85
    {
86
        return JSON_ERROR_NONE !== json_last_error() ? json_last_error_msg() : false;
87
    }
88
}
89