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_UNVERSIONED = 'unversioned'; |
||
27 | |||
28 | /** |
||
29 | * Reference to configuration. |
||
30 | * |
||
31 | * @var ConfigEditor |
||
32 | */ |
||
33 | private $_configEditor; |
||
34 | |||
35 | /** |
||
36 | * Process factory. |
||
37 | * |
||
38 | * @var IProcessFactory |
||
39 | */ |
||
40 | private $_processFactory; |
||
41 | |||
42 | /** |
||
43 | * Console IO. |
||
44 | * |
||
45 | * @var ConsoleIO |
||
46 | */ |
||
47 | private $_io; |
||
48 | |||
49 | /** |
||
50 | * Cache manager. |
||
51 | * |
||
52 | * @var CacheManager |
||
53 | */ |
||
54 | private $_cacheManager; |
||
55 | |||
56 | /** |
||
57 | * Path to an svn command. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $_svnCommand = 'svn'; |
||
62 | |||
63 | /** |
||
64 | * Cache duration for next invoked command. |
||
65 | * |
||
66 | * @var mixed |
||
67 | */ |
||
68 | private $_nextCommandCacheDuration = null; |
||
69 | |||
70 | /** |
||
71 | * Whatever to cache last repository revision or not. |
||
72 | * |
||
73 | * @var mixed |
||
74 | */ |
||
75 | private $_lastRevisionCacheDuration = null; |
||
76 | |||
77 | /** |
||
78 | * Creates repository connector. |
||
79 | * |
||
80 | * @param ConfigEditor $config_editor ConfigEditor. |
||
81 | * @param IProcessFactory $process_factory Process factory. |
||
82 | * @param ConsoleIO $io Console IO. |
||
83 | * @param CacheManager $cache_manager Cache manager. |
||
84 | */ |
||
85 | 47 | public function __construct( |
|
106 | |||
107 | /** |
||
108 | * Prepares static part of svn command to be used across the script. |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | 47 | protected function prepareSvnCommand() |
|
127 | |||
128 | /** |
||
129 | * Builds a command. |
||
130 | * |
||
131 | * @param string $sub_command Sub command. |
||
132 | * @param string|null $param_string Parameter string. |
||
133 | * |
||
134 | * @return Command |
||
135 | */ |
||
136 | 29 | public function getCommand($sub_command, $param_string = null) |
|
153 | |||
154 | /** |
||
155 | * Builds command from given arguments. |
||
156 | * |
||
157 | * @param string $sub_command Command. |
||
158 | * @param string $param_string Parameter string. |
||
159 | * |
||
160 | * @return string |
||
161 | * @throws \InvalidArgumentException When command contains spaces. |
||
162 | */ |
||
163 | 29 | protected function buildCommand($sub_command, $param_string = null) |
|
189 | |||
190 | /** |
||
191 | * Sets cache configuration for next created command. |
||
192 | * |
||
193 | * @param mixed $cache_duration Cache duration. |
||
194 | * |
||
195 | * @return self |
||
196 | */ |
||
197 | 17 | public function withCache($cache_duration) |
|
203 | |||
204 | /** |
||
205 | * Returns property value. |
||
206 | * |
||
207 | * @param string $name Property name. |
||
208 | * @param string $path_or_url Path to get property from. |
||
209 | * @param mixed $revision Revision. |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
223 | |||
224 | /** |
||
225 | * Returns relative path of given path/url to the root of the repository. |
||
226 | * |
||
227 | * @param string $path_or_url Path or url. |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | 2 | public function getRelativePath($path_or_url) |
|
238 | |||
239 | /** |
||
240 | * Returns repository root url from given path/url. |
||
241 | * |
||
242 | * @param string $path_or_url Path or url. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | 4 | public function getRootUrl($path_or_url) |
|
250 | |||
251 | /** |
||
252 | * Detects ref from given path. |
||
253 | * |
||
254 | * @param string $path Path to a file. |
||
255 | * |
||
256 | * @return string|boolean |
||
257 | */ |
||
258 | 6 | public function getRefByPath($path) |
|
266 | |||
267 | /** |
||
268 | * Returns URL of the working copy. |
||
269 | * |
||
270 | * @param string $wc_path Working copy path. |
||
271 | * |
||
272 | * @return string |
||
273 | * @throws RepositoryCommandException When repository command failed to execute. |
||
274 | */ |
||
275 | 7 | public function getWorkingCopyUrl($wc_path) |
|
302 | |||
303 | /** |
||
304 | * Returns last changed revision on path/url. |
||
305 | * |
||
306 | * @param string $path_or_url Path or url. |
||
307 | * |
||
308 | * @return integer |
||
309 | */ |
||
310 | 6 | public function getLastRevision($path_or_url) |
|
317 | |||
318 | /** |
||
319 | * Determines if given path is in fact an url. |
||
320 | * |
||
321 | * @param string $path Path. |
||
322 | * |
||
323 | * @return boolean |
||
324 | */ |
||
325 | 17 | public function isUrl($path) |
|
329 | |||
330 | /** |
||
331 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
332 | * |
||
333 | * @param string $repository_url Repository url. |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | 9 | public function getProjectUrl($repository_url) |
|
345 | |||
346 | /** |
||
347 | * Returns "svn info" entry for path or url. |
||
348 | * |
||
349 | * @param string $path_or_url Path or url. |
||
350 | * @param mixed $cache_duration Cache duration. |
||
351 | * |
||
352 | * @return \SimpleXMLElement |
||
353 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
354 | */ |
||
355 | 16 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
371 | |||
372 | /** |
||
373 | * Returns path of "svn info" entry. |
||
374 | * |
||
375 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | 14 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
391 | |||
392 | /** |
||
393 | * Returns revision, when path was added to repository. |
||
394 | * |
||
395 | * @param string $url Url. |
||
396 | * |
||
397 | * @return integer |
||
398 | * @throws \InvalidArgumentException When not an url was given. |
||
399 | */ |
||
400 | public function getFirstRevision($url) |
||
410 | |||
411 | /** |
||
412 | * Returns conflicts in working copy. |
||
413 | * |
||
414 | * @param string $wc_path Working copy path. |
||
415 | * |
||
416 | * @return array |
||
417 | */ |
||
418 | public function getWorkingCopyConflicts($wc_path) |
||
430 | |||
431 | /** |
||
432 | * Returns compact working copy status. |
||
433 | * |
||
434 | * @param string $wc_path Working copy path. |
||
435 | * @param boolean $with_unversioned With unversioned. |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | public function getCompactWorkingCopyStatus($wc_path, $with_unversioned = true) |
||
456 | |||
457 | /** |
||
458 | * Returns short item status. |
||
459 | * |
||
460 | * @param string $status Status. |
||
461 | * |
||
462 | * @return string |
||
463 | * @throws \InvalidArgumentException When unknown status given. |
||
464 | */ |
||
465 | protected function getShortItemStatus($status) |
||
490 | |||
491 | /** |
||
492 | * Returns short item status. |
||
493 | * |
||
494 | * @param string $status Status. |
||
495 | * |
||
496 | * @return string |
||
497 | * @throws \InvalidArgumentException When unknown status given. |
||
498 | */ |
||
499 | protected function getShortPropertiesStatus($status) |
||
514 | |||
515 | /** |
||
516 | * Returns working copy status. |
||
517 | * |
||
518 | * @param string $wc_path Working copy path. |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | protected function getWorkingCopyStatus($wc_path) |
||
552 | |||
553 | /** |
||
554 | * Determines if working copy contains mixed revisions. |
||
555 | * |
||
556 | * @param string $wc_path Working copy path. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | public function isMixedRevisionWorkingCopy($wc_path) |
||
582 | |||
583 | /** |
||
584 | * Determines if there is a working copy on a given path. |
||
585 | * |
||
586 | * @param string $path Path. |
||
587 | * |
||
588 | * @return boolean |
||
589 | * @throws \InvalidArgumentException When path isn't found. |
||
590 | * @throws RepositoryCommandException When repository command failed to execute. |
||
591 | */ |
||
1 ignored issue
–
show
|
|||
592 | public function isWorkingCopy($path) |
||
611 | |||
612 | } |
||
613 |