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_NORMAL = 'normal'; |
||
27 | |||
28 | const STATUS_ADDED = 'added'; |
||
29 | |||
30 | const STATUS_CONFLICTED = 'conflicted'; |
||
31 | |||
32 | const STATUS_UNVERSIONED = 'unversioned'; |
||
33 | |||
34 | const STATUS_NONE = 'none'; |
||
35 | |||
36 | const URL_REGEXP = '#([\w]*)://([^/@\s\']+@)?([^/@:\s\']+)(:\d+)?([^@\s\']*)?#'; |
||
37 | |||
38 | const SVN_INFO_CACHE_DURATION = '1 year'; |
||
39 | |||
40 | /** |
||
41 | * Reference to configuration. |
||
42 | * |
||
43 | * @var ConfigEditor |
||
44 | */ |
||
45 | private $_configEditor; |
||
46 | |||
47 | /** |
||
48 | * Process factory. |
||
49 | * |
||
50 | * @var IProcessFactory |
||
51 | */ |
||
52 | private $_processFactory; |
||
53 | |||
54 | /** |
||
55 | * Console IO. |
||
56 | * |
||
57 | * @var ConsoleIO |
||
58 | */ |
||
59 | private $_io; |
||
60 | |||
61 | /** |
||
62 | * Cache manager. |
||
63 | * |
||
64 | * @var CacheManager |
||
65 | */ |
||
66 | private $_cacheManager; |
||
67 | |||
68 | /** |
||
69 | * Path to an svn command. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $_svnCommand = 'svn'; |
||
74 | |||
75 | /** |
||
76 | * Cache duration for next invoked command. |
||
77 | * |
||
78 | * @var mixed |
||
79 | */ |
||
80 | private $_nextCommandCacheDuration = null; |
||
81 | |||
82 | /** |
||
83 | * Whatever to cache last repository revision or not. |
||
84 | * |
||
85 | * @var mixed |
||
86 | */ |
||
87 | private $_lastRevisionCacheDuration = null; |
||
88 | |||
89 | /** |
||
90 | * Creates repository connector. |
||
91 | * |
||
92 | * @param ConfigEditor $config_editor ConfigEditor. |
||
93 | * @param IProcessFactory $process_factory Process factory. |
||
94 | * @param ConsoleIO $io Console IO. |
||
95 | * @param CacheManager $cache_manager Cache manager. |
||
96 | */ |
||
97 | 78 | public function __construct( |
|
118 | |||
119 | /** |
||
120 | * Prepares static part of svn command to be used across the script. |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | 78 | protected function prepareSvnCommand() |
|
139 | |||
140 | /** |
||
141 | * Builds a command. |
||
142 | * |
||
143 | * @param string $sub_command Sub command. |
||
144 | * @param string|null $param_string Parameter string. |
||
145 | * |
||
146 | * @return Command |
||
147 | */ |
||
148 | 39 | public function getCommand($sub_command, $param_string = null) |
|
166 | |||
167 | /** |
||
168 | * Builds command from given arguments. |
||
169 | * |
||
170 | * @param string $sub_command Command. |
||
171 | * @param string $param_string Parameter string. |
||
172 | * |
||
173 | * @return string |
||
174 | * @throws \InvalidArgumentException When command contains spaces. |
||
175 | */ |
||
176 | 39 | protected function buildCommand($sub_command, $param_string = null) |
|
202 | |||
203 | /** |
||
204 | * Sets cache configuration for next created command. |
||
205 | * |
||
206 | * @param mixed $cache_duration Cache duration. |
||
207 | * |
||
208 | * @return self |
||
209 | */ |
||
210 | 20 | public function withCache($cache_duration) |
|
216 | |||
217 | /** |
||
218 | * Returns property value. |
||
219 | * |
||
220 | * @param string $name Property name. |
||
221 | * @param string $path_or_url Path to get property from. |
||
222 | * @param mixed $revision Revision. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
236 | |||
237 | /** |
||
238 | * Returns relative path of given path/url to the root of the repository. |
||
239 | * |
||
240 | * @param string $path_or_url Path or url. |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 3 | public function getRelativePath($path_or_url) |
|
255 | |||
256 | /** |
||
257 | * Returns repository root url from given path/url. |
||
258 | * |
||
259 | * @param string $path_or_url Path or url. |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | 3 | public function getRootUrl($path_or_url) |
|
267 | |||
268 | /** |
||
269 | * Determines if path is a root of the ref. |
||
270 | * |
||
271 | * @param string $path Path to a file. |
||
272 | * |
||
273 | * @return boolean |
||
274 | */ |
||
275 | 13 | public function isRefRoot($path) |
|
285 | |||
286 | /** |
||
287 | * Detects ref from given path. |
||
288 | * |
||
289 | * @param string $path Path to a file. |
||
290 | * |
||
291 | * @return string|boolean |
||
292 | * @see getProjectUrl |
||
293 | */ |
||
294 | 22 | public function getRefByPath($path) |
|
302 | |||
303 | /** |
||
304 | * Returns URL of the working copy. |
||
305 | * |
||
306 | * @param string $wc_path Working copy path. |
||
307 | * |
||
308 | * @return string |
||
309 | * @throws RepositoryCommandException When repository command failed to execute. |
||
310 | */ |
||
311 | 8 | public function getWorkingCopyUrl($wc_path) |
|
338 | |||
339 | /** |
||
340 | * Returns last changed revision on path/url. |
||
341 | * |
||
342 | * @param string $path_or_url Path or url. |
||
343 | * |
||
344 | * @return integer |
||
345 | */ |
||
346 | 7 | public function getLastRevision($path_or_url) |
|
353 | |||
354 | /** |
||
355 | * Determines if given path is in fact an url. |
||
356 | * |
||
357 | * @param string $path Path. |
||
358 | * |
||
359 | * @return boolean |
||
360 | */ |
||
361 | 24 | public function isUrl($path) |
|
365 | |||
366 | /** |
||
367 | * Removes credentials from url. |
||
368 | * |
||
369 | * @param string $url URL. |
||
370 | * |
||
371 | * @return string |
||
372 | * @throws \InvalidArgumentException When non-url given. |
||
373 | */ |
||
374 | 15 | public function removeCredentials($url) |
|
382 | |||
383 | /** |
||
384 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
385 | * |
||
386 | * @param string $repository_url Repository url. |
||
387 | * |
||
388 | * @return string |
||
389 | * @see getRefByPath |
||
390 | */ |
||
391 | 9 | public function getProjectUrl($repository_url) |
|
400 | |||
401 | /** |
||
402 | * Returns "svn info" entry for path or url. |
||
403 | * |
||
404 | * @param string $path_or_url Path or url. |
||
405 | * @param mixed $cache_duration Cache duration. |
||
406 | * |
||
407 | * @return \SimpleXMLElement |
||
408 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
409 | */ |
||
410 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
439 | |||
440 | /** |
||
441 | * Returns path of "svn info" entry. |
||
442 | * |
||
443 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
459 | |||
460 | /** |
||
461 | * Returns revision, when path was added to repository. |
||
462 | * |
||
463 | * @param string $url Url. |
||
464 | * |
||
465 | * @return integer |
||
466 | * @throws \InvalidArgumentException When not an url was given. |
||
467 | */ |
||
468 | public function getFirstRevision($url) |
||
478 | |||
479 | /** |
||
480 | * Returns conflicts in working copy. |
||
481 | * |
||
482 | * @param string $wc_path Working copy path. |
||
483 | * |
||
484 | * @return array |
||
485 | */ |
||
486 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
498 | |||
499 | /** |
||
500 | * Returns compact working copy status. |
||
501 | * |
||
502 | * @param string $wc_path Working copy path. |
||
503 | * @param string|null $changelist Changelist. |
||
504 | * @param boolean $with_unversioned With unversioned. |
||
505 | * |
||
506 | * @return string |
||
507 | */ |
||
508 | public function getCompactWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
||
521 | |||
522 | /** |
||
523 | * Returns short item status. |
||
524 | * |
||
525 | * @param string $status Status. |
||
526 | * |
||
527 | * @return string |
||
528 | * @throws \InvalidArgumentException When unknown status given. |
||
529 | */ |
||
530 | protected function getShortItemStatus($status) |
||
555 | |||
556 | /** |
||
557 | * Returns short item status. |
||
558 | * |
||
559 | * @param string $status Status. |
||
560 | * |
||
561 | * @return string |
||
562 | * @throws \InvalidArgumentException When unknown status given. |
||
563 | */ |
||
564 | protected function getShortPropertiesStatus($status) |
||
579 | |||
580 | /** |
||
581 | * Returns working copy status. |
||
582 | * |
||
583 | * @param string $wc_path Working copy path. |
||
584 | * @param string|null $changelist Changelist. |
||
585 | * @param boolean $with_unversioned With unversioned. |
||
586 | * |
||
587 | * @return array |
||
588 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
589 | */ |
||
590 | 5 | public function getWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
|
648 | |||
649 | /** |
||
650 | * Processes "entry" nodes from "svn status" command. |
||
651 | * |
||
652 | * @param string $wc_path Working copy path. |
||
653 | * @param \SimpleXMLElement $entries Entries. |
||
654 | * |
||
655 | * @return array |
||
656 | */ |
||
657 | 4 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
674 | |||
675 | /** |
||
676 | * Detects specific path status. |
||
677 | * |
||
678 | * @param array $status Path status. |
||
679 | * @param string $path_status Expected path status. |
||
680 | * |
||
681 | * @return boolean |
||
682 | */ |
||
683 | 4 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
701 | |||
702 | /** |
||
703 | * Returns parent paths from given paths. |
||
704 | * |
||
705 | * @param array $paths Paths. |
||
706 | * |
||
707 | * @return array |
||
708 | */ |
||
709 | 1 | protected function getParentPaths(array $paths) |
|
722 | |||
723 | /** |
||
724 | * Returns working copy changelists. |
||
725 | * |
||
726 | * @param string $wc_path Working copy path. |
||
727 | * |
||
728 | * @return array |
||
729 | */ |
||
730 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
743 | |||
744 | /** |
||
745 | * Determines if working copy contains mixed revisions. |
||
746 | * |
||
747 | * @param string $wc_path Working copy path. |
||
748 | * |
||
749 | * @return array |
||
750 | */ |
||
751 | public function isMixedRevisionWorkingCopy($wc_path) |
||
773 | |||
774 | /** |
||
775 | * Determines if there is a working copy on a given path. |
||
776 | * |
||
777 | * @param string $path Path. |
||
778 | * |
||
779 | * @return boolean |
||
780 | * @throws \InvalidArgumentException When path isn't found. |
||
781 | * @throws RepositoryCommandException When repository command failed to execute. |
||
782 | */ |
||
783 | public function isWorkingCopy($path) |
||
802 | |||
803 | } |
||
804 |