1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper { |
16
|
|
|
use Exception; |
17
|
|
|
use JsonException; |
18
|
|
|
use function class_exists; |
19
|
|
|
use function error_clear_last; |
20
|
|
|
use function json_decode as original_json_decode; |
21
|
|
|
use function json_encode as original_json_encode; |
22
|
|
|
use function json_last_error; |
23
|
|
|
use function json_last_error_msg as original_json_last_error_msg; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws JsonException |
27
|
|
|
* |
28
|
|
|
* @return object|array |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
function json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
error_clear_last(); |
33
|
|
|
|
34
|
|
|
$result = original_json_decode($json, $assoc, $depth, $options); |
35
|
|
|
|
36
|
|
|
if (null === $result) { |
37
|
|
|
throw create_json_exception(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
function json_encode($value, int $options = 0, int $depth = 512): string |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
error_clear_last(); |
46
|
|
|
|
47
|
|
|
$result = original_json_encode($value, $options, $depth); |
48
|
|
|
|
49
|
|
|
if (false === $result) { |
50
|
|
|
throw create_json_exception(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @internal |
58
|
|
|
*/ |
59
|
|
|
function create_json_exception(): JsonException |
60
|
|
|
{ |
61
|
|
|
return new JsonException(original_json_last_error_msg(), json_last_error()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
namespace { |
66
|
|
|
if (false === class_exists(JsonException::class, false)) { |
67
|
|
|
class JsonException extends Exception |
68
|
|
|
{ |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.