Issues (584)

class/Github/Helpers.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace XoopsModules\Wggithub\Github;
4
5
6
/**
7
 * Just helpers.
8
 *
9
 * The JSON encode/decode methods are stolen from Nette Utils (https://github.com/nette/utils).
10
 *
11
 * @author  David Grudl
12
 * @author  Miloslav Hůla (https://github.com/milo)
13
 */
14
class Helpers
15
{
16
    private static $jsonMessages = [
17
        JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
18
        JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON',
19
        JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
20
        JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
21
        JSON_ERROR_UTF8 => 'Invalid UTF-8 sequence',
22
    ];
23
24
25
    /** @var Http\IClient */
26
    private static $client;
27
28
29
    /**
30
     * @param  mixed
31
     * @return string
32
     *
33
     * @throws JsonException
34
     */
35
    public static function jsonEncode($value)
36
    {
37
        if (PHP_VERSION_ID < 50500) {
38
            set_error_handler(function($severity, $message) { // needed to receive 'recursion detected' error
39
                restore_error_handler();
40
                throw new JsonException($message);
41
            });
42
        }
43
44
        $json = \json_encode($value, JSON_UNESCAPED_UNICODE);
45
46
        if (PHP_VERSION_ID < 50500) {
47
            restore_error_handler();
48
        }
49
50
        if ($error = json_last_error()) {
51
            $message = isset(static::$jsonMessages[$error])
0 ignored issues
show
Since $jsonMessages is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $jsonMessages to at least protected.
Loading history...
52
                ? static::$jsonMessages[$error]
53
                : (PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error');
54
55
            throw new JsonException($message, $error);
56
        }
57
58
        $json = \str_replace(["\xe2\x80\xa8", "\xe2\x80\xa9"], ['\u2028', '\u2029'], $json);
59
        return $json;
60
    }
61
62
63
    /**
64
     * @param $json
65
     * @param bool $assoc
66
     * @return string
67
     *
68
     */
69
    public static function jsonDecode($json, $assoc = false)
70
    {
71
        $json = (string) $json;
72
        if (!\preg_match('##u', $json)) {
73
            throw new JsonException('Invalid UTF-8 sequence', 5); // PECL JSON-C
74
        }
75
76
        $value = \json_decode($json, $assoc, 512, (\defined('JSON_C_VERSION') && PHP_INT_SIZE > 4) ? 0 : JSON_BIGINT_AS_STRING);
77
78
        if ($value === NULL && $json !== '' && strcasecmp($json, 'null')) { // '' does not clear json_last_error()
79
            $error = json_last_error();
80
            throw new JsonException(isset(static::$jsonMessages[$error]) ? static::$jsonMessages[$error] : 'Unknown error', $error);
0 ignored issues
show
Since $jsonMessages is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $jsonMessages to at least protected.
Loading history...
81
        }
82
        return $value;
83
    }
84
85
86
    /**
87
     * @param  bool
88
     * @return Http\IClient
89
     */
90
    public static function createDefaultClient($newInstance = FALSE)
91
    {
92
        if (self::$client === NULL || $newInstance) {
93
            self::$client = \extension_loaded('curl')
94
                ? new Http\CurlClient
95
                : new Http\StreamClient;
96
        }
97
98
        return self::$client;
99
    }
100
101
}
102