Complex classes like Connector 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 Connector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Connector |
||
24 | { |
||
25 | |||
26 | const STATUS_UNVERSIONED = 'unversioned'; |
||
27 | |||
28 | const URL_REGEXP = '#([\w]*)://([^/@\s\']+@)?([^/@:\s\']+)(:\d+)?([^@\s\']*)?#'; |
||
29 | |||
30 | /** |
||
31 | * Reference to configuration. |
||
32 | * |
||
33 | * @var ConfigEditor |
||
34 | */ |
||
35 | private $_configEditor; |
||
36 | |||
37 | /** |
||
38 | * Process factory. |
||
39 | * |
||
40 | * @var IProcessFactory |
||
41 | */ |
||
42 | private $_processFactory; |
||
43 | |||
44 | /** |
||
45 | * Console IO. |
||
46 | * |
||
47 | * @var ConsoleIO |
||
48 | */ |
||
49 | private $_io; |
||
50 | |||
51 | /** |
||
52 | * Cache manager. |
||
53 | * |
||
54 | * @var CacheManager |
||
55 | */ |
||
56 | private $_cacheManager; |
||
57 | |||
58 | /** |
||
59 | * Path to an svn command. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $_svnCommand = 'svn'; |
||
64 | |||
65 | /** |
||
66 | * Cache duration for next invoked command. |
||
67 | * |
||
68 | * @var mixed |
||
69 | */ |
||
70 | private $_nextCommandCacheDuration = null; |
||
71 | |||
72 | /** |
||
73 | * Whatever to cache last repository revision or not. |
||
74 | * |
||
75 | * @var mixed |
||
76 | */ |
||
77 | private $_lastRevisionCacheDuration = null; |
||
78 | |||
79 | /** |
||
80 | * Creates repository connector. |
||
81 | * |
||
82 | * @param ConfigEditor $config_editor ConfigEditor. |
||
83 | * @param IProcessFactory $process_factory Process factory. |
||
84 | * @param ConsoleIO $io Console IO. |
||
85 | * @param CacheManager $cache_manager Cache manager. |
||
86 | */ |
||
87 | 58 | public function __construct( |
|
108 | |||
109 | /** |
||
110 | * Prepares static part of svn command to be used across the script. |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | 58 | protected function prepareSvnCommand() |
|
129 | |||
130 | /** |
||
131 | * Builds a command. |
||
132 | * |
||
133 | * @param string $sub_command Sub command. |
||
134 | * @param string|null $param_string Parameter string. |
||
135 | * |
||
136 | * @return Command |
||
137 | */ |
||
138 | 32 | public function getCommand($sub_command, $param_string = null) |
|
155 | |||
156 | /** |
||
157 | * Builds command from given arguments. |
||
158 | * |
||
159 | * @param string $sub_command Command. |
||
160 | * @param string $param_string Parameter string. |
||
161 | * |
||
162 | * @return string |
||
163 | * @throws \InvalidArgumentException When command contains spaces. |
||
164 | */ |
||
165 | 32 | protected function buildCommand($sub_command, $param_string = null) |
|
191 | |||
192 | /** |
||
193 | * Sets cache configuration for next created command. |
||
194 | * |
||
195 | * @param mixed $cache_duration Cache duration. |
||
196 | * |
||
197 | * @return self |
||
198 | */ |
||
199 | 20 | public function withCache($cache_duration) |
|
205 | |||
206 | /** |
||
207 | * Returns property value. |
||
208 | * |
||
209 | * @param string $name Property name. |
||
210 | * @param string $path_or_url Path to get property from. |
||
211 | * @param mixed $revision Revision. |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
225 | |||
226 | /** |
||
227 | * Returns relative path of given path/url to the root of the repository. |
||
228 | * |
||
229 | * @param string $path_or_url Path or url. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | 3 | public function getRelativePath($path_or_url) |
|
240 | |||
241 | /** |
||
242 | * Returns repository root url from given path/url. |
||
243 | * |
||
244 | * @param string $path_or_url Path or url. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 6 | public function getRootUrl($path_or_url) |
|
252 | |||
253 | /** |
||
254 | * Determines if path is a root of the ref. |
||
255 | * |
||
256 | * @param string $path Path to a file. |
||
257 | * |
||
258 | * @return boolean |
||
259 | */ |
||
260 | public function isRefRoot($path) |
||
270 | |||
271 | /** |
||
272 | * Detects ref from given path. |
||
273 | * |
||
274 | * @param string $path Path to a file. |
||
275 | * |
||
276 | * @return string|boolean |
||
277 | * @see getProjectUrl |
||
278 | 8 | */ |
|
279 | public function getRefByPath($path) |
||
287 | 6 | ||
288 | 3 | /** |
|
289 | 2 | * Returns URL of the working copy. |
|
290 | * |
||
291 | 2 | * @param string $wc_path Working copy path. |
|
292 | * |
||
293 | 2 | * @return string |
|
294 | 1 | * @throws RepositoryCommandException When repository command failed to execute. |
|
295 | */ |
||
296 | 1 | public function getWorkingCopyUrl($wc_path) |
|
323 | |||
324 | /** |
||
325 | * Returns last changed revision on path/url. |
||
326 | * |
||
327 | * @param string $path_or_url Path or url. |
||
328 | 24 | * |
|
329 | * @return integer |
||
330 | 24 | */ |
|
331 | public function getLastRevision($path_or_url) |
||
338 | |||
339 | /** |
||
340 | * Determines if given path is in fact an url. |
||
341 | 15 | * |
|
342 | * @param string $path Path. |
||
343 | 15 | * |
|
344 | 1 | * @return boolean |
|
345 | */ |
||
346 | public function isUrl($path) |
||
350 | |||
351 | /** |
||
352 | * Removes credentials from url. |
||
353 | * |
||
354 | * @param string $url URL. |
||
355 | * |
||
356 | * @return string |
||
357 | * @throws \InvalidArgumentException When non-url given. |
||
358 | 9 | */ |
|
359 | public function removeCredentials($url) |
||
367 | |||
368 | /** |
||
369 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
370 | * |
||
371 | * @param string $repository_url Repository url. |
||
372 | * |
||
373 | * @return string |
||
374 | * @see getRefByPath |
||
375 | */ |
||
376 | public function getProjectUrl($repository_url) |
||
385 | 19 | ||
386 | 10 | /** |
|
387 | 10 | * Returns "svn info" entry for path or url. |
|
388 | * |
||
389 | * @param string $path_or_url Path or url. |
||
390 | 19 | * @param mixed $cache_duration Cache duration. |
|
391 | * |
||
392 | * @return \SimpleXMLElement |
||
393 | 17 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
|
394 | */ |
||
395 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
||
424 | 16 | ||
425 | /** |
||
426 | * Returns path of "svn info" entry. |
||
427 | * |
||
428 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
||
444 | |||
445 | /** |
||
446 | * Returns revision, when path was added to repository. |
||
447 | * |
||
448 | * @param string $url Url. |
||
449 | * |
||
450 | * @return integer |
||
451 | * @throws \InvalidArgumentException When not an url was given. |
||
452 | */ |
||
453 | public function getFirstRevision($url) |
||
463 | |||
464 | /** |
||
465 | * Returns conflicts in working copy. |
||
466 | * |
||
467 | * @param string $wc_path Working copy path. |
||
468 | * |
||
469 | * @return array |
||
470 | */ |
||
471 | public function getWorkingCopyConflicts($wc_path) |
||
483 | |||
484 | /** |
||
485 | * Returns compact working copy status. |
||
486 | * |
||
487 | * @param string $wc_path Working copy path. |
||
488 | * @param boolean $with_unversioned With unversioned. |
||
489 | * |
||
490 | * @return string |
||
491 | */ |
||
492 | public function getCompactWorkingCopyStatus($wc_path, $with_unversioned = true) |
||
509 | |||
510 | /** |
||
511 | * Returns short item status. |
||
512 | * |
||
513 | * @param string $status Status. |
||
514 | * |
||
515 | * @return string |
||
516 | * @throws \InvalidArgumentException When unknown status given. |
||
517 | */ |
||
518 | protected function getShortItemStatus($status) |
||
543 | |||
544 | /** |
||
545 | * Returns short item status. |
||
546 | * |
||
547 | * @param string $status Status. |
||
548 | * |
||
549 | * @return string |
||
550 | * @throws \InvalidArgumentException When unknown status given. |
||
551 | */ |
||
552 | protected function getShortPropertiesStatus($status) |
||
567 | |||
568 | /** |
||
569 | * Returns working copy status. |
||
570 | * |
||
571 | * @param string $wc_path Working copy path. |
||
572 | * |
||
573 | * @return array |
||
574 | */ |
||
575 | public function getWorkingCopyStatus($wc_path) |
||
605 | |||
606 | /** |
||
607 | * Determines if working copy contains mixed revisions. |
||
608 | * |
||
609 | * @param string $wc_path Working copy path. |
||
610 | * |
||
611 | * @return array |
||
612 | */ |
||
613 | public function isMixedRevisionWorkingCopy($wc_path) |
||
635 | |||
636 | /** |
||
637 | * Determines if there is a working copy on a given path. |
||
638 | * |
||
639 | * @param string $path Path. |
||
640 | * |
||
641 | * @return boolean |
||
642 | * @throws \InvalidArgumentException When path isn't found. |
||
643 | * @throws RepositoryCommandException When repository command failed to execute. |
||
644 | */ |
||
645 | public function isWorkingCopy($path) |
||
664 | |||
665 | } |
||
666 |