FormatJson: minor cleanup
authorKevin Israel <pleasestand@live.com>
Tue, 8 Oct 2013 14:52:01 +0000 (10:52 -0400)
committerKevin Israel <pleasestand@live.com>
Tue, 8 Oct 2013 22:28:09 +0000 (18:28 -0400)
* 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

includes/json/FormatJson.php

index 91e1e87..bc2fff1 100644 (file)
@@ -90,10 +90,10 @@ class FormatJson {
         * @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 @@ class FormatJson {
         * @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 @@ class FormatJson {
         */
        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 @@ class FormatJson {
                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 @@ class FormatJson {
                        $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 @@ class FormatJson {
                                        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;