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 |
||
24 | class Connector |
||
25 | { |
||
26 | |||
27 | const STATUS_NORMAL = 'normal'; |
||
28 | |||
29 | const STATUS_ADDED = 'added'; |
||
30 | |||
31 | const STATUS_CONFLICTED = 'conflicted'; |
||
32 | |||
33 | const STATUS_UNVERSIONED = 'unversioned'; |
||
34 | |||
35 | const STATUS_EXTERNAL = 'external'; |
||
36 | |||
37 | const STATUS_MISSING = 'missing'; |
||
38 | |||
39 | const STATUS_NONE = 'none'; |
||
40 | |||
41 | const URL_REGEXP = '#([\w]*)://([^/@\s\']+@)?([^/@:\s\']+)(:\d+)?([^@\s\']*)?#'; |
||
42 | |||
43 | const SVN_INFO_CACHE_DURATION = '1 year'; |
||
44 | |||
45 | /** |
||
46 | * Reference to configuration. |
||
47 | * |
||
48 | * @var ConfigEditor |
||
49 | */ |
||
50 | private $_configEditor; |
||
51 | |||
52 | /** |
||
53 | * Process factory. |
||
54 | * |
||
55 | * @var IProcessFactory |
||
56 | */ |
||
57 | private $_processFactory; |
||
58 | |||
59 | /** |
||
60 | * Console IO. |
||
61 | * |
||
62 | * @var ConsoleIO |
||
63 | */ |
||
64 | private $_io; |
||
65 | |||
66 | /** |
||
67 | * Cache manager. |
||
68 | * |
||
69 | * @var CacheManager |
||
70 | */ |
||
71 | private $_cacheManager; |
||
72 | |||
73 | /** |
||
74 | * Revision list parser. |
||
75 | * |
||
76 | * @var RevisionListParser |
||
77 | */ |
||
78 | private $_revisionListParser; |
||
79 | |||
80 | /** |
||
81 | * Path to an svn command. |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | private $_svnCommand = 'svn'; |
||
86 | |||
87 | /** |
||
88 | * Cache duration for next invoked command. |
||
89 | * |
||
90 | * @var mixed |
||
91 | */ |
||
92 | private $_nextCommandCacheDuration = null; |
||
93 | |||
94 | /** |
||
95 | * Whatever to cache last repository revision or not. |
||
96 | * |
||
97 | * @var mixed |
||
98 | */ |
||
99 | private $_lastRevisionCacheDuration = null; |
||
100 | |||
101 | /** |
||
102 | * Creates repository connector. |
||
103 | * |
||
104 | * @param ConfigEditor $config_editor ConfigEditor. |
||
105 | * @param IProcessFactory $process_factory Process factory. |
||
106 | * @param ConsoleIO $io Console IO. |
||
107 | * @param CacheManager $cache_manager Cache manager. |
||
108 | * @param RevisionListParser $revision_list_parser Revision list parser. |
||
109 | */ |
||
110 | 89 | public function __construct( |
|
133 | |||
134 | /** |
||
135 | * Prepares static part of svn command to be used across the script. |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | 89 | protected function prepareSvnCommand() |
|
154 | |||
155 | /** |
||
156 | * Builds a command. |
||
157 | * |
||
158 | * @param string $sub_command Sub command. |
||
159 | * @param string|null $param_string Parameter string. |
||
160 | * |
||
161 | * @return Command |
||
162 | */ |
||
163 | 45 | public function getCommand($sub_command, $param_string = null) |
|
181 | |||
182 | /** |
||
183 | * Builds command from given arguments. |
||
184 | * |
||
185 | * @param string $sub_command Command. |
||
186 | * @param string $param_string Parameter string. |
||
187 | * |
||
188 | * @return string |
||
189 | * @throws \InvalidArgumentException When command contains spaces. |
||
190 | */ |
||
191 | 45 | protected function buildCommand($sub_command, $param_string = null) |
|
217 | |||
218 | /** |
||
219 | * Sets cache configuration for next created command. |
||
220 | * |
||
221 | * @param mixed $cache_duration Cache duration. |
||
222 | * |
||
223 | * @return self |
||
224 | */ |
||
225 | 20 | public function withCache($cache_duration) |
|
231 | |||
232 | /** |
||
233 | * Returns property value. |
||
234 | * |
||
235 | * @param string $name Property name. |
||
236 | * @param string $path_or_url Path to get property from. |
||
237 | * @param mixed $revision Revision. |
||
238 | * |
||
239 | * @return string |
||
240 | * @throws RepositoryCommandException When other, then missing property exception happens. |
||
241 | */ |
||
242 | 6 | public function getProperty($name, $path_or_url, $revision = null) |
|
265 | |||
266 | /** |
||
267 | * Returns relative path of given path/url to the root of the repository. |
||
268 | * |
||
269 | * @param string $path_or_url Path or url. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | 3 | public function getRelativePath($path_or_url) |
|
284 | |||
285 | /** |
||
286 | * Returns repository root url from given path/url. |
||
287 | * |
||
288 | * @param string $path_or_url Path or url. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | 3 | public function getRootUrl($path_or_url) |
|
296 | |||
297 | /** |
||
298 | * Determines if path is a root of the ref. |
||
299 | * |
||
300 | * @param string $path Path to a file. |
||
301 | * |
||
302 | * @return boolean |
||
303 | */ |
||
304 | 13 | public function isRefRoot($path) |
|
314 | |||
315 | /** |
||
316 | * Detects ref from given path. |
||
317 | * |
||
318 | * @param string $path Path to a file. |
||
319 | * |
||
320 | * @return string|boolean |
||
321 | * @see getProjectUrl |
||
322 | */ |
||
323 | 22 | public function getRefByPath($path) |
|
331 | |||
332 | /** |
||
333 | * Returns URL of the working copy. |
||
334 | * |
||
335 | * @param string $wc_path Working copy path. |
||
336 | * |
||
337 | * @return string |
||
338 | * @throws RepositoryCommandException When repository command failed to execute. |
||
339 | */ |
||
340 | 8 | public function getWorkingCopyUrl($wc_path) |
|
367 | |||
368 | /** |
||
369 | * Returns last changed revision on path/url. |
||
370 | * |
||
371 | * @param string $path_or_url Path or url. |
||
372 | * |
||
373 | * @return integer |
||
374 | */ |
||
375 | 7 | public function getLastRevision($path_or_url) |
|
382 | |||
383 | /** |
||
384 | * Determines if given path is in fact an url. |
||
385 | * |
||
386 | * @param string $path Path. |
||
387 | * |
||
388 | * @return boolean |
||
389 | */ |
||
390 | 24 | public function isUrl($path) |
|
394 | |||
395 | /** |
||
396 | * Removes credentials from url. |
||
397 | * |
||
398 | * @param string $url URL. |
||
399 | * |
||
400 | * @return string |
||
401 | * @throws \InvalidArgumentException When non-url given. |
||
402 | */ |
||
403 | 15 | public function removeCredentials($url) |
|
411 | |||
412 | /** |
||
413 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
414 | * |
||
415 | * @param string $repository_url Repository url. |
||
416 | * |
||
417 | * @return string |
||
418 | * @see getRefByPath |
||
419 | */ |
||
420 | 9 | public function getProjectUrl($repository_url) |
|
429 | |||
430 | /** |
||
431 | * Returns "svn info" entry for path or url. |
||
432 | * |
||
433 | * @param string $path_or_url Path or url. |
||
434 | * @param mixed $cache_duration Cache duration. |
||
435 | * |
||
436 | * @return \SimpleXMLElement |
||
437 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
438 | */ |
||
439 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
468 | |||
469 | /** |
||
470 | * Returns path of "svn info" entry. |
||
471 | * |
||
472 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
488 | |||
489 | /** |
||
490 | * Returns revision, when path was added to repository. |
||
491 | * |
||
492 | * @param string $url Url. |
||
493 | * |
||
494 | * @return integer |
||
495 | * @throws \InvalidArgumentException When not an url was given. |
||
496 | */ |
||
497 | public function getFirstRevision($url) |
||
507 | |||
508 | /** |
||
509 | * Returns conflicts in working copy. |
||
510 | * |
||
511 | * @param string $wc_path Working copy path. |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
527 | |||
528 | /** |
||
529 | * Returns missing paths in working copy. |
||
530 | * |
||
531 | * @param string $wc_path Working copy path. |
||
532 | * |
||
533 | * @return array |
||
534 | */ |
||
535 | 2 | public function getWorkingCopyMissing($wc_path) |
|
547 | |||
548 | /** |
||
549 | * Returns compact working copy status. |
||
550 | * |
||
551 | * @param string $wc_path Working copy path. |
||
552 | * @param string|null $changelist Changelist. |
||
553 | * @param boolean $with_unversioned With unversioned. |
||
554 | * |
||
555 | * @return string |
||
556 | */ |
||
557 | public function getCompactWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
||
570 | |||
571 | /** |
||
572 | * Returns short item status. |
||
573 | * |
||
574 | * @param string $status Status. |
||
575 | * |
||
576 | * @return string |
||
577 | * @throws \InvalidArgumentException When unknown status given. |
||
578 | */ |
||
579 | protected function getShortItemStatus($status) |
||
604 | |||
605 | /** |
||
606 | * Returns short item status. |
||
607 | * |
||
608 | * @param string $status Status. |
||
609 | * |
||
610 | * @return string |
||
611 | * @throws \InvalidArgumentException When unknown status given. |
||
612 | */ |
||
613 | protected function getShortPropertiesStatus($status) |
||
628 | |||
629 | /** |
||
630 | * Returns working copy status. |
||
631 | * |
||
632 | * @param string $wc_path Working copy path. |
||
633 | * @param string|null $changelist Changelist. |
||
634 | * @param boolean $with_unversioned With unversioned. |
||
635 | * |
||
636 | * @return array |
||
637 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
638 | */ |
||
639 | 7 | public function getWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
|
697 | |||
698 | /** |
||
699 | * Processes "entry" nodes from "svn status" command. |
||
700 | * |
||
701 | * @param string $wc_path Working copy path. |
||
702 | * @param \SimpleXMLElement $entries Entries. |
||
703 | * |
||
704 | * @return array |
||
705 | */ |
||
706 | 5 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
723 | |||
724 | /** |
||
725 | * Detects specific path status. |
||
726 | * |
||
727 | * @param array $status Path status. |
||
728 | * @param string $path_status Expected path status. |
||
729 | * |
||
730 | * @return boolean |
||
731 | */ |
||
732 | 5 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
750 | |||
751 | /** |
||
752 | * Returns parent paths from given paths. |
||
753 | * |
||
754 | * @param array $paths Paths. |
||
755 | * |
||
756 | * @return array |
||
757 | */ |
||
758 | 1 | protected function getParentPaths(array $paths) |
|
771 | |||
772 | /** |
||
773 | * Returns working copy changelists. |
||
774 | * |
||
775 | * @param string $wc_path Working copy path. |
||
776 | * |
||
777 | * @return array |
||
778 | */ |
||
779 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
792 | |||
793 | /** |
||
794 | * Returns revisions of paths in a working copy. |
||
795 | * |
||
796 | * @param string $wc_path Working copy path. |
||
797 | * |
||
798 | * @return array |
||
799 | */ |
||
800 | public function getWorkingCopyRevisions($wc_path) |
||
822 | |||
823 | /** |
||
824 | * Determines if there is a working copy on a given path. |
||
825 | * |
||
826 | * @param string $path Path. |
||
827 | * |
||
828 | * @return boolean |
||
829 | * @throws \InvalidArgumentException When path isn't found. |
||
830 | * @throws RepositoryCommandException When repository command failed to execute. |
||
831 | */ |
||
832 | public function isWorkingCopy($path) |
||
851 | |||
852 | /** |
||
853 | * Returns list of just merged revisions. |
||
854 | * |
||
855 | * @param string $wc_path Working copy path, where merge happens. |
||
856 | * |
||
857 | * @return array |
||
858 | */ |
||
859 | 2 | public function getFreshMergedRevisions($wc_path) |
|
892 | |||
893 | /** |
||
894 | * Returns list of merged revisions per path. |
||
895 | * |
||
896 | * @param string $wc_path Merge target: working copy path. |
||
897 | * @param integer $revision Revision. |
||
898 | * |
||
899 | * @return array |
||
900 | */ |
||
901 | 2 | protected function getMergedRevisions($wc_path, $revision = null) |
|
915 | |||
916 | } |
||
917 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.