Complex classes like Nexcessnet_Turpentine_Model_Varnish_Admin_Socket 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 Nexcessnet_Turpentine_Model_Varnish_Admin_Socket, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class Nexcessnet_Turpentine_Model_Varnish_Admin_Socket { |
||
67 | |||
68 | // possible command return codes, from vcli.h |
||
69 | const CODE_SYNTAX = 100; |
||
70 | const CODE_UNKNOWN = 101; |
||
71 | const CODE_UNIMPL = 102; |
||
72 | const CODE_TOOFEW = 104; |
||
73 | const CODE_TOOMANY = 105; |
||
74 | const CODE_PARAM = 106; |
||
75 | const CODE_AUTH = 107; |
||
76 | const CODE_OK = 200; |
||
77 | const CODE_CANT = 300; |
||
78 | const CODE_COMMS = 400; |
||
79 | const CODE_CLOSE = 500; |
||
80 | |||
81 | const READ_CHUNK_SIZE = 1024; |
||
82 | // varnish default, can only be changed at Varnish startup time |
||
83 | // if data to write is over this limit the actual run-time limit is checked |
||
84 | // and used |
||
85 | const CLI_CMD_LENGTH_LIMIT = 16384; |
||
86 | |||
87 | /** |
||
88 | * Regexp to detect the varnish version number |
||
89 | * @var string |
||
90 | */ |
||
91 | const REGEXP_VARNISH_VERSION = '/^varnish\-(?P<vmajor>\d+)\.(?P<vminor>\d+)\.(?P<vsub>\d+) revision (?P<vhash>[0-9a-f]+)$/'; |
||
92 | |||
93 | /** |
||
94 | * VCL config versions, should match config select values |
||
95 | */ |
||
96 | static protected $_VERSIONS = array('2.1', '3.0', '4.0', '4.1'); |
||
97 | |||
98 | /** |
||
99 | * Varnish socket connection |
||
100 | * |
||
101 | * @var resource |
||
102 | */ |
||
103 | protected $_varnishConn = null; |
||
104 | protected $_host = '127.0.0.1'; |
||
105 | protected $_port = 6082; |
||
106 | protected $_private = null; |
||
107 | protected $_authSecret = null; |
||
108 | protected $_timeout = 5; |
||
109 | protected $_version = null; //auto-detect |
||
110 | |||
111 | public function __construct(array $options = array()) { |
||
134 | |||
135 | /** |
||
136 | * Provide simple Varnish methods |
||
137 | * |
||
138 | * Methods provided: |
||
139 | help [command] |
||
140 | ping [timestamp] |
||
141 | auth response |
||
142 | banner |
||
143 | stats |
||
144 | vcl.load <configname> <filename> |
||
145 | vcl.inline <configname> <quoted_VCLstring> |
||
146 | vcl.use <configname> |
||
147 | vcl.discard <configname> |
||
148 | vcl.list |
||
149 | vcl.show <configname> |
||
150 | param.show [-l] [<param>] |
||
151 | param.set <param> <value> |
||
152 | purge.url <regexp> |
||
153 | purge <field> <operator> <arg> [&& <field> <oper> <arg>]... |
||
154 | purge.list |
||
155 | * |
||
156 | * @param string $name method name |
||
157 | * @param array $args method args |
||
158 | * @return array |
||
159 | */ |
||
160 | public function __call($name, $args) { |
||
165 | |||
166 | /** |
||
167 | * Get the connection string for this socket (<host>:<port>) |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getConnectionString() { |
||
174 | |||
175 | /** |
||
176 | * Get the set host for this instance |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getHost() { |
||
183 | |||
184 | /** |
||
185 | * Set the Varnish host name/ip to connect to |
||
186 | * |
||
187 | * @param string $host hostname or ip |
||
188 | */ |
||
189 | public function setHost($host) { |
||
194 | |||
195 | /** |
||
196 | * Get the port set for this instance |
||
197 | * |
||
198 | * @return int |
||
199 | */ |
||
200 | public function getPort() { |
||
203 | |||
204 | /** |
||
205 | * Set the Varnish admin port |
||
206 | * |
||
207 | * @param int $port |
||
208 | */ |
||
209 | public function setPort($port) { |
||
214 | |||
215 | /** |
||
216 | * Set the Varnish admin auth secret, use null to indicate there isn't one |
||
217 | * |
||
218 | * @param string $authSecret |
||
219 | */ |
||
220 | public function setAuthSecret($authSecret = null) { |
||
224 | |||
225 | /** |
||
226 | * Set the timeout to connect to the varnish instance |
||
227 | * |
||
228 | * @param int $timeout |
||
229 | */ |
||
230 | public function setTimeout($timeout) { |
||
237 | |||
238 | /** |
||
239 | * Explicitly set the version of the varnish instance we're connecting to |
||
240 | * |
||
241 | * @param string $version version from $_VERSIONS |
||
242 | */ |
||
243 | public function setVersion($version) { |
||
250 | |||
251 | /** |
||
252 | * Check if we're connected to Varnish |
||
253 | * |
||
254 | * @return boolean |
||
255 | */ |
||
256 | public function isConnected() { |
||
259 | |||
260 | /** |
||
261 | * Find out what version mode we're running in |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getVersion() { |
||
271 | |||
272 | /** |
||
273 | * Stop the Varnish instance |
||
274 | */ |
||
275 | public function quit() { |
||
279 | |||
280 | /** |
||
281 | * Check if Varnish has a child running or not |
||
282 | * |
||
283 | * @return boolean |
||
284 | */ |
||
285 | public function status() { |
||
293 | |||
294 | /** |
||
295 | * Stop the running child (if it is running) |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function stop() { |
||
305 | |||
306 | /** |
||
307 | * Start running the Varnish child |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | public function start() { |
||
315 | |||
316 | /** |
||
317 | * Establish a connection to the configured Varnish instance |
||
318 | * |
||
319 | * @return boolean |
||
320 | */ |
||
321 | protected function _connect() { |
||
354 | |||
355 | /** |
||
356 | * @param string $bannerText |
||
357 | */ |
||
358 | protected function _determineVersion($bannerText) { |
||
379 | |||
380 | /** |
||
381 | * Close the connection (if we're connected) |
||
382 | * |
||
383 | * @return $this |
||
384 | */ |
||
385 | protected function _close() { |
||
392 | |||
393 | /** |
||
394 | * Write data to the Varnish instance, a newline is automatically appended |
||
395 | * |
||
396 | * @param string $data data to write |
||
397 | * @return $this |
||
398 | */ |
||
399 | protected function _write($data) { |
||
435 | |||
436 | /** |
||
437 | * Read a response from Varnish instance |
||
438 | * |
||
439 | * @return array tuple of the response (code, text) |
||
440 | */ |
||
441 | protected function _read() { |
||
471 | |||
472 | /** |
||
473 | * [_command description] |
||
474 | * @param string $verb command name |
||
475 | * @param integer $okCode code that indicates command was successful |
||
476 | * @param string ... command args |
||
477 | * @return array |
||
478 | */ |
||
479 | protected function _command($verb, $okCode = 200) { |
||
508 | |||
509 | /** |
||
510 | * Handle v2.1 <> v3.0 command compatibility |
||
511 | * |
||
512 | * @param string $verb command to check |
||
513 | * @return string |
||
514 | */ |
||
515 | protected function _translateCommandMethod($verb) { |
||
532 | } |
||
533 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.