1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 16:22:43 +01:00

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
This commit is contained in:
Joshua Spence 2015-05-05 20:20:11 +10:00
parent 05e745d9b8
commit 70c8649142
34 changed files with 144 additions and 89 deletions

View file

@ -53,7 +53,7 @@ foreach ($sheets as $name => $sheet) {
if (!$args->getArg('force')) { if (!$args->getArg('force')) {
if (Filesystem::pathExists($manifest_path)) { if (Filesystem::pathExists($manifest_path)) {
$data = Filesystem::readFile($manifest_path); $data = Filesystem::readFile($manifest_path);
$data = json_decode($data, true); $data = phutil_json_decode($data);
if (!$sheet->needsRegeneration($data)) { if (!$sheet->needsRegeneration($data)) {
continue; continue;
} }

View file

@ -647,12 +647,15 @@ final class PhabricatorConduitAPIController
// entire param dictionary JSON encoded. // entire param dictionary JSON encoded.
$params_json = $request->getStr('params'); $params_json = $request->getStr('params');
if (strlen($params_json)) { if (strlen($params_json)) {
$params = json_decode($params_json, true); $params = null;
if (!is_array($params)) { try {
throw new Exception( $params = phutil_json_decode($params_json);
"Invalid parameter information was passed to method ". } catch (PhutilJSONParserException $ex) {
"'{$method}', could not decode JSON serialization. Data: ". throw new PhutilProxyException(
$params_json); pht(
"Invalid parameter information was passed to method '%s'",
$method),
$ex);
} }
$metadata = idx($params, '__conduit__', array()); $metadata = idx($params, '__conduit__', array());

View file

@ -26,13 +26,17 @@ final class ConduitSSHWorkflow extends PhabricatorSSHWorkflow {
$method = head($methodv); $method = head($methodv);
$json = $this->readAllInput(); $json = $this->readAllInput();
$raw_params = json_decode($json, true); $raw_params = null;
if (!is_array($raw_params)) { try {
throw new Exception('Invalid JSON input.'); $raw_params = phutil_json_decode($json);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Invalid JSON input.'),
$ex);
} }
$params = idx($raw_params, 'params', '[]'); $params = idx($raw_params, 'params', '[]');
$params = json_decode($params, true); $params = phutil_json_decode($params);
$metadata = idx($params, '__conduit__', array()); $metadata = idx($params, '__conduit__', array());
unset($params['__conduit__']); unset($params['__conduit__']);

View file

@ -33,9 +33,9 @@ final class DarkConsoleDataController extends PhabricatorController {
return new Aphront400Response(); return new Aphront400Response();
} }
$result = json_decode($result, true); try {
$result = phutil_json_decode($result);
if (!is_array($result)) { } catch (PhutilJSONParserException $ex) {
return new Aphront400Response(); return new Aphront400Response();
} }

View file

@ -17,9 +17,12 @@ final class DifferentialParseRenderTestCase extends PhabricatorTestCase {
$opt_file = $dir.$file.'.options'; $opt_file = $dir.$file.'.options';
if (Filesystem::pathExists($opt_file)) { if (Filesystem::pathExists($opt_file)) {
$options = Filesystem::readFile($opt_file); $options = Filesystem::readFile($opt_file);
$options = json_decode($options, true); try {
if (!is_array($options)) { $options = phutil_json_decode($options);
throw new Exception("Invalid options file: {$opt_file}."); } catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Invalid options file: %s.', $opt_file),
$ex);
} }
} else { } else {
$options = array(); $options = array();

View file

@ -86,7 +86,7 @@ final class DifferentialSetDiffPropertyConduitAPIMethod
protected function execute(ConduitAPIRequest $request) { protected function execute(ConduitAPIRequest $request) {
$diff_id = $request->getValue('diff_id'); $diff_id = $request->getValue('diff_id');
$name = $request->getValue('name'); $name = $request->getValue('name');
$data = json_decode($request->getValue('data'), true); $data = phutil_json_decode($request->getValue('data'));
self::updateDiffProperty($diff_id, $name, $data); self::updateDiffProperty($diff_id, $name, $data);

View file

@ -154,9 +154,11 @@ final class DiffusionLintSaveRunner {
$files); $files);
foreach (new LinesOfALargeExecFuture($future) as $json) { foreach (new LinesOfALargeExecFuture($future) as $json) {
$paths = json_decode($json, true); $paths = null;
if (!is_array($paths)) { try {
fprintf(STDERR, "Invalid JSON: {$json}\n"); $paths = phutil_json_decode($json);
} catch (PhutilJSONParserException $ex) {
fprintf(STDERR, pht("Invalid JSON: %s\n", $json));
continue; continue;
} }

View file

@ -11,8 +11,9 @@ final class DiffusionLastModifiedController extends DiffusionController {
$viewer = $request->getUser(); $viewer = $request->getUser();
$paths = $request->getStr('paths'); $paths = $request->getStr('paths');
$paths = json_decode($paths, true); try {
if (!is_array($paths)) { $paths = phutil_json_decode($paths);
} catch (PhutilJSONParserException $ex) {
return new Aphront400Response(); return new Aphront400Response();
} }

View file

@ -9,7 +9,7 @@ final class DiffusionMercurialWireSSHTestCase extends PhabricatorTestCase {
$raw = Filesystem::readFile($data.$file); $raw = Filesystem::readFile($data.$file);
$raw = explode("\n~~~~~~~~~~\n", $raw, 2); $raw = explode("\n~~~~~~~~~~\n", $raw, 2);
$this->assertEqual(2, count($raw)); $this->assertEqual(2, count($raw));
$expect = json_decode($raw[1], true); $expect = phutil_json_decode($raw[1]);
$this->assertTrue(is_array($expect), $file); $this->assertTrue(is_array($expect), $file);
$this->assertParserResult($expect, $raw[0], $file); $this->assertParserResult($expect, $raw[0], $file);

View file

@ -7,8 +7,9 @@ final class DoorkeeperTagsController extends PhabricatorController {
$viewer = $request->getUser(); $viewer = $request->getUser();
$tags = $request->getStr('tags'); $tags = $request->getStr('tags');
$tags = json_decode($tags, true); try {
if (!is_array($tags)) { $tags = phutil_json_decode($tags);
} catch (PhutilJSONParserException $ex) {
$tags = array(); $tags = array();
} }

View file

@ -12,8 +12,9 @@ final class PhabricatorHelpKeyboardShortcutController
$user = $request->getUser(); $user = $request->getUser();
$keys = $request->getStr('keys'); $keys = $request->getStr('keys');
$keys = json_decode($keys, true); try {
if (!is_array($keys)) { $keys = phutil_json_decode($keys);
} catch (PhutilJSONParserException $ex) {
return new Aphront400Response(); return new Aphront400Response();
} }

View file

@ -633,14 +633,16 @@ abstract class HeraldAdapter {
// dictionary. The first regexp must match the dictionary key, and the // dictionary. The first regexp must match the dictionary key, and the
// second regexp must match the dictionary value. If any key/value pair // second regexp must match the dictionary value. If any key/value pair
// in the dictionary matches both regexps, the condition is satisfied. // in the dictionary matches both regexps, the condition is satisfied.
$regexp_pair = json_decode($condition_value, true); $regexp_pair = null;
if (!is_array($regexp_pair)) { try {
$regexp_pair = phutil_json_decode($condition_value);
} catch (PhutilJSONParserException $ex) {
throw new HeraldInvalidConditionException( throw new HeraldInvalidConditionException(
'Regular expression pair is not valid JSON!'); pht('Regular expression pair is not valid JSON!'));
} }
if (count($regexp_pair) != 2) { if (count($regexp_pair) != 2) {
throw new HeraldInvalidConditionException( throw new HeraldInvalidConditionException(
'Regular expression pair is not a pair!'); pht('Regular expression pair is not a pair!'));
} }
$key_regexp = array_shift($regexp_pair); $key_regexp = array_shift($regexp_pair);
@ -705,8 +707,10 @@ abstract class HeraldAdapter {
} }
break; break;
case self::CONDITION_REGEXP_PAIR: case self::CONDITION_REGEXP_PAIR:
$json = json_decode($condition_value, true); $json = null;
if (!is_array($json)) { try {
$json = phutil_json_decode($condition_value);
} catch (PhutilJSONParserException $ex) {
throw new HeraldInvalidConditionException( throw new HeraldInvalidConditionException(
pht( pht(
'The regular expression pair "%s" is not valid JSON. Enter a '. 'The regular expression pair "%s" is not valid JSON. Enter a '.

View file

@ -258,7 +258,15 @@ final class HeraldRuleController extends HeraldController {
$errors[] = pht('Rule must have a name.'); $errors[] = pht('Rule must have a name.');
} }
$data = json_decode($request->getStr('rule'), true); $data = null;
try {
$data = phutil_json_decode($request->getStr('rule'));
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to decode rule data.'),
$ex);
}
if (!is_array($data) || if (!is_array($data) ||
!$data['conditions'] || !$data['conditions'] ||
!$data['actions']) { !$data['actions']) {

View file

@ -47,7 +47,7 @@ final class ManiphestBatchEditController extends ManiphestController {
$actions = $request->getStr('actions'); $actions = $request->getStr('actions');
if ($actions) { if ($actions) {
$actions = json_decode($actions, true); $actions = phutil_json_decode($actions);
} }
if ($request->isFormPost() && is_array($actions)) { if ($request->isFormPost() && is_array($actions)) {

View file

@ -74,7 +74,7 @@ final class ManiphestTransactionPreviewController extends ManiphestController {
break; break;
case PhabricatorTransactions::TYPE_EDGE: case PhabricatorTransactions::TYPE_EDGE:
if ($value) { if ($value) {
$value = json_decode($value); $value = phutil_json_decode($value);
} }
if (!$value) { if (!$value) {
$value = array(); $value = array();

View file

@ -120,9 +120,13 @@ final class PhabricatorMailImplementationMailgunAdapter
list($body) = $future->resolvex(); list($body) = $future->resolvex();
$response = json_decode($body, true); $response = null;
if (!is_array($response)) { try {
throw new Exception("Failed to JSON decode response: {$body}"); $response = phutil_json_decode($body);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to JSON decode response.'),
$ex);
} }
if (!idx($response, 'id')) { if (!idx($response, 'id')) {

View file

@ -78,8 +78,10 @@ final class PhabricatorMailImplementationSendGridAdapter
if (!$user || !$key) { if (!$user || !$key) {
throw new Exception( throw new Exception(
"Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use ". pht(
"SendGrid for mail delivery."); "Configure '%s' and '%s' to use SendGrid for mail delivery.",
'sendgrid.api-user',
'sendgrid.api-key'));
} }
$params = array(); $params = array();
@ -142,14 +144,18 @@ final class PhabricatorMailImplementationSendGridAdapter
list($body) = $future->resolvex(); list($body) = $future->resolvex();
$response = json_decode($body, true); $response = null;
if (!is_array($response)) { try {
throw new Exception("Failed to JSON decode response: {$body}"); $response = phutil_json_decode($body);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to JSON decode response.'),
$ex);
} }
if ($response['message'] !== 'success') { if ($response['message'] !== 'success') {
$errors = implode(';', $response['errors']); $errors = implode(';', $response['errors']);
throw new Exception("Request failed with errors: {$errors}."); throw new Exception(pht('Request failed with errors: %s.', $errors));
} }
return true; return true;

View file

@ -32,7 +32,7 @@ final class PhabricatorContentSource {
} }
public static function newFromSerialized($serialized) { public static function newFromSerialized($serialized) {
$dict = json_decode($serialized, true); $dict = phutil_json_decode($serialized);
if (!is_array($dict)) { if (!is_array($dict)) {
$dict = array(); $dict = array();
} }

View file

@ -93,15 +93,18 @@ final class PhameSkinSpecification {
} }
private static function loadSkinSpecification($path) { private static function loadSkinSpecification($path) {
$config_path = $path.DIRECTORY_SEPARATOR.'skin.json'; $config_path = $path.DIRECTORY_SEPARATOR.'skin.json';
$config = array(); $config = array();
if (Filesystem::pathExists($config_path)) { if (Filesystem::pathExists($config_path)) {
$config = Filesystem::readFile($config_path); $config = Filesystem::readFile($config_path);
$config = json_decode($config, true); try {
if (!is_array($config)) { $config = phutil_json_decode($config);
throw new Exception( } catch (PhutilJSONParserException $ex) {
"Skin configuration file '{$config_path}' is not a valid JSON file."); throw new PhutilProxyException(
pht(
"Skin configuration file '%s' is not a valid JSON file.",
$config_path),
$ex);
} }
$type = idx($config, 'type', self::TYPE_BASIC); $type = idx($config, 'type', self::TYPE_BASIC);
} else { } else {

View file

@ -94,20 +94,23 @@ final class PhortunePaymentMethodCreateController
if (!$errors) { if (!$errors) {
$client_token_raw = $request->getStr('token'); $client_token_raw = $request->getStr('token');
$client_token = json_decode($client_token_raw, true); $client_token = null;
if (!is_array($client_token)) { try {
$client_token = phutil_json_decode($client_token_raw);
} catch (PhutilJSONParserException $ex) {
$errors[] = pht( $errors[] = pht(
'There was an error decoding token information submitted by the '. 'There was an error decoding token information submitted by the '.
'client. Expected a JSON-encoded token dictionary, received: %s.', 'client. Expected a JSON-encoded token dictionary, received: %s.',
nonempty($client_token_raw, pht('nothing'))); nonempty($client_token_raw, pht('nothing')));
} else { }
if (!$provider->validateCreatePaymentMethodToken($client_token)) { if (!$provider->validateCreatePaymentMethodToken($client_token)) {
$errors[] = pht( $errors[] = pht(
'There was an error with the payment token submitted by the '. 'There was an error with the payment token submitted by the '.
'client. Expected a valid dictionary, received: %s.', 'client. Expected a valid dictionary, received: %s.',
$client_token_raw); $client_token_raw);
} }
}
if (!$errors) { if (!$errors) {
$errors = $provider->createPaymentMethodFromRequest( $errors = $provider->createPaymentMethodFromRequest(
$request, $request,
@ -215,8 +218,10 @@ final class PhortunePaymentMethodCreateController
$errors = array(); $errors = array();
$client_errors = json_decode($client_errors_raw, true); $client_errors = null;
if (!is_array($client_errors)) { try {
$client_errors = phutil_json_decode($client_errors_raw);
} catch (PhutilJSONParserException $ex) {
$errors[] = pht( $errors[] = pht(
'There was an error decoding error information submitted by the '. 'There was an error decoding error information submitted by the '.
'client. Expected a JSON-encoded list of error codes, received: %s.', 'client. Expected a JSON-encoded list of error codes, received: %s.',

View file

@ -52,9 +52,12 @@ final class PhabricatorPolicyEditController
$errors = array(); $errors = array();
if ($request->isFormPost()) { if ($request->isFormPost()) {
$data = $request->getStr('rules'); $data = $request->getStr('rules');
$data = @json_decode($data, true); try {
if (!is_array($data)) { $data = phutil_json_decode($data);
throw new Exception('Failed to JSON decode rule data!'); } catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to JSON decode rule data!'),
$ex);
} }
$rule_data = array(); $rule_data = array();

View file

@ -39,7 +39,7 @@ final class DifferentialReleephRequestFieldSpecification {
public function setValueFromStorage($json) { public function setValueFromStorage($json) {
if ($json) { if ($json) {
$dict = json_decode($json, true); $dict = phutil_json_decode($json);
$this->releephAction = idx($dict, 'releephAction'); $this->releephAction = idx($dict, 'releephAction');
$this->releephPHIDs = idx($dict, 'releephPHIDs'); $this->releephPHIDs = idx($dict, 'releephPHIDs');
} }

View file

@ -397,12 +397,13 @@ final class PhabricatorElasticSearchEngine extends PhabricatorSearchEngine {
return null; return null;
} }
$body = json_decode($body, true); try {
if (!is_array($body)) { return phutil_json_decode($body);
throw new Exception('elasticsearch server returned invalid JSON!'); } catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('ElasticSearch server returned invalid JSON!'),
$ex);
} }
return $body;
} }
} }

View file

@ -21,9 +21,12 @@ final class PhabricatorXHProfProfileController
} }
$data = $file->loadFileData(); $data = $file->loadFileData();
$data = @json_decode($data, true); try {
if (!$data) { $data = phutil_json_decode($data);
throw new Exception('Failed to unserialize XHProf profile!'); } catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to unserialize XHProf profile!'),
$ex);
} }
$symbol = $request->getStr('symbol'); $symbol = $request->getStr('symbol');

View file

@ -11,7 +11,7 @@ final class PhabricatorCustomFieldConfigOptionType
$errors = array(); $errors = array();
$storage_value = $request->getStr('value'); $storage_value = $request->getStr('value');
$in_value = json_decode($storage_value, true); $in_value = phutil_json_decode($storage_value);
if (!is_array($in_value)) { if (!is_array($in_value)) {
$in_value = array(); $in_value = array();
} }

View file

@ -124,7 +124,7 @@ abstract class PhabricatorStandardCustomFieldPHIDs
$old = array(); $old = array();
} }
$new = json_decode($xaction->getNewValue()); $new = phutil_json_decode($xaction->getNewValue());
if (!is_array($new)) { if (!is_array($new)) {
$new = array(); $new = array();
} }

View file

@ -23,9 +23,12 @@ final class PhabricatorBot extends PhabricatorDaemon {
} }
$json_raw = Filesystem::readFile($argv[0]); $json_raw = Filesystem::readFile($argv[0]);
$config = json_decode($json_raw, true); try {
if (!is_array($config)) { $config = phutil_json_decode($json_raw);
throw new Exception("File '{$argv[0]}' is not valid JSON!"); } catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht("File '%s' is not valid JSON!", $argv[0]),
$ex);
} }
$nick = idx($config, 'nick', 'phabot'); $nick = idx($config, 'nick', 'phabot');

View file

@ -124,7 +124,7 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
$message = substr($buffer, 0, $until + 1); $message = substr($buffer, 0, $until + 1);
$buffer = substr($buffer, $until + 2); $buffer = substr($buffer, $until + 2);
$m_obj = json_decode($message, true); $m_obj = phutil_json_decode($message);
if ($message = $this->processMessage($m_obj)) { if ($message = $this->processMessage($m_obj)) {
return $message; return $message;
} }
@ -149,7 +149,7 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
$output = trim($output); $output = trim($output);
if (strlen($output)) { if (strlen($output)) {
return json_decode($output, true); return phutil_json_decode($output);
} }
return true; return true;
@ -165,6 +165,6 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
abstract protected function buildStreamingUrl($channel); abstract protected function buildStreamingUrl($channel);
abstract protected function processMessage($raw_object); abstract protected function processMessage(array $raw_object);
} }

View file

@ -26,7 +26,7 @@ final class PhabricatorBotFlowdockProtocolAdapter
return $url; return $url;
} }
protected function processMessage($m_obj) { protected function processMessage(array $m_obj) {
$command = null; $command = null;
switch ($m_obj['event']) { switch ($m_obj['event']) {
case 'message': case 'message':

View file

@ -16,7 +16,7 @@ final class PhabricatorCampfireProtocolAdapter
return $url; return $url;
} }
protected function processMessage($m_obj) { protected function processMessage(array $m_obj) {
$command = null; $command = null;
switch ($m_obj['type']) { switch ($m_obj['type']) {
case 'TextMessage': case 'TextMessage':

View file

@ -191,7 +191,7 @@ final class PhabricatorWorkerLeaseQuery extends PhabricatorQuery {
foreach ($data as $row) { foreach ($data as $row) {
$tasks[$row['id']]->setServerTime($row['_serverTime']); $tasks[$row['id']]->setServerTime($row['_serverTime']);
if ($row['_taskData']) { if ($row['_taskData']) {
$task_data = json_decode($row['_taskData'], true); $task_data = phutil_json_decode($row['_taskData']);
} else { } else {
$task_data = null; $task_data = null;
} }

View file

@ -201,7 +201,7 @@ final class PhabricatorEdgeQuery extends PhabricatorQuery {
$data_ids); $data_ids);
foreach ($data_rows as $row) { foreach ($data_rows as $row) {
$data_map[$row['id']] = idx( $data_map[$row['id']] = idx(
json_decode($row['data'], true), phutil_json_decode($row['data']),
'data'); 'data');
} }
} }

View file

@ -1637,7 +1637,7 @@ abstract class LiskDAO {
break; break;
case self::SERIALIZATION_JSON: case self::SERIALIZATION_JSON:
if ($deserialize) { if ($deserialize) {
$data[$col] = json_decode($data[$col], true); $data[$col] = phutil_json_decode($data[$col]);
} else { } else {
$data[$col] = json_encode($data[$col]); $data[$col] = json_encode($data[$col]);
} }

View file

@ -106,7 +106,7 @@ final class PHUIIconView extends AphrontTagView {
$root = dirname(phutil_get_library_root('phabricator')); $root = dirname(phutil_get_library_root('phabricator'));
$path = $root.'/resources/sprite/manifest/'.$sheet.'.json'; $path = $root.'/resources/sprite/manifest/'.$sheet.'.json';
$data = Filesystem::readFile($path); $data = Filesystem::readFile($path);
return idx(json_decode($data, true), 'sprites'); return idx(phutil_json_decode($data), 'sprites');
} }
public static function getFontIcons() { public static function getFontIcons() {