Complex classes like FormatJson often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FormatJson, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class FormatJson { |
||
27 | /** |
||
28 | * Skip escaping most characters above U+007F for readability and compactness. |
||
29 | * This encoding option saves 3 to 8 bytes (uncompressed) for each such character; |
||
30 | * however, it could break compatibility with systems that incorrectly handle UTF-8. |
||
31 | * |
||
32 | * @since 1.22 |
||
33 | */ |
||
34 | const UTF8_OK = 1; |
||
35 | |||
36 | /** |
||
37 | * Skip escaping the characters '<', '>', and '&', which have special meanings in |
||
38 | * HTML and XML. |
||
39 | * |
||
40 | * @warning Do not use this option for JSON that could end up in inline scripts. |
||
41 | * - HTML5, §4.3.1.2 Restrictions for contents of script elements |
||
42 | * - XML 1.0 (5th Ed.), §2.4 Character Data and Markup |
||
43 | * |
||
44 | * @since 1.22 |
||
45 | */ |
||
46 | const XMLMETA_OK = 2; |
||
47 | |||
48 | /** |
||
49 | * Skip escaping as many characters as reasonably possible. |
||
50 | * |
||
51 | * @warning When generating inline script blocks, use FormatJson::UTF8_OK instead. |
||
52 | * |
||
53 | * @since 1.22 |
||
54 | */ |
||
55 | const ALL_OK = 3; |
||
56 | |||
57 | /** |
||
58 | * If set, treat json objects '{...}' as associative arrays. Without this option, |
||
59 | * json objects will be converted to stdClass. |
||
60 | * The value is set to 1 to be backward compatible with 'true' that was used before. |
||
61 | * |
||
62 | * @since 1.24 |
||
63 | */ |
||
64 | const FORCE_ASSOC = 0x100; |
||
65 | |||
66 | /** |
||
67 | * If set, attempts to fix invalid json. |
||
68 | * |
||
69 | * @since 1.24 |
||
70 | */ |
||
71 | const TRY_FIXING = 0x200; |
||
72 | |||
73 | /** |
||
74 | * If set, strip comments from input before parsing as JSON. |
||
75 | * |
||
76 | * @since 1.25 |
||
77 | */ |
||
78 | const STRIP_COMMENTS = 0x400; |
||
79 | |||
80 | /** |
||
81 | * Regex that matches whitespace inside empty arrays and objects. |
||
82 | * |
||
83 | * This doesn't affect regular strings inside the JSON because those can't |
||
84 | * have a real line break (\n) in them, at this point they are already escaped |
||
85 | * as the string "\n" which this doesn't match. |
||
86 | * |
||
87 | * @private |
||
88 | */ |
||
89 | const WS_CLEANUP_REGEX = '/(?<=[\[{])\n\s*+(?=[\]}])/'; |
||
90 | |||
91 | /** |
||
92 | * Characters problematic in JavaScript. |
||
93 | * |
||
94 | * @note These are listed in ECMA-262 (5.1 Ed.), §7.3 Line Terminators along with U+000A (LF) |
||
95 | * and U+000D (CR). However, PHP already escapes LF and CR according to RFC 4627. |
||
96 | */ |
||
97 | private static $badChars = [ |
||
98 | "\xe2\x80\xa8", // U+2028 LINE SEPARATOR |
||
99 | "\xe2\x80\xa9", // U+2029 PARAGRAPH SEPARATOR |
||
100 | ]; |
||
101 | |||
102 | /** |
||
103 | * Escape sequences for characters listed in FormatJson::$badChars. |
||
104 | */ |
||
105 | private static $badCharsEscaped = [ |
||
106 | '\u2028', // U+2028 LINE SEPARATOR |
||
107 | '\u2029', // U+2029 PARAGRAPH SEPARATOR |
||
108 | ]; |
||
109 | |||
110 | /** |
||
111 | * Returns the JSON representation of a value. |
||
112 | * |
||
113 | * @note Empty arrays are encoded as numeric arrays, not as objects, so cast any associative |
||
114 | * array that might be empty to an object before encoding it. |
||
115 | * |
||
116 | * @note In pre-1.22 versions of MediaWiki, using this function for generating inline script |
||
117 | * blocks may result in an XSS vulnerability, and quite likely will in XML documents |
||
118 | * (cf. FormatJson::XMLMETA_OK). Use Xml::encodeJsVar() instead in such cases. |
||
119 | * |
||
120 | * @param mixed $value The value to encode. Can be any type except a resource. |
||
121 | * @param string|bool $pretty If a string, add non-significant whitespace to improve |
||
122 | * readability, using that string for indentation. If true, use the default indent |
||
123 | * string (four spaces). |
||
124 | * @param int $escaping Bitfield consisting of _OK class constants |
||
125 | * @return string|false String if successful; false upon failure |
||
126 | */ |
||
127 | public static function encode( $value, $pretty = false, $escaping = 0 ) { |
||
173 | |||
174 | /** |
||
175 | * Decodes a JSON string. It is recommended to use FormatJson::parse(), |
||
176 | * which returns more comprehensive result in case of an error, and has |
||
177 | * more parsing options. |
||
178 | * |
||
179 | * @param string $value The JSON string being decoded |
||
180 | * @param bool $assoc When true, returned objects will be converted into associative arrays. |
||
181 | * |
||
182 | * @return mixed The value encoded in JSON in appropriate PHP type. |
||
183 | * `null` is returned if $value represented `null`, if $value could not be decoded, |
||
184 | * or if the encoded data was deeper than the recursion limit. |
||
185 | * Use FormatJson::parse() to distinguish between types of `null` and to get proper error code. |
||
186 | */ |
||
187 | public static function decode( $value, $assoc = false ) { |
||
190 | |||
191 | /** |
||
192 | * Decodes a JSON string. |
||
193 | * Unlike FormatJson::decode(), if $value represents null value, it will be |
||
194 | * properly decoded as valid. |
||
195 | * |
||
196 | * @param string $value The JSON string being decoded |
||
197 | * @param int $options A bit field that allows FORCE_ASSOC, TRY_FIXING, |
||
198 | * STRIP_COMMENTS |
||
199 | * @return Status If valid JSON, the value is available in $result->getValue() |
||
200 | */ |
||
201 | public static function parse( $value, $options = 0 ) { |
||
263 | |||
264 | /** |
||
265 | * Remove multiline and single line comments from an otherwise valid JSON |
||
266 | * input string. This can be used as a preprocessor for to allow JSON |
||
267 | * formatted configuration files to contain comments. |
||
268 | * |
||
269 | * @param string $json |
||
270 | * @return string JSON with comments removed |
||
271 | */ |
||
272 | public static function stripComments( $json ) { |
||
338 | } |
||
339 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.