Complex classes like Convert 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 Convert, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Convert |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Convert a value to be suitable for an XML attribute. |
||
| 34 | * |
||
| 35 | * @param array|string $val String to escape, or array of strings |
||
| 36 | * @return array|string |
||
| 37 | */ |
||
| 38 | public static function raw2att($val) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Convert a value to be suitable for an HTML attribute. |
||
| 45 | * |
||
| 46 | * @param string|array $val String to escape, or array of strings |
||
| 47 | * @return array|string |
||
| 48 | */ |
||
| 49 | public static function raw2htmlatt($val) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Convert a value to be suitable for an HTML ID attribute. Replaces non |
||
| 56 | * supported characters with a space. |
||
| 57 | * |
||
| 58 | * @see http://www.w3.org/TR/REC-html40/types.html#type-cdata |
||
| 59 | * |
||
| 60 | * @param array|string $val String to escape, or array of strings |
||
| 61 | * |
||
| 62 | * @return array|string |
||
| 63 | */ |
||
| 64 | public static function raw2htmlname($val) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Convert a value to be suitable for an HTML ID attribute. Replaces non |
||
| 79 | * supported characters with an underscore. |
||
| 80 | * |
||
| 81 | * @see http://www.w3.org/TR/REC-html40/types.html#type-cdata |
||
| 82 | * |
||
| 83 | * @param array|string $val String to escape, or array of strings |
||
| 84 | * |
||
| 85 | * @return array|string |
||
| 86 | */ |
||
| 87 | public static function raw2htmlid($val) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Ensure that text is properly escaped for XML. |
||
| 109 | * |
||
| 110 | * @see http://www.w3.org/TR/REC-xml/#dt-escape |
||
| 111 | * @param array|string $val String to escape, or array of strings |
||
| 112 | * @return array|string |
||
| 113 | */ |
||
| 114 | public static function raw2xml($val) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Ensure that text is properly escaped for Javascript. |
||
| 128 | * |
||
| 129 | * @param array|string $val String to escape, or array of strings |
||
| 130 | * @return array|string |
||
| 131 | */ |
||
| 132 | public static function raw2js($val) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Encode a value as a JSON encoded string. |
||
| 151 | * |
||
| 152 | * @param mixed $val Value to be encoded |
||
| 153 | * @return string JSON encoded string |
||
| 154 | */ |
||
| 155 | public static function raw2json($val) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Encode an array as a JSON encoded string. |
||
| 162 | * THis is an alias to {@link raw2json()} |
||
| 163 | * |
||
| 164 | * @param array $val Array to convert |
||
| 165 | * @return string JSON encoded string |
||
| 166 | */ |
||
| 167 | public static function array2json($val) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Safely encodes a value (or list of values) using the current database's |
||
| 174 | * safe string encoding method |
||
| 175 | * |
||
| 176 | * @param mixed|array $val Input value, or list of values as an array |
||
| 177 | * @param boolean $quoted Flag indicating whether the value should be safely |
||
| 178 | * quoted, instead of only being escaped. By default this function will |
||
| 179 | * only escape the string (false). |
||
| 180 | * @return string|array Safely encoded value in the same format as the input |
||
| 181 | */ |
||
| 182 | public static function raw2sql($val, $quoted = false) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Safely encodes a SQL symbolic identifier (or list of identifiers), such as a database, |
||
| 200 | * table, or column name. Supports encoding of multi identfiers separated by |
||
| 201 | * a delimiter (e.g. ".") |
||
| 202 | * |
||
| 203 | * @param string|array $identifier The identifier to escape. E.g. 'SiteTree.Title' or list of identifiers |
||
| 204 | * to be joined via the separator. |
||
| 205 | * @param string $separator The string that delimits subsequent identifiers |
||
| 206 | * @return string The escaped identifier. E.g. '"SiteTree"."Title"' |
||
| 207 | */ |
||
| 208 | public static function symbol2sql($identifier, $separator = '.') |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Convert XML to raw text. |
||
| 215 | * @uses html2raw() |
||
| 216 | * @todo Currently &#xxx; entries are stripped; they should be converted |
||
| 217 | * @param mixed $val |
||
| 218 | * @return array|string |
||
| 219 | */ |
||
| 220 | public static function xml2raw($val) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Convert a JSON encoded string into an object. |
||
| 239 | * |
||
| 240 | * @param string $val |
||
| 241 | * @return object|boolean |
||
| 242 | */ |
||
| 243 | public static function json2obj($val) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Convert a JSON string into an array. |
||
| 250 | * |
||
| 251 | * @uses json2obj |
||
| 252 | * @param string $val JSON string to convert |
||
| 253 | * @return array|boolean |
||
| 254 | */ |
||
| 255 | public static function json2array($val) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Converts an XML string to a PHP array |
||
| 262 | * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection |
||
| 263 | * |
||
| 264 | * @uses recursiveXMLToArray() |
||
| 265 | * @param string $val |
||
| 266 | * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. |
||
| 267 | * false by default. |
||
| 268 | * @param boolean $disableExternals Disables the loading of external entities. false by default. |
||
| 269 | * @return array |
||
| 270 | * @throws Exception |
||
| 271 | */ |
||
| 272 | public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Convert a XML string to a PHP array recursively. Do not |
||
| 301 | * call this function directly, Please use {@link Convert::xml2array()} |
||
| 302 | * |
||
| 303 | * @param SimpleXMLElement |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | protected static function recursiveXMLToArray($xml) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Create a link if the string is a valid URL |
||
| 340 | * |
||
| 341 | * @param string $string The string to linkify |
||
| 342 | * @return string A link to the URL if string is a URL |
||
| 343 | */ |
||
| 344 | public static function linkIfMatch($string) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Simple conversion of HTML to plaintext. |
||
| 355 | * |
||
| 356 | * @param string $data Input data |
||
| 357 | * @param bool $preserveLinks |
||
| 358 | * @param int $wordWrap |
||
| 359 | * @param array $config |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public static function html2raw($data, $preserveLinks = false, $wordWrap = 0, $config = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * There are no real specifications on correctly encoding mailto-links, |
||
| 436 | * but this seems to be compatible with most of the user-agents. |
||
| 437 | * Does nearly the same as rawurlencode(). |
||
| 438 | * Please only encode the values, not the whole url, e.g. |
||
| 439 | * "mailto:[email protected]?subject=" . Convert::raw2mailto($subject) |
||
| 440 | * |
||
| 441 | * @param $data string |
||
| 442 | * @return string |
||
| 443 | * @see http://www.ietf.org/rfc/rfc1738.txt |
||
| 444 | */ |
||
| 445 | public static function raw2mailto($data) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Convert a string (normally a title) to a string suitable for using in |
||
| 456 | * urls and other html attributes. Uses {@link URLSegmentFilter}. |
||
| 457 | * |
||
| 458 | * @param string |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public static function raw2url($title) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Normalises newline sequences to conform to (an) OS specific format. |
||
| 469 | * |
||
| 470 | * @param string $data Text containing potentially mixed formats of newline |
||
| 471 | * sequences including \r, \r\n, \n, or unicode newline characters |
||
| 472 | * @param string $nl The newline sequence to normalise to. Defaults to that |
||
| 473 | * specified by the current OS |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public static function nl2os($data, $nl = PHP_EOL) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Encode a value into a string that can be used as part of a filename. |
||
| 483 | * All string data must be UTF-8 encoded. |
||
| 484 | * |
||
| 485 | * @param mixed $val Value to be encoded |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public static function base64url_encode($val) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Decode a value that was encoded with Convert::base64url_encode. |
||
| 495 | * |
||
| 496 | * @param string $val Value to be decoded |
||
| 497 | * @return mixed Original value |
||
| 498 | */ |
||
| 499 | public static function base64url_decode($val) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Converts upper camel case names to lower camel case, |
||
| 509 | * with leading upper case characters replaced with lower case. |
||
| 510 | * Tries to retain word case. |
||
| 511 | * |
||
| 512 | * Examples: |
||
| 513 | * - ID => id |
||
| 514 | * - IDField => idField |
||
| 515 | * - iDField => iDField |
||
| 516 | * |
||
| 517 | * @param $str |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public static function upperCamelToLowerCamel($str) |
||
| 550 | } |
||
| 551 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.