Skip to content

Commit 70c8649

Browse files
committedMay 5, 2015
Use phutil_json_decode instead of json_decode
Summary: Generally, `phutil_json_decode` should be preferred over `json_decode`. Test Plan: Eyellballed. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: Korvin, epriestley Differential Revision: https://secure.phabricator.com/D12680
1 parent 05e745d commit 70c8649

File tree

34 files changed

+144
-89
lines changed

34 files changed

+144
-89
lines changed
 

‎scripts/celerity/generate_sprites.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
if (!$args->getArg('force')) {
5454
if (Filesystem::pathExists($manifest_path)) {
5555
$data = Filesystem::readFile($manifest_path);
56-
$data = json_decode($data, true);
56+
$data = phutil_json_decode($data);
5757
if (!$sheet->needsRegeneration($data)) {
5858
continue;
5959
}

‎src/applications/conduit/controller/PhabricatorConduitAPIController.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,15 @@ private function decodeConduitParams(
647647
// entire param dictionary JSON encoded.
648648
$params_json = $request->getStr('params');
649649
if (strlen($params_json)) {
650-
$params = json_decode($params_json, true);
651-
if (!is_array($params)) {
652-
throw new Exception(
653-
"Invalid parameter information was passed to method ".
654-
"'{$method}', could not decode JSON serialization. Data: ".
655-
$params_json);
650+
$params = null;
651+
try {
652+
$params = phutil_json_decode($params_json);
653+
} catch (PhutilJSONParserException $ex) {
654+
throw new PhutilProxyException(
655+
pht(
656+
"Invalid parameter information was passed to method '%s'",
657+
$method),
658+
$ex);
656659
}
657660

658661
$metadata = idx($params, '__conduit__', array());

‎src/applications/conduit/ssh/ConduitSSHWorkflow.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ public function execute(PhutilArgumentParser $args) {
2626
$method = head($methodv);
2727

2828
$json = $this->readAllInput();
29-
$raw_params = json_decode($json, true);
30-
if (!is_array($raw_params)) {
31-
throw new Exception('Invalid JSON input.');
29+
$raw_params = null;
30+
try {
31+
$raw_params = phutil_json_decode($json);
32+
} catch (PhutilJSONParserException $ex) {
33+
throw new PhutilProxyException(
34+
pht('Invalid JSON input.'),
35+
$ex);
3236
}
3337

3438
$params = idx($raw_params, 'params', '[]');
35-
$params = json_decode($params, true);
39+
$params = phutil_json_decode($params);
3640
$metadata = idx($params, '__conduit__', array());
3741
unset($params['__conduit__']);
3842

‎src/applications/console/controller/DarkConsoleDataController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function processRequest() {
3333
return new Aphront400Response();
3434
}
3535

36-
$result = json_decode($result, true);
37-
38-
if (!is_array($result)) {
36+
try {
37+
$result = phutil_json_decode($result);
38+
} catch (PhutilJSONParserException $ex) {
3939
return new Aphront400Response();
4040
}
4141

‎src/applications/differential/__tests__/DifferentialParseRenderTestCase.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public function testParseRender() {
1717
$opt_file = $dir.$file.'.options';
1818
if (Filesystem::pathExists($opt_file)) {
1919
$options = Filesystem::readFile($opt_file);
20-
$options = json_decode($options, true);
21-
if (!is_array($options)) {
22-
throw new Exception("Invalid options file: {$opt_file}.");
20+
try {
21+
$options = phutil_json_decode($options);
22+
} catch (PhutilJSONParserException $ex) {
23+
throw new PhutilProxyException(
24+
pht('Invalid options file: %s.', $opt_file),
25+
$ex);
2326
}
2427
} else {
2528
$options = array();

‎src/applications/differential/conduit/DifferentialSetDiffPropertyConduitAPIMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static function updateLintStatus($diff_id) {
8686
protected function execute(ConduitAPIRequest $request) {
8787
$diff_id = $request->getValue('diff_id');
8888
$name = $request->getValue('name');
89-
$data = json_decode($request->getValue('data'), true);
89+
$data = phutil_json_decode($request->getValue('data'));
9090

9191
self::updateDiffProperty($diff_id, $name, $data);
9292

‎src/applications/diffusion/DiffusionLintSaveRunner.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ private function runArcLint(array $files) {
154154
$files);
155155

156156
foreach (new LinesOfALargeExecFuture($future) as $json) {
157-
$paths = json_decode($json, true);
158-
if (!is_array($paths)) {
159-
fprintf(STDERR, "Invalid JSON: {$json}\n");
157+
$paths = null;
158+
try {
159+
$paths = phutil_json_decode($json);
160+
} catch (PhutilJSONParserException $ex) {
161+
fprintf(STDERR, pht("Invalid JSON: %s\n", $json));
160162
continue;
161163
}
162164

‎src/applications/diffusion/controller/DiffusionLastModifiedController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ protected function processDiffusionRequest(AphrontRequest $request) {
1111
$viewer = $request->getUser();
1212

1313
$paths = $request->getStr('paths');
14-
$paths = json_decode($paths, true);
15-
if (!is_array($paths)) {
14+
try {
15+
$paths = phutil_json_decode($paths);
16+
} catch (PhutilJSONParserException $ex) {
1617
return new Aphront400Response();
1718
}
1819

‎src/applications/diffusion/ssh/__tests__/DiffusionMercurialWireSSHTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function testMercurialClientWireProtocolParser() {
99
$raw = Filesystem::readFile($data.$file);
1010
$raw = explode("\n~~~~~~~~~~\n", $raw, 2);
1111
$this->assertEqual(2, count($raw));
12-
$expect = json_decode($raw[1], true);
12+
$expect = phutil_json_decode($raw[1]);
1313
$this->assertTrue(is_array($expect), $file);
1414

1515
$this->assertParserResult($expect, $raw[0], $file);

‎src/applications/doorkeeper/controller/DoorkeeperTagsController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ public function processRequest() {
77
$viewer = $request->getUser();
88

99
$tags = $request->getStr('tags');
10-
$tags = json_decode($tags, true);
11-
if (!is_array($tags)) {
10+
try {
11+
$tags = phutil_json_decode($tags);
12+
} catch (PhutilJSONParserException $ex) {
1213
$tags = array();
1314
}
1415

‎src/applications/help/controller/PhabricatorHelpKeyboardShortcutController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public function processRequest() {
1212
$user = $request->getUser();
1313

1414
$keys = $request->getStr('keys');
15-
$keys = json_decode($keys, true);
16-
if (!is_array($keys)) {
15+
try {
16+
$keys = phutil_json_decode($keys);
17+
} catch (PhutilJSONParserException $ex) {
1718
return new Aphront400Response();
1819
}
1920

‎src/applications/herald/adapter/HeraldAdapter.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,16 @@ public function doesConditionMatch(
633633
// dictionary. The first regexp must match the dictionary key, and the
634634
// second regexp must match the dictionary value. If any key/value pair
635635
// in the dictionary matches both regexps, the condition is satisfied.
636-
$regexp_pair = json_decode($condition_value, true);
637-
if (!is_array($regexp_pair)) {
636+
$regexp_pair = null;
637+
try {
638+
$regexp_pair = phutil_json_decode($condition_value);
639+
} catch (PhutilJSONParserException $ex) {
638640
throw new HeraldInvalidConditionException(
639-
'Regular expression pair is not valid JSON!');
641+
pht('Regular expression pair is not valid JSON!'));
640642
}
641643
if (count($regexp_pair) != 2) {
642644
throw new HeraldInvalidConditionException(
643-
'Regular expression pair is not a pair!');
645+
pht('Regular expression pair is not a pair!'));
644646
}
645647

646648
$key_regexp = array_shift($regexp_pair);
@@ -705,8 +707,10 @@ public function willSaveCondition(HeraldCondition $condition) {
705707
}
706708
break;
707709
case self::CONDITION_REGEXP_PAIR:
708-
$json = json_decode($condition_value, true);
709-
if (!is_array($json)) {
710+
$json = null;
711+
try {
712+
$json = phutil_json_decode($condition_value);
713+
} catch (PhutilJSONParserException $ex) {
710714
throw new HeraldInvalidConditionException(
711715
pht(
712716
'The regular expression pair "%s" is not valid JSON. Enter a '.

‎src/applications/herald/controller/HeraldRuleController.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,15 @@ private function saveRule(HeraldAdapter $adapter, $rule, $request) {
258258
$errors[] = pht('Rule must have a name.');
259259
}
260260

261-
$data = json_decode($request->getStr('rule'), true);
261+
$data = null;
262+
try {
263+
$data = phutil_json_decode($request->getStr('rule'));
264+
} catch (PhutilJSONParserException $ex) {
265+
throw new PhutilProxyException(
266+
pht('Failed to decode rule data.'),
267+
$ex);
268+
}
269+
262270
if (!is_array($data) ||
263271
!$data['conditions'] ||
264272
!$data['actions']) {

‎src/applications/maniphest/controller/ManiphestBatchEditController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function handleRequest(AphrontRequest $request) {
4747

4848
$actions = $request->getStr('actions');
4949
if ($actions) {
50-
$actions = json_decode($actions, true);
50+
$actions = phutil_json_decode($actions);
5151
}
5252

5353
if ($request->isFormPost() && is_array($actions)) {

‎src/applications/maniphest/controller/ManiphestTransactionPreviewController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function processRequest() {
7474
break;
7575
case PhabricatorTransactions::TYPE_EDGE:
7676
if ($value) {
77-
$value = json_decode($value);
77+
$value = phutil_json_decode($value);
7878
}
7979
if (!$value) {
8080
$value = array();

‎src/applications/metamta/adapter/PhabricatorMailImplementationMailgunAdapter.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ public function send() {
120120

121121
list($body) = $future->resolvex();
122122

123-
$response = json_decode($body, true);
124-
if (!is_array($response)) {
125-
throw new Exception("Failed to JSON decode response: {$body}");
123+
$response = null;
124+
try {
125+
$response = phutil_json_decode($body);
126+
} catch (PhutilJSONParserException $ex) {
127+
throw new PhutilProxyException(
128+
pht('Failed to JSON decode response.'),
129+
$ex);
126130
}
127131

128132
if (!idx($response, 'id')) {

‎src/applications/metamta/adapter/PhabricatorMailImplementationSendGridAdapter.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ public function send() {
7878

7979
if (!$user || !$key) {
8080
throw new Exception(
81-
"Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use ".
82-
"SendGrid for mail delivery.");
81+
pht(
82+
"Configure '%s' and '%s' to use SendGrid for mail delivery.",
83+
'sendgrid.api-user',
84+
'sendgrid.api-key'));
8385
}
8486

8587
$params = array();
@@ -142,14 +144,18 @@ public function send() {
142144

143145
list($body) = $future->resolvex();
144146

145-
$response = json_decode($body, true);
146-
if (!is_array($response)) {
147-
throw new Exception("Failed to JSON decode response: {$body}");
147+
$response = null;
148+
try {
149+
$response = phutil_json_decode($body);
150+
} catch (PhutilJSONParserException $ex) {
151+
throw new PhutilProxyException(
152+
pht('Failed to JSON decode response.'),
153+
$ex);
148154
}
149155

150156
if ($response['message'] !== 'success') {
151157
$errors = implode(';', $response['errors']);
152-
throw new Exception("Request failed with errors: {$errors}.");
158+
throw new Exception(pht('Request failed with errors: %s.', $errors));
153159
}
154160

155161
return true;

‎src/applications/metamta/contentsource/PhabricatorContentSource.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function newForSource($source, array $params) {
3232
}
3333

3434
public static function newFromSerialized($serialized) {
35-
$dict = json_decode($serialized, true);
35+
$dict = phutil_json_decode($serialized);
3636
if (!is_array($dict)) {
3737
$dict = array();
3838
}

‎src/applications/phame/skins/PhameSkinSpecification.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,18 @@ public static function loadOneSkinSpecification($name) {
9393
}
9494

9595
private static function loadSkinSpecification($path) {
96-
9796
$config_path = $path.DIRECTORY_SEPARATOR.'skin.json';
9897
$config = array();
9998
if (Filesystem::pathExists($config_path)) {
10099
$config = Filesystem::readFile($config_path);
101-
$config = json_decode($config, true);
102-
if (!is_array($config)) {
103-
throw new Exception(
104-
"Skin configuration file '{$config_path}' is not a valid JSON file.");
100+
try {
101+
$config = phutil_json_decode($config);
102+
} catch (PhutilJSONParserException $ex) {
103+
throw new PhutilProxyException(
104+
pht(
105+
"Skin configuration file '%s' is not a valid JSON file.",
106+
$config_path),
107+
$ex);
105108
}
106109
$type = idx($config, 'type', self::TYPE_BASIC);
107110
} else {

‎src/applications/phortune/controller/PhortunePaymentMethodCreateController.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,23 @@ public function processRequest() {
9494

9595
if (!$errors) {
9696
$client_token_raw = $request->getStr('token');
97-
$client_token = json_decode($client_token_raw, true);
98-
if (!is_array($client_token)) {
97+
$client_token = null;
98+
try {
99+
$client_token = phutil_json_decode($client_token_raw);
100+
} catch (PhutilJSONParserException $ex) {
99101
$errors[] = pht(
100102
'There was an error decoding token information submitted by the '.
101103
'client. Expected a JSON-encoded token dictionary, received: %s.',
102104
nonempty($client_token_raw, pht('nothing')));
103-
} else {
104-
if (!$provider->validateCreatePaymentMethodToken($client_token)) {
105-
$errors[] = pht(
106-
'There was an error with the payment token submitted by the '.
107-
'client. Expected a valid dictionary, received: %s.',
108-
$client_token_raw);
109-
}
110105
}
106+
107+
if (!$provider->validateCreatePaymentMethodToken($client_token)) {
108+
$errors[] = pht(
109+
'There was an error with the payment token submitted by the '.
110+
'client. Expected a valid dictionary, received: %s.',
111+
$client_token_raw);
112+
}
113+
111114
if (!$errors) {
112115
$errors = $provider->createPaymentMethodFromRequest(
113116
$request,
@@ -215,8 +218,10 @@ private function processClientErrors(
215218

216219
$errors = array();
217220

218-
$client_errors = json_decode($client_errors_raw, true);
219-
if (!is_array($client_errors)) {
221+
$client_errors = null;
222+
try {
223+
$client_errors = phutil_json_decode($client_errors_raw);
224+
} catch (PhutilJSONParserException $ex) {
220225
$errors[] = pht(
221226
'There was an error decoding error information submitted by the '.
222227
'client. Expected a JSON-encoded list of error codes, received: %s.',

‎src/applications/policy/controller/PhabricatorPolicyEditController.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ public function processRequest() {
5252
$errors = array();
5353
if ($request->isFormPost()) {
5454
$data = $request->getStr('rules');
55-
$data = @json_decode($data, true);
56-
if (!is_array($data)) {
57-
throw new Exception('Failed to JSON decode rule data!');
55+
try {
56+
$data = phutil_json_decode($data);
57+
} catch (PhutilJSONParserException $ex) {
58+
throw new PhutilProxyException(
59+
pht('Failed to JSON decode rule data!'),
60+
$ex);
5861
}
5962

6063
$rule_data = array();

‎src/applications/releeph/differential/DifferentialReleephRequestFieldSpecification.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getValueForStorage() {
3939

4040
public function setValueFromStorage($json) {
4141
if ($json) {
42-
$dict = json_decode($json, true);
42+
$dict = phutil_json_decode($json);
4343
$this->releephAction = idx($dict, 'releephAction');
4444
$this->releephPHIDs = idx($dict, 'releephPHIDs');
4545
}

‎src/applications/search/engine/PhabricatorElasticSearchEngine.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,13 @@ private function executeRequest($path, array $data, $method = 'GET') {
397397
return null;
398398
}
399399

400-
$body = json_decode($body, true);
401-
if (!is_array($body)) {
402-
throw new Exception('elasticsearch server returned invalid JSON!');
400+
try {
401+
return phutil_json_decode($body);
402+
} catch (PhutilJSONParserException $ex) {
403+
throw new PhutilProxyException(
404+
pht('ElasticSearch server returned invalid JSON!'),
405+
$ex);
403406
}
404-
405-
return $body;
406407
}
407408

408409
}

0 commit comments

Comments
 (0)
Failed to load comments.