Complex classes like Script 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 Script, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class Script |
||
| 62 | { |
||
| 63 | /** |
||
| 64 | * Parses a value from a RouterOS scripting context. |
||
| 65 | * |
||
| 66 | * Turns a value from RouterOS into an equivalent PHP value, based on |
||
| 67 | * determining the type in the same way RouterOS would determine it for a |
||
| 68 | * literal. |
||
| 69 | * |
||
| 70 | * This method is intended to be the very opposite of |
||
| 71 | * {@link static::escapeValue()}. That is, results from that method, if |
||
| 72 | * given to this method, should produce equivalent results. |
||
| 73 | * |
||
| 74 | * @param string $value The value to be parsed. Must be a literal of a |
||
| 75 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
| 76 | * |
||
| 77 | * @return mixed Depending on RouterOS type detected: |
||
| 78 | * - "nil" (the string "[]") or "nothing" (empty string) - NULL. |
||
| 79 | * - "number" - int or double for large values. |
||
| 80 | * - "bool" - a boolean. |
||
| 81 | * - "array" - an array, with the keys and values processed recursively. |
||
| 82 | * - "str" - a string. |
||
| 83 | * - "time" - a {@link DateInterval} object. |
||
| 84 | * - "date" (pseudo type; string in the form "M/j/Y") - a DateTime |
||
| 85 | * object with the specified date, at midnight UTC time. |
||
| 86 | * - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a |
||
| 87 | * DateTime object with the specified date and UTC time. |
||
| 88 | * - Unrecognized type - treated as an unquoted string. |
||
| 89 | */ |
||
| 90 | public static function parseValue($value) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Parses a RouterOS value into a PHP simple type. |
||
| 118 | * |
||
| 119 | * Parses a RouterOS value into a PHP simple type. "Simple" types being |
||
| 120 | * scalar types, plus NULL. |
||
| 121 | * |
||
| 122 | * @param string $value The value to be parsed. Must be a literal of a |
||
| 123 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
| 124 | * |
||
| 125 | * @return string|bool|int|double|null Depending on RouterOS type detected: |
||
| 126 | * - "nil" (the string "[]") or "nothing" (empty string) - NULL. |
||
| 127 | * - "number" - int or double for large values. |
||
| 128 | * - "bool" - a boolean. |
||
| 129 | * - Unrecognized type - treated as an unquoted string. |
||
| 130 | */ |
||
| 131 | public static function parseValueToSimple($value) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Parses a RouterOS value into a PHP object. |
||
| 149 | * |
||
| 150 | * Parses a RouterOS value into a PHP object. |
||
| 151 | * |
||
| 152 | * @param string $value The value to be parsed. Must be a literal of a |
||
| 153 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
| 154 | * |
||
| 155 | * @return string|DateInterval|DateTime Depending on RouterOS type detected: |
||
| 156 | * - "time" - a {@link DateInterval} object. |
||
| 157 | * - "date" (pseudo type; string in the form "M/j/Y") - a DateTime |
||
| 158 | * object with the specified date, at midnight UTC time. |
||
| 159 | * - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a |
||
| 160 | * DateTime object with the specified date and UTC time. |
||
| 161 | * - Unrecognized type - treated as an unquoted string. |
||
| 162 | */ |
||
| 163 | public static function parseValueToObject($value) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Parses a RouterOS value into a PHP array. |
||
| 275 | * |
||
| 276 | * Parses a RouterOS value into a PHP array. |
||
| 277 | * |
||
| 278 | * @param string $value The value to be parsed. Must be a literal of a |
||
| 279 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
| 280 | * |
||
| 281 | * @return string|array Depending on RouterOS type detected: |
||
| 282 | * - "array" - an array, with the keys and values processed recursively. |
||
| 283 | * - Unrecognized type - treated as an unquoted string. |
||
| 284 | */ |
||
| 285 | public static function parseValueToArray($value) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Prepares a script. |
||
| 339 | * |
||
| 340 | * Prepares a script for eventual execution by prepending parameters as |
||
| 341 | * variables to it. |
||
| 342 | * |
||
| 343 | * This is particularly useful when you're creating scripts that you don't |
||
| 344 | * want to execute right now (as with {@link static::exec()}, but instead |
||
| 345 | * you want to store it for later execution, perhaps by supplying it to |
||
| 346 | * "/system scheduler". |
||
| 347 | * |
||
| 348 | * @param string|resource $source The source of the script, as a string |
||
| 349 | * or stream. If a stream is provided, reading starts from the current |
||
| 350 | * position to the end of the stream, and the pointer stays at the end |
||
| 351 | * after reading is done. |
||
| 352 | * @param array<string,mixed> $params An array of parameters to make |
||
| 353 | * available in the script as local variables. |
||
| 354 | * Variable names are array keys, and variable values are array values. |
||
| 355 | * Array values are automatically processed with |
||
| 356 | * {@link static::escapeValue()}. Streams are also supported, and are |
||
| 357 | * processed in chunks, each with |
||
| 358 | * {@link static::escapeString()}. Processing starts from the current |
||
| 359 | * position to the end of the stream, and the stream's pointer stays at |
||
| 360 | * the end after reading is done. |
||
| 361 | * |
||
| 362 | * @return resource A new PHP temporary stream with the script as contents, |
||
| 363 | * with the pointer back at the start. |
||
| 364 | * |
||
| 365 | * @see static::append() |
||
| 366 | */ |
||
| 367 | public static function prepare( |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Appends a script. |
||
| 379 | * |
||
| 380 | * Appends a script to an existing stream. |
||
| 381 | * |
||
| 382 | * @param resource $stream An existing stream to write the |
||
| 383 | * resulting script to. |
||
| 384 | * @param string|resource $source The source of the script, as a string |
||
| 385 | * or stream. If a stream is provided, reading starts from the current |
||
| 386 | * position to the end of the stream, and the pointer stays at the end |
||
| 387 | * after reading is done. |
||
| 388 | * @param array<string,mixed> $params An array of parameters to make |
||
| 389 | * available in the script as local variables. |
||
| 390 | * Variable names are array keys, and variable values are array values. |
||
| 391 | * Array values are automatically processed with |
||
| 392 | * {@link static::escapeValue()}. Streams are also supported, and are |
||
| 393 | * processed in chunks, each with |
||
| 394 | * {@link static::escapeString()}. Processing starts from the current |
||
| 395 | * position to the end of the stream, and the stream's pointer stays at |
||
| 396 | * the end after reading is done. |
||
| 397 | * |
||
| 398 | * @return int The number of bytes written to $stream is returned, |
||
| 399 | * and the pointer remains where it was after the write |
||
| 400 | * (i.e. it is not seeked back, even if seeking is supported). |
||
| 401 | */ |
||
| 402 | public static function append( |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Escapes a value for a RouterOS scripting context. |
||
| 434 | * |
||
| 435 | * Turns any native PHP value into an equivalent whole value that can be |
||
| 436 | * inserted as part of a RouterOS script. |
||
| 437 | * |
||
| 438 | * DateInterval objects will be casted to RouterOS' "time" type. |
||
| 439 | * |
||
| 440 | * DateTime objects will be casted to a string following the "M/d/Y H:i:s" |
||
| 441 | * format. If the time is exactly midnight (including microseconds), and |
||
| 442 | * the timezone is UTC, the string will include only the "M/d/Y" date. |
||
| 443 | * |
||
| 444 | * Unrecognized types (i.e. resources and other objects) are casted to |
||
| 445 | * strings. |
||
| 446 | * |
||
| 447 | * @param mixed $value The value to be escaped. |
||
| 448 | * |
||
| 449 | * @return string A string representation that can be directly inserted in a |
||
| 450 | * script as a whole value. |
||
| 451 | */ |
||
| 452 | public static function escapeValue($value) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Escapes a string for a RouterOS scripting context. |
||
| 505 | * |
||
| 506 | * Escapes a string for a RouterOS scripting context. The value can then be |
||
| 507 | * surrounded with quotes at a RouterOS script (or concatenated onto a |
||
| 508 | * larger string first), and you can be sure there won't be any code |
||
| 509 | * injections coming from it. |
||
| 510 | * |
||
| 511 | * @param string $value Value to be escaped. |
||
| 512 | * |
||
| 513 | * @return string The escaped value. |
||
| 514 | */ |
||
| 515 | public static function escapeString($value) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Escapes a character for a RouterOS scripting context. |
||
| 526 | * |
||
| 527 | * Escapes a character for a RouterOS scripting context. Intended to only be |
||
| 528 | * called for non-alphanumeric characters. |
||
| 529 | * |
||
| 530 | * @param string $chars The matches array, expected to contain exactly one |
||
| 531 | * member, in which is the whole string to be escaped. |
||
| 532 | * |
||
| 533 | * @return string The escaped characters. |
||
| 534 | */ |
||
| 535 | private static function _escapeCharacters($chars) |
||
| 548 | } |
||
| 549 |