Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Request extends Message |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string The command to be executed. |
||
| 42 | */ |
||
| 43 | private $_command; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Query A query for the command. |
||
| 47 | */ |
||
| 48 | private $_query; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates a request to send to RouterOS. |
||
| 52 | * |
||
| 53 | * @param string $command The command to send. |
||
| 54 | * Can also contain arguments expressed in a shell-like syntax. |
||
| 55 | * @param Query|null $query A query to associate with the request. |
||
| 56 | * @param string|null $tag The tag for the request. |
||
| 57 | * |
||
| 58 | * @see setCommand() |
||
| 59 | * @see setArgument() |
||
| 60 | * @see setTag() |
||
| 61 | * @see setQuery() |
||
| 62 | */ |
||
| 63 | public function __construct($command, Query $query = null, $tag = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * A shorthand gateway. |
||
| 81 | * |
||
| 82 | * This is a magic PHP method that allows you to call the object as a |
||
| 83 | * function. Depending on the argument given, one of the other functions in |
||
| 84 | * the class is invoked and its returned value is returned by this function. |
||
| 85 | * |
||
| 86 | * @param Query|Communicator|string|null $arg A {@link Query} to associate |
||
| 87 | * the request with, a {@link Communicator} to send the request over, |
||
| 88 | * an argument to get the value of, or NULL to get the tag. If a |
||
| 89 | * second argument is provided, this becomes the name of the argument to |
||
| 90 | * set the value of, and the second argument is the value to set. |
||
| 91 | * |
||
| 92 | * @return string|resource|int|$this Whatever the long form |
||
| 93 | * function returns. |
||
| 94 | */ |
||
| 95 | public function __invoke($arg = null) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Sets the command to send to RouterOS. |
||
| 111 | * |
||
| 112 | * Sets the command to send to RouterOS. The command can use the API or CLI |
||
| 113 | * syntax of RouterOS, but either way, it must be absolute (begin with a |
||
| 114 | * "/") and without arguments. |
||
| 115 | * |
||
| 116 | * @param string $command The command to send. |
||
| 117 | * |
||
| 118 | * @return $this The request object. |
||
| 119 | * |
||
| 120 | * @see getCommand() |
||
| 121 | * @see setArgument() |
||
| 122 | */ |
||
| 123 | public function setCommand($command) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the command that will be send to RouterOS. |
||
| 165 | * |
||
| 166 | * Gets the command that will be send to RouterOS in its API syntax. |
||
| 167 | * |
||
| 168 | * @return string The command to send. |
||
| 169 | * |
||
| 170 | * @see setCommand() |
||
| 171 | */ |
||
| 172 | public function getCommand() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Sets the query to send with the command. |
||
| 179 | * |
||
| 180 | * @param Query|null $query The query to be set. |
||
| 181 | * Setting NULL will remove the currently associated query. |
||
| 182 | * |
||
| 183 | * @return $this The request object. |
||
| 184 | * |
||
| 185 | * @see getQuery() |
||
| 186 | */ |
||
| 187 | public function setQuery(Query $query = null) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Gets the currently associated query |
||
| 195 | * |
||
| 196 | * @return Query|null The currently associated query. |
||
| 197 | * |
||
| 198 | * @see setQuery() |
||
| 199 | */ |
||
| 200 | public function getQuery() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the tag to associate the request with. |
||
| 207 | * |
||
| 208 | * Sets the tag to associate the request with. Setting NULL erases the |
||
| 209 | * currently set tag. |
||
| 210 | * |
||
| 211 | * @param string|null $tag The tag to set. |
||
| 212 | * |
||
| 213 | * @return $this The request object. |
||
| 214 | * |
||
| 215 | * @see getTag() |
||
| 216 | */ |
||
| 217 | public function setTag($tag) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets an argument for the request. |
||
| 224 | * |
||
| 225 | * @param string $name Name of the argument. |
||
| 226 | * @param string|resource|null $value Value of the argument as a string or |
||
| 227 | * seekable stream. |
||
| 228 | * Setting the value to NULL removes an argument of this name. |
||
| 229 | * If a seekable stream is provided, it is sent from its current |
||
| 230 | * position to its end, and the pointer is seeked back to its current |
||
| 231 | * position after sending. |
||
| 232 | * Non seekable streams, as well as all other types, are casted to a |
||
| 233 | * string. |
||
| 234 | * |
||
| 235 | * @return $this The request object. |
||
| 236 | * |
||
| 237 | * @see getArgument() |
||
| 238 | */ |
||
| 239 | public function setArgument($name, $value = '') |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Gets the value of an argument. |
||
| 246 | * |
||
| 247 | * @param string $name The name of the argument. |
||
| 248 | * |
||
| 249 | * @return string|resource|null The value of the specified argument. |
||
| 250 | * Returns NULL if such an argument is not set. |
||
| 251 | * |
||
| 252 | * @see setAttribute() |
||
| 253 | */ |
||
| 254 | public function getArgument($name) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Removes all arguments from the request. |
||
| 261 | * |
||
| 262 | * @return $this The request object. |
||
| 263 | */ |
||
| 264 | public function removeAllArguments() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Sends a request over a communicator. |
||
| 271 | * |
||
| 272 | * @param Communicator $com The communicator to send the request over. |
||
| 273 | * @param Registry|null $reg An optional registry to sync the request with. |
||
| 274 | * |
||
| 275 | * @return int The number of bytes sent. |
||
| 276 | * |
||
| 277 | * @see Client::sendSync() |
||
| 278 | * @see Client::sendAsync() |
||
| 279 | */ |
||
| 280 | public function send(Communicator $com, Registry $reg = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Sends a request over a communicator. |
||
| 302 | * |
||
| 303 | * The only difference with the non private equivalent is that this one does |
||
| 304 | * not do locking. |
||
| 305 | * |
||
| 306 | * @param Communicator $com The communicator to send the request over. |
||
| 307 | * |
||
| 308 | * @return int The number of bytes sent. |
||
| 309 | * |
||
| 310 | * @see Client::sendSync() |
||
| 311 | * @see Client::sendAsync() |
||
| 312 | */ |
||
| 313 | private function _send(Communicator $com) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Verifies the request. |
||
| 344 | * |
||
| 345 | * Verifies the request against a communicator, i.e. whether the request |
||
| 346 | * could successfully be sent (assuming the connection is still opened). |
||
| 347 | * |
||
| 348 | * @param Communicator $com The Communicator to check against. |
||
| 349 | * |
||
| 350 | * @return $this The request object itself. |
||
| 351 | * |
||
| 352 | * @throws LengthException If the resulting length of an API word is not |
||
| 353 | * supported. |
||
| 354 | */ |
||
| 355 | public function verify(Communicator $com) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Parses the arguments of a command. |
||
| 378 | * |
||
| 379 | * @param string $string The argument string to parse. |
||
| 380 | * |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | protected function parseArgumentString($string) |
||
| 446 | } |
||
| 447 |