Complex classes like BridgeFileSystem 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 BridgeFileSystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class BridgeFileSystem implements BridgeInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var Filesystem |
||
24 | */ |
||
25 | private $oFileSystemService; |
||
26 | |||
27 | /** |
||
28 | * @var CacheInterface |
||
29 | */ |
||
30 | private $oCache; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $sRepositoryDir = ''; |
||
36 | |||
37 | /** |
||
38 | * @var string[] |
||
39 | */ |
||
40 | private $aDirectorySeparators = ['.', ':', '-', '\\']; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $aJobFileMap = []; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $aGroupedApps = []; |
||
51 | |||
52 | /** |
||
53 | * @param Filesystem $oFileSystemService |
||
54 | * @param CacheInterface $oCache |
||
55 | * @param string $sRepositoryDir |
||
56 | */ |
||
57 | 9 | public function __construct( |
|
67 | |||
68 | /** |
||
69 | * @return JobEntityInterface[] |
||
70 | */ |
||
71 | 8 | public function getJobs() |
|
80 | |||
81 | /** |
||
82 | * @param ChronosJobEntity|JobEntityInterface $oJobEntity |
||
83 | * @return bool |
||
84 | * @throws JobLoadException |
||
85 | */ |
||
86 | 2 | public function addJob(JobEntityInterface $oJobEntity) |
|
99 | |||
100 | /** |
||
101 | * @param JobEntityInterface $oJobEntity |
||
102 | * @return bool |
||
103 | */ |
||
104 | 3 | public function updateJob(JobEntityInterface $oJobEntity) |
|
119 | |||
120 | /** |
||
121 | * @param ChronosJobEntity|JobEntityInterface $oJobEntity |
||
122 | * @return bool |
||
123 | */ |
||
124 | 3 | public function removeJob(JobEntityInterface $oJobEntity) |
|
144 | |||
145 | /** |
||
146 | * @param JobEntityInterface $oJobEntity |
||
147 | * @return string |
||
148 | */ |
||
149 | 2 | private function generateJobFilePath(JobEntityInterface $oJobEntity) |
|
166 | |||
167 | /** |
||
168 | * @param string $sPath |
||
169 | * @param array $aJobFiles |
||
170 | * @return array |
||
171 | */ |
||
172 | 8 | private function getJobFilesFromFileSystem($sPath, array &$aJobFiles = []) |
|
195 | |||
196 | /** |
||
197 | * @param string $sJobName |
||
198 | * @param string $sJobFile |
||
199 | * @throws JobLoadException |
||
200 | */ |
||
201 | 7 | private function setJobFileToMap($sJobName, $sJobFile) |
|
214 | |||
215 | /** |
||
216 | * @param string $sJobName |
||
217 | * @return string |
||
218 | * @throws \RuntimeException |
||
219 | */ |
||
220 | 4 | private function getJobFileFromMap($sJobName) |
|
228 | |||
229 | /** |
||
230 | * @param string $sJobName |
||
231 | * @param string $sJobFile |
||
232 | * @return bool |
||
233 | * @throws \RuntimeException |
||
234 | */ |
||
235 | 2 | private function hasUnsetJobFileFromMap($sJobName, $sJobFile = '') |
|
247 | |||
248 | /** |
||
249 | * @param array $aJobFiles |
||
250 | * @param bool $bSetToFileMap |
||
251 | * @return JobEntityInterface[] |
||
252 | * @throws JobLoadException |
||
253 | */ |
||
254 | 8 | private function loadJobsFromFileContent(array $aJobFiles, $bSetToFileMap) |
|
318 | |||
319 | |||
320 | 4 | private function getMarathonEntitiesForConfig($aEntityData) |
|
339 | |||
340 | /** |
||
341 | * @param string $sJobFile |
||
342 | * @param JobEntityInterface $oJobEntity |
||
343 | * @return bool |
||
344 | */ |
||
345 | 2 | private function hasDumpFile($sJobFile, JobEntityInterface $oJobEntity) |
|
354 | |||
355 | /** |
||
356 | * @param string $sJobFile |
||
357 | * @param JobEntityInterface $oJobEntity |
||
358 | * @param bool $bAdd |
||
359 | * @return bool |
||
360 | */ |
||
361 | 2 | private function dumpFileWithGroup($sJobFile, JobEntityInterface $oJobEntity, $bAdd = true) |
|
424 | } |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.