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_EXTERNAL = 'external'; |
||
35 | |||
36 | const STATUS_MISSING = 'missing'; |
||
37 | |||
38 | const STATUS_NONE = 'none'; |
||
39 | |||
40 | const URL_REGEXP = '#([\w]*)://([^/@\s\']+@)?([^/@:\s\']+)(:\d+)?([^@\s\']*)?#'; |
||
41 | |||
42 | const SVN_INFO_CACHE_DURATION = '1 year'; |
||
43 | |||
44 | /** |
||
45 | * Reference to configuration. |
||
46 | * |
||
47 | * @var ConfigEditor |
||
48 | */ |
||
49 | private $_configEditor; |
||
50 | |||
51 | /** |
||
52 | * Process factory. |
||
53 | * |
||
54 | * @var IProcessFactory |
||
55 | */ |
||
56 | private $_processFactory; |
||
57 | |||
58 | /** |
||
59 | * Console IO. |
||
60 | * |
||
61 | * @var ConsoleIO |
||
62 | */ |
||
63 | private $_io; |
||
64 | |||
65 | /** |
||
66 | * Cache manager. |
||
67 | * |
||
68 | * @var CacheManager |
||
69 | */ |
||
70 | private $_cacheManager; |
||
71 | |||
72 | /** |
||
73 | * Path to an svn command. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | private $_svnCommand = 'svn'; |
||
78 | |||
79 | /** |
||
80 | * Cache duration for next invoked command. |
||
81 | * |
||
82 | * @var mixed |
||
83 | */ |
||
84 | private $_nextCommandCacheDuration = null; |
||
85 | |||
86 | /** |
||
87 | * Whatever to cache last repository revision or not. |
||
88 | * |
||
89 | * @var mixed |
||
90 | */ |
||
91 | private $_lastRevisionCacheDuration = null; |
||
92 | |||
93 | /** |
||
94 | * Creates repository connector. |
||
95 | * |
||
96 | * @param ConfigEditor $config_editor ConfigEditor. |
||
97 | * @param IProcessFactory $process_factory Process factory. |
||
98 | * @param ConsoleIO $io Console IO. |
||
99 | * @param CacheManager $cache_manager Cache manager. |
||
100 | */ |
||
101 | 84 | public function __construct( |
|
122 | |||
123 | /** |
||
124 | * Prepares static part of svn command to be used across the script. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | 84 | protected function prepareSvnCommand() |
|
143 | |||
144 | /** |
||
145 | * Builds a command. |
||
146 | * |
||
147 | * @param string $sub_command Sub command. |
||
148 | * @param string|null $param_string Parameter string. |
||
149 | * |
||
150 | * @return Command |
||
151 | */ |
||
152 | 41 | public function getCommand($sub_command, $param_string = null) |
|
170 | |||
171 | /** |
||
172 | * Builds command from given arguments. |
||
173 | * |
||
174 | * @param string $sub_command Command. |
||
175 | * @param string $param_string Parameter string. |
||
176 | * |
||
177 | * @return string |
||
178 | * @throws \InvalidArgumentException When command contains spaces. |
||
179 | */ |
||
180 | 41 | protected function buildCommand($sub_command, $param_string = null) |
|
206 | |||
207 | /** |
||
208 | * Sets cache configuration for next created command. |
||
209 | * |
||
210 | * @param mixed $cache_duration Cache duration. |
||
211 | * |
||
212 | * @return self |
||
213 | */ |
||
214 | 20 | public function withCache($cache_duration) |
|
220 | |||
221 | /** |
||
222 | * Returns property value. |
||
223 | * |
||
224 | * @param string $name Property name. |
||
225 | * @param string $path_or_url Path to get property from. |
||
226 | * @param mixed $revision Revision. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
240 | |||
241 | /** |
||
242 | * Returns relative path of given path/url to the root of the repository. |
||
243 | * |
||
244 | * @param string $path_or_url Path or url. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 3 | public function getRelativePath($path_or_url) |
|
259 | |||
260 | /** |
||
261 | * Returns repository root url from given path/url. |
||
262 | * |
||
263 | * @param string $path_or_url Path or url. |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | 3 | public function getRootUrl($path_or_url) |
|
271 | |||
272 | /** |
||
273 | * Determines if path is a root of the ref. |
||
274 | * |
||
275 | * @param string $path Path to a file. |
||
276 | * |
||
277 | * @return boolean |
||
278 | */ |
||
279 | 13 | public function isRefRoot($path) |
|
289 | |||
290 | /** |
||
291 | * Detects ref from given path. |
||
292 | * |
||
293 | * @param string $path Path to a file. |
||
294 | * |
||
295 | * @return string|boolean |
||
296 | * @see getProjectUrl |
||
297 | */ |
||
298 | 22 | public function getRefByPath($path) |
|
306 | |||
307 | /** |
||
308 | * Returns URL of the working copy. |
||
309 | * |
||
310 | * @param string $wc_path Working copy path. |
||
311 | * |
||
312 | * @return string |
||
313 | * @throws RepositoryCommandException When repository command failed to execute. |
||
314 | */ |
||
315 | 8 | public function getWorkingCopyUrl($wc_path) |
|
342 | |||
343 | /** |
||
344 | * Returns last changed revision on path/url. |
||
345 | * |
||
346 | * @param string $path_or_url Path or url. |
||
347 | * |
||
348 | * @return integer |
||
349 | */ |
||
350 | 7 | public function getLastRevision($path_or_url) |
|
357 | |||
358 | /** |
||
359 | * Determines if given path is in fact an url. |
||
360 | * |
||
361 | * @param string $path Path. |
||
362 | * |
||
363 | * @return boolean |
||
364 | */ |
||
365 | 24 | public function isUrl($path) |
|
369 | |||
370 | /** |
||
371 | * Removes credentials from url. |
||
372 | * |
||
373 | * @param string $url URL. |
||
374 | * |
||
375 | * @return string |
||
376 | * @throws \InvalidArgumentException When non-url given. |
||
377 | */ |
||
378 | 15 | public function removeCredentials($url) |
|
386 | |||
387 | /** |
||
388 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
389 | * |
||
390 | * @param string $repository_url Repository url. |
||
391 | * |
||
392 | * @return string |
||
393 | * @see getRefByPath |
||
394 | */ |
||
395 | 9 | public function getProjectUrl($repository_url) |
|
404 | |||
405 | /** |
||
406 | * Returns "svn info" entry for path or url. |
||
407 | * |
||
408 | * @param string $path_or_url Path or url. |
||
409 | * @param mixed $cache_duration Cache duration. |
||
410 | * |
||
411 | * @return \SimpleXMLElement |
||
412 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
413 | */ |
||
414 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
443 | |||
444 | /** |
||
445 | * Returns path of "svn info" entry. |
||
446 | * |
||
447 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
463 | |||
464 | /** |
||
465 | * Returns revision, when path was added to repository. |
||
466 | * |
||
467 | * @param string $url Url. |
||
468 | * |
||
469 | * @return integer |
||
470 | * @throws \InvalidArgumentException When not an url was given. |
||
471 | */ |
||
472 | public function getFirstRevision($url) |
||
482 | |||
483 | /** |
||
484 | * Returns conflicts in working copy. |
||
485 | * |
||
486 | * @param string $wc_path Working copy path. |
||
487 | * |
||
488 | * @return array |
||
489 | */ |
||
490 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
502 | |||
503 | /** |
||
504 | * Returns missing paths in working copy. |
||
505 | * |
||
506 | * @param string $wc_path Working copy path. |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | 2 | public function getWorkingCopyMissing($wc_path) |
|
522 | |||
523 | /** |
||
524 | * Returns compact working copy status. |
||
525 | * |
||
526 | * @param string $wc_path Working copy path. |
||
527 | * @param string|null $changelist Changelist. |
||
528 | * @param boolean $with_unversioned With unversioned. |
||
529 | * |
||
530 | * @return string |
||
531 | */ |
||
532 | public function getCompactWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
||
545 | |||
546 | /** |
||
547 | * Returns short item status. |
||
548 | * |
||
549 | * @param string $status Status. |
||
550 | * |
||
551 | * @return string |
||
552 | * @throws \InvalidArgumentException When unknown status given. |
||
553 | */ |
||
554 | protected function getShortItemStatus($status) |
||
579 | |||
580 | /** |
||
581 | * Returns short item status. |
||
582 | * |
||
583 | * @param string $status Status. |
||
584 | * |
||
585 | * @return string |
||
586 | * @throws \InvalidArgumentException When unknown status given. |
||
587 | */ |
||
588 | protected function getShortPropertiesStatus($status) |
||
603 | |||
604 | /** |
||
605 | * Returns working copy status. |
||
606 | * |
||
607 | * @param string $wc_path Working copy path. |
||
608 | * @param string|null $changelist Changelist. |
||
609 | * @param boolean $with_unversioned With unversioned. |
||
610 | * |
||
611 | * @return array |
||
612 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
613 | */ |
||
614 | 7 | public function getWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
|
672 | |||
673 | /** |
||
674 | * Processes "entry" nodes from "svn status" command. |
||
675 | * |
||
676 | * @param string $wc_path Working copy path. |
||
677 | * @param \SimpleXMLElement $entries Entries. |
||
678 | * |
||
679 | * @return array |
||
680 | */ |
||
681 | 5 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
698 | |||
699 | /** |
||
700 | * Detects specific path status. |
||
701 | * |
||
702 | * @param array $status Path status. |
||
703 | * @param string $path_status Expected path status. |
||
704 | * |
||
705 | * @return boolean |
||
706 | */ |
||
707 | 5 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
725 | |||
726 | /** |
||
727 | * Returns parent paths from given paths. |
||
728 | * |
||
729 | * @param array $paths Paths. |
||
730 | * |
||
731 | * @return array |
||
732 | */ |
||
733 | 1 | protected function getParentPaths(array $paths) |
|
746 | |||
747 | /** |
||
748 | * Returns working copy changelists. |
||
749 | * |
||
750 | * @param string $wc_path Working copy path. |
||
751 | * |
||
752 | * @return array |
||
753 | */ |
||
754 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
767 | |||
768 | /** |
||
769 | * Determines if working copy contains mixed revisions. |
||
770 | * |
||
771 | * @param string $wc_path Working copy path. |
||
772 | * |
||
773 | * @return boolean |
||
774 | */ |
||
775 | public function isMixedRevisionWorkingCopy($wc_path) |
||
800 | |||
801 | /** |
||
802 | * Determines if there is a working copy on a given path. |
||
803 | * |
||
804 | * @param string $path Path. |
||
805 | * |
||
806 | * @return boolean |
||
807 | * @throws \InvalidArgumentException When path isn't found. |
||
808 | * @throws RepositoryCommandException When repository command failed to execute. |
||
809 | */ |
||
810 | public function isWorkingCopy($path) |
||
829 | |||
830 | } |
||
831 |
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.