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 | * For better usefulness, in addition to "actual" RouterOS types, a pseudo |
||
| 75 | * "date" type is also recognized, whenever the string is in the form |
||
| 76 | * "M/j/Y". |
||
| 77 | * |
||
| 78 | * @param string $value The value to be parsed. Must be a literal of a |
||
| 79 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
| 80 | * |
||
| 81 | * @return mixed Depending on RouterOS type detected: |
||
| 82 | * - "nil" or "nothing" - NULL. |
||
| 83 | * - "number" - int or double for large values. |
||
| 84 | * - "bool" - a boolean. |
||
| 85 | * - "time" - a {@link DateInterval} object. |
||
| 86 | * - "array" - an array, with the values processed recursively. |
||
| 87 | * - "str" - a string. |
||
| 88 | * - "date" (pseudo type) - a DateTime object with the specified date, |
||
| 89 | * at midnight UTC time. |
||
| 90 | * - Unrecognized type - treated as an unquoted string. |
||
| 91 | */ |
||
| 92 | public static function parseValue($value) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Prepares a script. |
||
| 264 | * |
||
| 265 | * Prepares a script for eventual execution by prepending parameters as |
||
| 266 | * variables to it. |
||
| 267 | * |
||
| 268 | * This is particularly useful when you're creating scripts that you don't |
||
| 269 | * want to execute right now (as with {@link static::exec()}, but instead |
||
| 270 | * you want to store it for later execution, perhaps by supplying it to |
||
| 271 | * "/system scheduler". |
||
| 272 | * |
||
| 273 | * @param string|resource $source The source of the script, as a string |
||
| 274 | * or stream. If a stream is provided, reading starts from the current |
||
| 275 | * position to the end of the stream, and the pointer stays at the end |
||
| 276 | * after reading is done. |
||
| 277 | * @param array<string,mixed> $params An array of parameters to make |
||
| 278 | * available in the script as local variables. |
||
| 279 | * Variable names are array keys, and variable values are array values. |
||
| 280 | * Array values are automatically processed with |
||
| 281 | * {@link static::escapeValue()}. Streams are also supported, and are |
||
| 282 | * processed in chunks, each with |
||
| 283 | * {@link static::escapeString()}. Processing starts from the current |
||
| 284 | * position to the end of the stream, and the stream's pointer stays at |
||
| 285 | * the end after reading is done. |
||
| 286 | * |
||
| 287 | * @return resource A new PHP temporary stream with the script as contents, |
||
| 288 | * with the pointer back at the start. |
||
| 289 | * |
||
| 290 | * @see static::appendScript() |
||
| 291 | */ |
||
| 292 | public static function prepare( |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Appends a script. |
||
| 304 | * |
||
| 305 | * Appends a script to an existing stream. |
||
| 306 | * |
||
| 307 | * @param resource $stream An existing stream to write the |
||
| 308 | * resulting script to. |
||
| 309 | * @param string|resource $source The source of the script, as a string |
||
| 310 | * or stream. If a stream is provided, reading starts from the current |
||
| 311 | * position to the end of the stream, and the pointer stays at the end |
||
| 312 | * after reading is done. |
||
| 313 | * @param array<string,mixed> $params An array of parameters to make |
||
| 314 | * available in the script as local variables. |
||
| 315 | * Variable names are array keys, and variable values are array values. |
||
| 316 | * Array values are automatically processed with |
||
| 317 | * {@link static::escapeValue()}. Streams are also supported, and are |
||
| 318 | * processed in chunks, each with |
||
| 319 | * {@link static::escapeString()}. Processing starts from the current |
||
| 320 | * position to the end of the stream, and the stream's pointer stays at |
||
| 321 | * the end after reading is done. |
||
| 322 | * |
||
| 323 | * @return int The number of bytes written to $stream is returned, |
||
| 324 | * and the pointer remains where it was after the write |
||
| 325 | * (i.e. it is not seeked back, even if seeking is supported). |
||
| 326 | */ |
||
| 327 | public static function append( |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Escapes a value for a RouterOS scripting context. |
||
| 359 | * |
||
| 360 | * Turns any native PHP value into an equivalent whole value that can be |
||
| 361 | * inserted as part of a RouterOS script. |
||
| 362 | * |
||
| 363 | * DateInterval objects will be casted to RouterOS' "time" type. |
||
| 364 | * |
||
| 365 | * DateTime objects will be casted to a string following the "M/d/Y H:i:s" |
||
| 366 | * format. If the time is exactly midnight (including microseconds), and |
||
| 367 | * the timezone is UTC, the string will include only the "M/d/Y" date. |
||
| 368 | * |
||
| 369 | * Unrecognized types (i.e. resources and other objects) are casted to |
||
| 370 | * strings. |
||
| 371 | * |
||
| 372 | * @param mixed $value The value to be escaped. |
||
| 373 | * |
||
| 374 | * @return string A string representation that can be directly inserted in a |
||
| 375 | * script as a whole value. |
||
| 376 | */ |
||
| 377 | public static function escapeValue($value) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Escapes a string for a RouterOS scripting context. |
||
| 430 | * |
||
| 431 | * Escapes a string for a RouterOS scripting context. The value can then be |
||
| 432 | * surrounded with quotes at a RouterOS script (or concatenated onto a |
||
| 433 | * larger string first), and you can be sure there won't be any code |
||
| 434 | * injections coming from it. |
||
| 435 | * |
||
| 436 | * @param string $value Value to be escaped. |
||
| 437 | * |
||
| 438 | * @return string The escaped value. |
||
| 439 | */ |
||
| 440 | public static function escapeString($value) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Escapes a character for a RouterOS scripting context. |
||
| 451 | * |
||
| 452 | * Escapes a character for a RouterOS scripting context. Intended to only be |
||
| 453 | * called for non-alphanumeric characters. |
||
| 454 | * |
||
| 455 | * @param string $chars The matches array, expected to contain exactly one |
||
| 456 | * member, in which is the whole string to be escaped. |
||
| 457 | * |
||
| 458 | * @return string The escaped characters. |
||
| 459 | */ |
||
| 460 | private static function _escapeCharacters($chars) |
||
| 473 | } |
||
| 474 |