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 | const SVN_CAT_CACHE_DURATION = '1 month'; |
||
46 | |||
47 | /** |
||
48 | * Reference to configuration. |
||
49 | * |
||
50 | * @var ConfigEditor |
||
51 | */ |
||
52 | private $_configEditor; |
||
53 | |||
54 | /** |
||
55 | * Process factory. |
||
56 | * |
||
57 | * @var IProcessFactory |
||
58 | */ |
||
59 | private $_processFactory; |
||
60 | |||
61 | /** |
||
62 | * Console IO. |
||
63 | * |
||
64 | * @var ConsoleIO |
||
65 | */ |
||
66 | private $_io; |
||
67 | |||
68 | /** |
||
69 | * Cache manager. |
||
70 | * |
||
71 | * @var CacheManager |
||
72 | */ |
||
73 | private $_cacheManager; |
||
74 | |||
75 | /** |
||
76 | * Revision list parser. |
||
77 | * |
||
78 | * @var RevisionListParser |
||
79 | */ |
||
80 | private $_revisionListParser; |
||
81 | |||
82 | /** |
||
83 | * Path to an svn command. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $_svnCommand = 'svn'; |
||
88 | |||
89 | /** |
||
90 | * Cache duration for next invoked command. |
||
91 | * |
||
92 | * @var mixed |
||
93 | */ |
||
94 | private $_nextCommandCacheDuration = null; |
||
95 | |||
96 | /** |
||
97 | * Whatever to cache last repository revision or not. |
||
98 | * |
||
99 | * @var mixed |
||
100 | */ |
||
101 | private $_lastRevisionCacheDuration = null; |
||
102 | |||
103 | /** |
||
104 | * Creates repository connector. |
||
105 | * |
||
106 | * @param ConfigEditor $config_editor ConfigEditor. |
||
107 | * @param IProcessFactory $process_factory Process factory. |
||
108 | * @param ConsoleIO $io Console IO. |
||
109 | * @param CacheManager $cache_manager Cache manager. |
||
110 | * @param RevisionListParser $revision_list_parser Revision list parser. |
||
111 | */ |
||
112 | 102 | public function __construct( |
|
135 | |||
136 | /** |
||
137 | * Prepares static part of svn command to be used across the script. |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 102 | protected function prepareSvnCommand() |
|
156 | |||
157 | /** |
||
158 | * Builds a command. |
||
159 | * |
||
160 | * @param string $sub_command Sub command. |
||
161 | * @param string|null $param_string Parameter string. |
||
162 | * |
||
163 | * @return Command |
||
164 | */ |
||
165 | 56 | public function getCommand($sub_command, $param_string = null) |
|
183 | |||
184 | /** |
||
185 | * Builds command from given arguments. |
||
186 | * |
||
187 | * @param string $sub_command Command. |
||
188 | * @param string $param_string Parameter string. |
||
189 | * |
||
190 | * @return string |
||
191 | * @throws \InvalidArgumentException When command contains spaces. |
||
192 | */ |
||
193 | 56 | protected function buildCommand($sub_command, $param_string = null) |
|
219 | |||
220 | /** |
||
221 | * Sets cache configuration for next created command. |
||
222 | * |
||
223 | * @param mixed $cache_duration Cache duration. |
||
224 | * |
||
225 | * @return self |
||
226 | */ |
||
227 | 26 | public function withCache($cache_duration) |
|
233 | |||
234 | /** |
||
235 | * Returns property value. |
||
236 | * |
||
237 | * @param string $name Property name. |
||
238 | * @param string $path_or_url Path to get property from. |
||
239 | * @param mixed $revision Revision. |
||
240 | * |
||
241 | * @return string |
||
242 | * @throws RepositoryCommandException When other, then missing property exception happens. |
||
243 | */ |
||
244 | 8 | public function getProperty($name, $path_or_url, $revision = null) |
|
267 | |||
268 | /** |
||
269 | * Returns relative path of given path/url to the root of the repository. |
||
270 | * |
||
271 | * @param string $path_or_url Path or url. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | 3 | public function getRelativePath($path_or_url) |
|
286 | |||
287 | /** |
||
288 | * Returns repository root url from given path/url. |
||
289 | * |
||
290 | * @param string $path_or_url Path or url. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | 3 | public function getRootUrl($path_or_url) |
|
298 | |||
299 | /** |
||
300 | * Determines if path is a root of the ref. |
||
301 | * |
||
302 | * @param string $path Path to a file. |
||
303 | * |
||
304 | * @return boolean |
||
305 | */ |
||
306 | 13 | public function isRefRoot($path) |
|
316 | |||
317 | /** |
||
318 | * Detects ref from given path. |
||
319 | * |
||
320 | * @param string $path Path to a file. |
||
321 | * |
||
322 | * @return string|boolean |
||
323 | * @see getProjectUrl |
||
324 | */ |
||
325 | 22 | public function getRefByPath($path) |
|
333 | |||
334 | /** |
||
335 | * Returns URL of the working copy. |
||
336 | * |
||
337 | * @param string $wc_path Working copy path. |
||
338 | * |
||
339 | * @return string |
||
340 | * @throws RepositoryCommandException When repository command failed to execute. |
||
341 | */ |
||
342 | 11 | public function getWorkingCopyUrl($wc_path) |
|
370 | |||
371 | /** |
||
372 | * Returns last changed revision on path/url. |
||
373 | * |
||
374 | * @param string $path_or_url Path or url. |
||
375 | * |
||
376 | * @return integer |
||
377 | */ |
||
378 | 9 | public function getLastRevision($path_or_url) |
|
385 | |||
386 | /** |
||
387 | * Determines if given path is in fact an url. |
||
388 | * |
||
389 | * @param string $path Path. |
||
390 | * |
||
391 | * @return boolean |
||
392 | */ |
||
393 | 29 | public function isUrl($path) |
|
397 | |||
398 | /** |
||
399 | * Removes credentials from url. |
||
400 | * |
||
401 | * @param string $url URL. |
||
402 | * |
||
403 | * @return string |
||
404 | * @throws \InvalidArgumentException When non-url given. |
||
405 | */ |
||
406 | 17 | public function removeCredentials($url) |
|
414 | |||
415 | /** |
||
416 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
417 | * |
||
418 | * @param string $repository_url Repository url. |
||
419 | * |
||
420 | * @return string |
||
421 | * @see getRefByPath |
||
422 | */ |
||
423 | 9 | public function getProjectUrl($repository_url) |
|
432 | |||
433 | /** |
||
434 | * Returns "svn info" entry for path or url. |
||
435 | * |
||
436 | * @param string $path_or_url Path or url. |
||
437 | * @param mixed $cache_duration Cache duration. |
||
438 | * |
||
439 | * @return \SimpleXMLElement |
||
440 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
441 | */ |
||
442 | 24 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
481 | |||
482 | /** |
||
483 | * Returns revision, when path was added to repository. |
||
484 | * |
||
485 | * @param string $url Url. |
||
486 | * |
||
487 | * @return integer |
||
488 | * @throws \InvalidArgumentException When not an url was given. |
||
489 | */ |
||
490 | public function getFirstRevision($url) |
||
500 | |||
501 | /** |
||
502 | * Returns conflicts in working copy. |
||
503 | * |
||
504 | * @param string $wc_path Working copy path. |
||
505 | * |
||
506 | * @return array |
||
507 | */ |
||
508 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
520 | |||
521 | /** |
||
522 | * Returns missing paths in working copy. |
||
523 | * |
||
524 | * @param string $wc_path Working copy path. |
||
525 | * |
||
526 | * @return array |
||
527 | */ |
||
528 | 2 | public function getWorkingCopyMissing($wc_path) |
|
540 | |||
541 | /** |
||
542 | * Returns compact working copy status. |
||
543 | * |
||
544 | * @param string $wc_path Working copy path. |
||
545 | * @param string|null $changelist Changelist. |
||
546 | * @param array $except_statuses Except statuses. |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | public function getCompactWorkingCopyStatus( |
||
569 | |||
570 | /** |
||
571 | * Returns short item status. |
||
572 | * |
||
573 | * @param string $status Status. |
||
574 | * |
||
575 | * @return string |
||
576 | * @throws \InvalidArgumentException When unknown status given. |
||
577 | */ |
||
578 | protected function getShortItemStatus($status) |
||
603 | |||
604 | /** |
||
605 | * Returns short item status. |
||
606 | * |
||
607 | * @param string $status Status. |
||
608 | * |
||
609 | * @return string |
||
610 | * @throws \InvalidArgumentException When unknown status given. |
||
611 | */ |
||
612 | protected function getShortPropertiesStatus($status) |
||
627 | |||
628 | /** |
||
629 | * Returns working copy status. |
||
630 | * |
||
631 | * @param string $wc_path Working copy path. |
||
632 | * @param string|null $changelist Changelist. |
||
633 | * @param array $except_statuses Except statuses. |
||
634 | * |
||
635 | * @return array |
||
636 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
637 | */ |
||
638 | 10 | public function getWorkingCopyStatus( |
|
704 | |||
705 | /** |
||
706 | * Processes "entry" nodes from "svn status" command. |
||
707 | * |
||
708 | * @param string $wc_path Working copy path. |
||
709 | * @param \SimpleXMLElement $entries Entries. |
||
710 | * |
||
711 | * @return array |
||
712 | */ |
||
713 | 8 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
731 | |||
732 | /** |
||
733 | * Detects specific path status. |
||
734 | * |
||
735 | * @param array $status Path status. |
||
736 | * @param string $path_status Expected path status. |
||
737 | * |
||
738 | * @return boolean |
||
739 | */ |
||
740 | 8 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
760 | |||
761 | /** |
||
762 | * Returns parent paths from given paths. |
||
763 | * |
||
764 | * @param array $paths Paths. |
||
765 | * |
||
766 | * @return array |
||
767 | */ |
||
768 | 1 | protected function getParentPaths(array $paths) |
|
781 | |||
782 | /** |
||
783 | * Returns working copy changelists. |
||
784 | * |
||
785 | * @param string $wc_path Working copy path. |
||
786 | * |
||
787 | * @return array |
||
788 | */ |
||
789 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
802 | |||
803 | /** |
||
804 | * Returns revisions of paths in a working copy. |
||
805 | * |
||
806 | * @param string $wc_path Working copy path. |
||
807 | * |
||
808 | * @return array |
||
809 | */ |
||
810 | public function getWorkingCopyRevisions($wc_path) |
||
832 | |||
833 | /** |
||
834 | * Determines if there is a working copy on a given path. |
||
835 | * |
||
836 | * @param string $path Path. |
||
837 | * |
||
838 | * @return boolean |
||
839 | * @throws \InvalidArgumentException When path isn't found. |
||
840 | * @throws RepositoryCommandException When repository command failed to execute. |
||
841 | */ |
||
842 | public function isWorkingCopy($path) |
||
861 | |||
862 | /** |
||
863 | * Returns list of add/removed revisions from last merge operation. |
||
864 | * |
||
865 | * @param string $wc_path Working copy path, where merge happens. |
||
866 | * @param boolean $regular_or_reverse Merge direction ("regular" or "reverse"). |
||
867 | * |
||
868 | * @return array |
||
869 | */ |
||
870 | 4 | public function getMergedRevisionChanges($wc_path, $regular_or_reverse) |
|
910 | |||
911 | /** |
||
912 | * Returns list of merged revisions per path. |
||
913 | * |
||
914 | * @param string $wc_path Merge target: working copy path. |
||
915 | * @param integer $revision Revision. |
||
916 | * |
||
917 | * @return array |
||
918 | */ |
||
919 | 4 | protected function getMergedRevisions($wc_path, $revision = null) |
|
933 | |||
934 | /** |
||
935 | * Returns file contents at given revision. |
||
936 | * |
||
937 | * @param string $path_or_url Path or url. |
||
938 | * @param integer $revision Revision. |
||
939 | * |
||
940 | * @return string |
||
941 | */ |
||
942 | 1 | public function getFileContent($path_or_url, $revision) |
|
949 | |||
950 | } |
||
951 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.