Skip to content

Commit 63c96c1

Browse files
committed
drop old PHP workarounds
1 parent 8cb9a3e commit 63c96c1

2 files changed

Lines changed: 5 additions & 39 deletions

File tree

src/Github/Helpers.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@
1515
*/
1616
class Helpers
1717
{
18-
private static $jsonMessages = [
19-
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
20-
JSON_ERROR_STATE_MISMATCH => 'Syntax error, malformed JSON',
21-
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
22-
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
23-
JSON_ERROR_UTF8 => 'Invalid UTF-8 sequence',
24-
];
25-
26-
2718
/** @var Http\IClient */
2819
private static $client;
2920

@@ -36,28 +27,10 @@ class Helpers
3627
*/
3728
public static function jsonEncode($value)
3829
{
39-
if (PHP_VERSION_ID < 50500) {
40-
set_error_handler(function($severity, $message) { // needed to receive 'recursion detected' error
41-
restore_error_handler();
42-
throw new JsonException($message);
43-
});
44-
}
45-
4630
$json = json_encode($value, JSON_UNESCAPED_UNICODE);
47-
48-
if (PHP_VERSION_ID < 50500) {
49-
restore_error_handler();
50-
}
51-
5231
if ($error = json_last_error()) {
53-
$message = isset(static::$jsonMessages[$error])
54-
? static::$jsonMessages[$error]
55-
: (PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error');
56-
57-
throw new JsonException($message, $error);
32+
throw new JsonException(json_last_error_msg(), $error);
5833
}
59-
60-
$json = str_replace(array("\xe2\x80\xa8", "\xe2\x80\xa9"), array('\u2028', '\u2029'), $json);
6134
return $json;
6235
}
6336

@@ -70,16 +43,9 @@ public static function jsonEncode($value)
7043
*/
7144
public static function jsonDecode($json)
7245
{
73-
$json = (string) $json;
74-
if (!preg_match('##u', $json)) {
75-
throw new JsonException('Invalid UTF-8 sequence', 5); // PECL JSON-C
76-
}
77-
78-
$value = json_decode($json, false, 512, (defined('JSON_C_VERSION') && PHP_INT_SIZE > 4) ? 0 : JSON_BIGINT_AS_STRING);
79-
80-
if ($value === null && $json !== '' && strcasecmp($json, 'null')) { // '' does not clear json_last_error()
81-
$error = json_last_error();
82-
throw new JsonException(isset(static::$jsonMessages[$error]) ? static::$jsonMessages[$error] : 'Unknown error', $error);
46+
$value = json_decode((string) $json, false, 512, JSON_BIGINT_AS_STRING);
47+
if ($error = json_last_error()) {
48+
throw new JsonException(json_last_error_msg(), $error);
8349
}
8450
return $value;
8551
}

tests/Github/OAuth/Login.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class LoginTestCase extends Tester\TestCase
141141
}, Milo\Github\OAuth\LoginException::class, 'Bad JSON in response.');
142142
$e = Assert::exception(function() use ($e) {
143143
throw $e->getPrevious();
144-
}, Milo\Github\JsonException::class, 'Syntax error, malformed JSON');
144+
}, Milo\Github\JsonException::class, 'Syntax error');
145145
Assert::null($e->getPrevious());
146146
}
147147

0 commit comments

Comments
 (0)