jenkins-bot has submitted this change and it was merged.
Change subject: FormatJson: minor cleanup
......................................................................
FormatJson: minor cleanup
* Prefer feature detection over version comparison.
* Prefer pre- over post- increment and decrement operators.
* Remove the statement that FormatJson::decode() decodes `true`,
`false`, and `null` case insensitively. Nobody should assume
this is (or is not) the case, even though the PHP manual says so.
* Avoid using the ternary operator with long strings; prior to
PHP 5.4, the operator prevented the copy-on-write optimization.
* Avoid placing comments on the same lines as code.
Change-Id: I8fc88e9b7b49aa0cbd4128216557836a3b2cd011
---
M includes/json/FormatJson.php
1 file changed, 20 insertions(+), 13 deletions(-)
Approvals:
Ori.livneh: Looks good to me, approved
Parent5446: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/includes/json/FormatJson.php b/includes/json/FormatJson.php
index 91e1e87..bc2fff1 100644
--- a/includes/json/FormatJson.php
+++ b/includes/json/FormatJson.php
@@ -90,10 +90,10 @@
* @return string|bool: String if successful; false upon failure
*/
public static function encode( $value, $pretty = false, $escaping = 0 )
{
- if ( version_compare( PHP_VERSION, '5.4.0', '<' ) ) {
- return self::encode53( $value, $pretty, $escaping );
+ if ( defined( 'JSON_UNESCAPED_UNICODE' ) ) {
+ return self::encode54( $value, $pretty, $escaping );
}
- return self::encode54( $value, $pretty, $escaping );
+ return self::encode53( $value, $pretty, $escaping );
}
/**
@@ -103,9 +103,8 @@
* @param bool $assoc When true, returned objects will be converted
into associative arrays.
*
* @return mixed: the value encoded in JSON in appropriate PHP type.
- * Values `"true"`, `"false"`, and `"null"` (case-insensitive) are
returned as `true`, `false`
- * and `null` respectively. `null` is returned if the JSON cannot be
- * decoded or if the encoded data is deeper than the recursion limit.
+ * `null` is returned if the JSON cannot be decoded or if the encoded
data is deeper than
+ * the recursion limit.
*/
public static function decode( $value, $assoc = false ) {
return json_decode( $value, $assoc );
@@ -121,8 +120,8 @@
*/
private static function encode54( $value, $pretty, $escaping ) {
// PHP escapes '/' to prevent breaking out of inline script
blocks using '</script>',
- // which is hardly useful when '<' and '>' are escaped, and
such escaping negatively
- // impacts the human readability of URLs and similar strings.
+ // which is hardly useful when '<' and '>' are escaped (and
inadequate), and such
+ // escaping negatively impacts the human readability of URLs
and similar strings.
$options = JSON_UNESCAPED_SLASHES;
$options |= $pretty ? JSON_PRETTY_PRINT : 0;
$options |= ( $escaping & self::UTF8_OK ) ?
JSON_UNESCAPED_UNICODE : 0;
@@ -152,7 +151,11 @@
if ( $json === false ) {
return false;
}
- $json = str_replace( '\\/', '/', $json ); // emulate
JSON_UNESCAPED_SLASHES
+
+ // Emulate JSON_UNESCAPED_SLASHES. Because the JSON contains no
unescaped slashes
+ // (only escaped slashes), a simple string replacement works
fine.
+ $json = str_replace( '\/', '/', $json );
+
if ( $escaping & self::UTF8_OK ) {
// JSON hex escape sequences follow the format \uDDDD,
where DDDD is four hex digits
// indicating the equivalent UTF-16 code unit's value.
To most efficiently unescape
@@ -166,7 +169,11 @@
$json = json_decode( preg_replace(
"/\\\\\\\\u(?!00[0-7])/", "\\\\u", "\"$json\"" ) );
$json = str_replace( self::$badChars,
self::$badCharsEscaped, $json );
}
- return $pretty ? self::prettyPrint( $json ) : $json;
+
+ if ( $pretty ) {
+ return self::prettyPrint( $json );
+ }
+ return $json;
}
/**
@@ -188,14 +195,14 @@
break;
case '[':
case '{':
- $indent++; // falls through
+ ++$indent;
+ // falls through
case ',':
$buf .= $json[$i] . "\n" . str_repeat(
' ', $indent );
break;
case ']':
case '}':
- $indent--;
- $buf .= "\n" . str_repeat( ' ',
$indent ) . $json[$i];
+ $buf .= "\n" . str_repeat( ' ',
--$indent ) . $json[$i];
break;
case '"':
$skip = strcspn( $json, '"', $i + 1 ) +
2;
--
To view, visit https://gerrit.wikimedia.org/r/88650
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8fc88e9b7b49aa0cbd4128216557836a3b2cd011
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: PleaseStand <[email protected]>
Gerrit-Reviewer: Bartosz DziewoĆski <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits