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 |
||
|
|||
75 | * of a value, e.g. what {@link static::escapeValue()} will give you. |
||
76 | * @param DateTimeZone|null $timezone The timezone which any resulting |
||
77 | * DateTime object (either the main value, or values within an array) |
||
78 | * will use. Defaults to UTC. |
||
79 | * |
||
80 | * @return mixed Depending on RouterOS type detected: |
||
81 | * - "nil" (the string "[]") or "nothing" (empty string) - NULL. |
||
82 | * - "number" - int or double for large values. |
||
83 | * - "bool" - a boolean. |
||
84 | * - "array" - an array, with the keys and values processed recursively. |
||
85 | * - "time" - a {@link DateInterval} object. |
||
86 | * - "date" (pseudo type; string in the form "M/j/Y") - a DateTime |
||
87 | * object with the specified date, at midnight. |
||
88 | * - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a |
||
89 | * DateTime object with the specified date and time. |
||
90 | * - "str" (a quoted string) - a string, with the contents escaped. |
||
91 | * - Unrecognized type - casted to a string, unmodified. |
||
92 | */ |
||
93 | public static function parseValue($value, DateTimeZone $timezone = null) |
||
117 | |||
118 | public static function parseValueToString($value) { |
||
129 | |||
130 | /** |
||
131 | * Parses a RouterOS value into a PHP simple type. |
||
132 | * |
||
133 | * Parses a RouterOS value into a PHP simple type. "Simple" types being |
||
134 | * scalar types, plus NULL. |
||
135 | * |
||
136 | * @param string $value The value to be parsed. Must be a literal of a |
||
137 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
138 | * |
||
139 | * @return string|bool|int|double|null Depending on RouterOS type detected: |
||
140 | * - "nil" (the string "[]") or "nothing" (empty string) - NULL. |
||
141 | * - "number" - int or double for large values. |
||
142 | * - "bool" - a boolean. |
||
143 | * - Unrecognized type - casted to a string, unmodified. |
||
144 | */ |
||
145 | public static function parseValueToSimple($value) |
||
160 | |||
161 | /** |
||
162 | * Parses a RouterOS value into a PHP DateTime object |
||
163 | * |
||
164 | * Parses a RouterOS value into a PHP DateTime object. |
||
165 | * |
||
166 | * @param string $value The value to be parsed. Must be a literal of a |
||
167 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
168 | * @param DateTimeZone $timezone The timezone which the resulting DateTime |
||
169 | * object will use. Defaults to UTC. |
||
170 | * |
||
171 | * @return string|DateTime Depending on RouterOS type detected: |
||
172 | * - "date" (pseudo type; string in the form "M/j/Y") - a DateTime |
||
173 | * object with the specified date, at midnight UTC time (regardless |
||
174 | * of timezone provided). |
||
175 | * - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a |
||
176 | * DateTime object with the specified date and time. |
||
177 | * - Unrecognized type - casted to a string, unmodified. |
||
178 | */ |
||
179 | public static function parseValueToDateTime( |
||
220 | |||
221 | /** |
||
222 | * Parses a RouterOS value into a PHP DateInterval. |
||
223 | * |
||
224 | * Parses a RouterOS value into a PHP DateInterval. |
||
225 | * |
||
226 | * @param string $value The value to be parsed. Must be a literal of a |
||
227 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
228 | * |
||
229 | * @return string|DateInterval|DateTime Depending on RouterOS type detected: |
||
230 | * - "time" - a {@link DateInterval} object. |
||
231 | * - Unrecognized type - casted to a string, unmodified. |
||
232 | */ |
||
233 | public static function parseValueToDateInterval($value) |
||
316 | |||
317 | /** |
||
318 | * Parses a RouterOS value into a PHP array. |
||
319 | * |
||
320 | * Parses a RouterOS value into a PHP array. |
||
321 | * |
||
322 | * @param string $value The value to be parsed. Must be a literal of a |
||
323 | * value, e.g. what {@link static::escapeValue()} will give you. |
||
324 | * |
||
325 | * @return string|array Depending on RouterOS type detected: |
||
326 | * - "array" - an array, with the keys and values processed recursively. |
||
327 | * - Unrecognized type - casted to a string, unmodified. |
||
328 | */ |
||
329 | public static function parseValueToArray($value) |
||
381 | |||
382 | /** |
||
383 | * Prepares a script. |
||
384 | * |
||
385 | * Prepares a script for eventual execution by prepending parameters as |
||
386 | * variables to it. |
||
387 | * |
||
388 | * This is particularly useful when you're creating scripts that you don't |
||
389 | * want to execute right now (as with {@link static::exec()}, but instead |
||
390 | * you want to store it for later execution, perhaps by supplying it to |
||
391 | * "/system scheduler". |
||
392 | * |
||
393 | * @param string|resource $source The source of the script, as a string |
||
394 | * or stream. If a stream is provided, reading starts from the current |
||
395 | * position to the end of the stream, and the pointer stays at the end |
||
396 | * after reading is done. |
||
397 | * @param array<string,mixed> $params An array of parameters to make |
||
398 | * available in the script as local variables. |
||
399 | * Variable names are array keys, and variable values are array values. |
||
400 | * Array values are automatically processed with |
||
401 | * {@link static::escapeValue()}. Streams are also supported, and are |
||
402 | * processed in chunks, each with |
||
403 | * {@link static::escapeString()}. Processing starts from the current |
||
404 | * position to the end of the stream, and the stream's pointer stays at |
||
405 | * the end after reading is done. |
||
406 | * |
||
407 | * @return resource A new PHP temporary stream with the script as contents, |
||
408 | * with the pointer back at the start. |
||
409 | * |
||
410 | * @see static::append() |
||
411 | */ |
||
412 | public static function prepare( |
||
421 | |||
422 | /** |
||
423 | * Appends a script. |
||
424 | * |
||
425 | * Appends a script to an existing stream. |
||
426 | * |
||
427 | * @param resource $stream An existing stream to write the |
||
428 | * resulting script to. |
||
429 | * @param string|resource $source The source of the script, as a string |
||
430 | * or stream. If a stream is provided, reading starts from the current |
||
431 | * position to the end of the stream, and the pointer stays at the end |
||
432 | * after reading is done. |
||
433 | * @param array<string,mixed> $params An array of parameters to make |
||
434 | * available in the script as local variables. |
||
435 | * Variable names are array keys, and variable values are array values. |
||
436 | * Array values are automatically processed with |
||
437 | * {@link static::escapeValue()}. Streams are also supported, and are |
||
438 | * processed in chunks, each with |
||
439 | * {@link static::escapeString()}. Processing starts from the current |
||
440 | * position to the end of the stream, and the stream's pointer stays at |
||
441 | * the end after reading is done. |
||
442 | * |
||
443 | * @return int The number of bytes written to $stream is returned, |
||
444 | * and the pointer remains where it was after the write |
||
445 | * (i.e. it is not seeked back, even if seeking is supported). |
||
446 | */ |
||
447 | public static function append( |
||
476 | |||
477 | /** |
||
478 | * Escapes a value for a RouterOS scripting context. |
||
479 | * |
||
480 | * Turns any native PHP value into an equivalent whole value that can be |
||
481 | * inserted as part of a RouterOS script. |
||
482 | * |
||
483 | * DateInterval objects will be casted to RouterOS' "time" type. |
||
484 | * |
||
485 | * DateTime objects will be casted to a string following the "M/d/Y H:i:s" |
||
486 | * format. If the time is exactly midnight (including microseconds), and |
||
487 | * the timezone is UTC, the string will include only the "M/d/Y" date. |
||
488 | * |
||
489 | * Unrecognized types (i.e. resources and other objects) are casted to |
||
490 | * strings, and those strings are then escaped. |
||
491 | * |
||
492 | * @param mixed $value The value to be escaped. |
||
493 | * |
||
494 | * @return string A string representation that can be directly inserted in a |
||
495 | * script as a whole value. |
||
496 | */ |
||
497 | public static function escapeValue($value) |
||
547 | |||
548 | /** |
||
549 | * Escapes a string for a RouterOS scripting context. |
||
550 | * |
||
551 | * Escapes a string for a RouterOS scripting context. The value can then be |
||
552 | * surrounded with quotes at a RouterOS script (or concatenated onto a |
||
553 | * larger string first), and you can be sure there won't be any code |
||
554 | * injections coming from it. |
||
555 | * |
||
556 | * For the sake of brevity of the output, alphanumeric characters and |
||
557 | * underscores are left untouched |
||
558 | * |
||
559 | * @param string $value Value to be escaped. |
||
560 | * |
||
561 | * @return string The escaped value. |
||
562 | * |
||
563 | * @internal Why leave ONLY those characters and not also others? |
||
564 | * Because those can't in any way be mistaken for language constructs, |
||
565 | * unlike many other "safe inside strings, but not outside" ASCII |
||
566 | * characters, like ",", ".", "+", "-", "~", etc. |
||
567 | */ |
||
568 | public static function escapeString($value) |
||
576 | |||
577 | /** |
||
578 | * Escapes a character for a RouterOS scripting context. |
||
579 | * |
||
580 | * Escapes a character for a RouterOS scripting context. Intended to only be |
||
581 | * called for non-alphanumeric characters. |
||
582 | * |
||
583 | * @param string $chars The matches array, expected to contain exactly one |
||
584 | * member, in which is the whole string to be escaped. |
||
585 | * |
||
586 | * @return string The escaped characters. |
||
587 | */ |
||
588 | private static function _escapeCharacters($chars) |
||
601 | } |
||
602 |