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 | 86 | public function __construct( |
|
133 | |||
134 | /** |
||
135 | * Prepares static part of svn command to be used across the script. |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | 86 | 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 | 43 | 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 | 43 | 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 | */ |
||
241 | 4 | public function getProperty($name, $path_or_url, $revision = null) |
|
251 | |||
252 | /** |
||
253 | * Returns relative path of given path/url to the root of the repository. |
||
254 | * |
||
255 | * @param string $path_or_url Path or url. |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 3 | public function getRelativePath($path_or_url) |
|
270 | |||
271 | /** |
||
272 | * Returns repository root url from given path/url. |
||
273 | * |
||
274 | * @param string $path_or_url Path or url. |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 3 | public function getRootUrl($path_or_url) |
|
282 | |||
283 | /** |
||
284 | * Determines if path is a root of the ref. |
||
285 | * |
||
286 | * @param string $path Path to a file. |
||
287 | * |
||
288 | * @return boolean |
||
289 | */ |
||
290 | 13 | public function isRefRoot($path) |
|
300 | |||
301 | /** |
||
302 | * Detects ref from given path. |
||
303 | * |
||
304 | * @param string $path Path to a file. |
||
305 | * |
||
306 | * @return string|boolean |
||
307 | * @see getProjectUrl |
||
308 | */ |
||
309 | 22 | public function getRefByPath($path) |
|
317 | |||
318 | /** |
||
319 | * Returns URL of the working copy. |
||
320 | * |
||
321 | * @param string $wc_path Working copy path. |
||
322 | * |
||
323 | * @return string |
||
324 | * @throws RepositoryCommandException When repository command failed to execute. |
||
325 | */ |
||
326 | 8 | public function getWorkingCopyUrl($wc_path) |
|
353 | |||
354 | /** |
||
355 | * Returns last changed revision on path/url. |
||
356 | * |
||
357 | * @param string $path_or_url Path or url. |
||
358 | * |
||
359 | * @return integer |
||
360 | */ |
||
361 | 7 | public function getLastRevision($path_or_url) |
|
368 | |||
369 | /** |
||
370 | * Determines if given path is in fact an url. |
||
371 | * |
||
372 | * @param string $path Path. |
||
373 | * |
||
374 | * @return boolean |
||
375 | */ |
||
376 | 24 | public function isUrl($path) |
|
380 | |||
381 | /** |
||
382 | * Removes credentials from url. |
||
383 | * |
||
384 | * @param string $url URL. |
||
385 | * |
||
386 | * @return string |
||
387 | * @throws \InvalidArgumentException When non-url given. |
||
388 | */ |
||
389 | 15 | public function removeCredentials($url) |
|
397 | |||
398 | /** |
||
399 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
400 | * |
||
401 | * @param string $repository_url Repository url. |
||
402 | * |
||
403 | * @return string |
||
404 | * @see getRefByPath |
||
405 | */ |
||
406 | 9 | public function getProjectUrl($repository_url) |
|
415 | |||
416 | /** |
||
417 | * Returns "svn info" entry for path or url. |
||
418 | * |
||
419 | * @param string $path_or_url Path or url. |
||
420 | * @param mixed $cache_duration Cache duration. |
||
421 | * |
||
422 | * @return \SimpleXMLElement |
||
423 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
424 | */ |
||
425 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
454 | |||
455 | /** |
||
456 | * Returns path of "svn info" entry. |
||
457 | * |
||
458 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
474 | |||
475 | /** |
||
476 | * Returns revision, when path was added to repository. |
||
477 | * |
||
478 | * @param string $url Url. |
||
479 | * |
||
480 | * @return integer |
||
481 | * @throws \InvalidArgumentException When not an url was given. |
||
482 | */ |
||
483 | public function getFirstRevision($url) |
||
493 | |||
494 | /** |
||
495 | * Returns conflicts in working copy. |
||
496 | * |
||
497 | * @param string $wc_path Working copy path. |
||
498 | * |
||
499 | * @return array |
||
500 | */ |
||
501 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
513 | |||
514 | /** |
||
515 | * Returns missing paths in working copy. |
||
516 | * |
||
517 | * @param string $wc_path Working copy path. |
||
518 | * |
||
519 | * @return array |
||
520 | */ |
||
521 | 2 | public function getWorkingCopyMissing($wc_path) |
|
533 | |||
534 | /** |
||
535 | * Returns compact working copy status. |
||
536 | * |
||
537 | * @param string $wc_path Working copy path. |
||
538 | * @param string|null $changelist Changelist. |
||
539 | * @param boolean $with_unversioned With unversioned. |
||
540 | * |
||
541 | * @return string |
||
542 | */ |
||
543 | public function getCompactWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
||
556 | |||
557 | /** |
||
558 | * Returns short item status. |
||
559 | * |
||
560 | * @param string $status Status. |
||
561 | * |
||
562 | * @return string |
||
563 | * @throws \InvalidArgumentException When unknown status given. |
||
564 | */ |
||
565 | protected function getShortItemStatus($status) |
||
590 | |||
591 | /** |
||
592 | * Returns short item status. |
||
593 | * |
||
594 | * @param string $status Status. |
||
595 | * |
||
596 | * @return string |
||
597 | * @throws \InvalidArgumentException When unknown status given. |
||
598 | */ |
||
599 | protected function getShortPropertiesStatus($status) |
||
614 | |||
615 | /** |
||
616 | * Returns working copy status. |
||
617 | * |
||
618 | * @param string $wc_path Working copy path. |
||
619 | * @param string|null $changelist Changelist. |
||
620 | * @param boolean $with_unversioned With unversioned. |
||
621 | * |
||
622 | * @return array |
||
623 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
624 | */ |
||
625 | 7 | public function getWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
|
683 | |||
684 | /** |
||
685 | * Processes "entry" nodes from "svn status" command. |
||
686 | * |
||
687 | * @param string $wc_path Working copy path. |
||
688 | * @param \SimpleXMLElement $entries Entries. |
||
689 | * |
||
690 | * @return array |
||
691 | */ |
||
692 | 5 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
709 | |||
710 | /** |
||
711 | * Detects specific path status. |
||
712 | * |
||
713 | * @param array $status Path status. |
||
714 | * @param string $path_status Expected path status. |
||
715 | * |
||
716 | * @return boolean |
||
717 | */ |
||
718 | 5 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
736 | |||
737 | /** |
||
738 | * Returns parent paths from given paths. |
||
739 | * |
||
740 | * @param array $paths Paths. |
||
741 | * |
||
742 | * @return array |
||
743 | */ |
||
744 | 1 | protected function getParentPaths(array $paths) |
|
757 | |||
758 | /** |
||
759 | * Returns working copy changelists. |
||
760 | * |
||
761 | * @param string $wc_path Working copy path. |
||
762 | * |
||
763 | * @return array |
||
764 | */ |
||
765 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
778 | |||
779 | /** |
||
780 | * Returns revisions of paths in a working copy. |
||
781 | * |
||
782 | * @param string $wc_path Working copy path. |
||
783 | * |
||
784 | * @return array |
||
785 | */ |
||
786 | public function getWorkingCopyRevisions($wc_path) |
||
808 | |||
809 | /** |
||
810 | * Determines if there is a working copy on a given path. |
||
811 | * |
||
812 | * @param string $path Path. |
||
813 | * |
||
814 | * @return boolean |
||
815 | * @throws \InvalidArgumentException When path isn't found. |
||
816 | * @throws RepositoryCommandException When repository command failed to execute. |
||
817 | */ |
||
818 | public function isWorkingCopy($path) |
||
837 | |||
838 | /** |
||
839 | * Returns list of just merged revisions. |
||
840 | * |
||
841 | * @param string $wc_path Working copy path, where merge happens. |
||
842 | * |
||
843 | * @return array |
||
844 | */ |
||
845 | 2 | public function getFreshMergedRevisions($wc_path) |
|
878 | |||
879 | /** |
||
880 | * Returns list of merged revisions per path. |
||
881 | * |
||
882 | * @param string $wc_path Merge target: working copy path. |
||
883 | * @param integer $revision Revision. |
||
884 | * |
||
885 | * @return array |
||
886 | */ |
||
887 | 2 | protected function getMergedRevisions($wc_path, $revision = null) |
|
901 | |||
902 | } |
||
903 |
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.