Complex classes like Git_Mirror_MirrorDataMapper 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 Git_Mirror_MirrorDataMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Git_Mirror_MirrorDataMapper { |
||
| 22 | |||
| 23 | const MIRROR_OWNER_PREFIX = 'forge__gitmirror_'; |
||
| 24 | const PROJECTS_HOSTNAME = 'projects'; |
||
| 25 | |||
| 26 | /** @var Git_Mirror_MirrorDao */ |
||
| 27 | private $dao; |
||
| 28 | |||
| 29 | /** UserManager */ |
||
| 30 | private $user_manager; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var GitRepositoryFactory |
||
| 34 | */ |
||
| 35 | private $repository_factory; |
||
| 36 | |||
| 37 | /** @var ProjectManager */ |
||
| 38 | private $project_manager; |
||
| 39 | |||
| 40 | /** @var Git_SystemEventManager */ |
||
| 41 | private $git_system_event_manager; |
||
| 42 | |||
| 43 | /** @var Git_Gitolite_GitoliteRCReader */ |
||
| 44 | private $reader; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DefaultProjectMirrorDao |
||
| 48 | */ |
||
| 49 | private $default_dao; |
||
| 50 | |||
| 51 | public function __construct( |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return Git_Mirror_Mirror |
||
| 71 | * @throws Git_Mirror_MissingDataException |
||
| 72 | * @throws Git_Mirror_CreateException |
||
| 73 | */ |
||
| 74 | public function save($url, $hostname, $ssh_key, $password, $name) { |
||
| 95 | |||
| 96 | private function checkThatHostnameIsValidOnCreation($hostname) { |
||
| 111 | |||
| 112 | private function doesHostnameIsForbidden($hostname) { |
||
| 116 | |||
| 117 | private function checkThatHostnameIsValidOnUpdate($id, $hostname) { |
||
| 132 | |||
| 133 | private function createUserForMirror($mirror_id, $password, $ssh_key) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return Git_Mirror_Mirror[] |
||
| 148 | */ |
||
| 149 | public function fetchAll() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return Git_Mirror_Mirror[] |
||
| 157 | */ |
||
| 158 | public function fetchAllForProject(Project $project) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return Git_Mirror_Mirror[] |
||
| 166 | */ |
||
| 167 | public function fetchAllRepositoryMirrors(GitRepository $repository) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return Git_Mirror_Mirror[] |
||
| 179 | */ |
||
| 180 | private function mapDataAccessResultToArrayOfMirrors(DataAccessResult $rows) { |
||
| 189 | |||
| 190 | public function fetchRepositoriesForMirror(Git_Mirror_Mirror $mirror) { |
||
| 198 | |||
| 199 | public function fetchAllProjectRepositoriesForMirror(Git_Mirror_Mirror $mirror, array $project_ids) { |
||
| 209 | |||
| 210 | public function fetchRepositoriesPerMirrorPresenters(Git_Mirror_Mirror $mirror) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return Project[] |
||
| 233 | */ |
||
| 234 | public function fetchAllProjectsConcernedByMirroring() { |
||
| 243 | |||
| 244 | public function fetchAllProjectsConcernedByAMirror(Git_Mirror_Mirror $mirror) { |
||
| 253 | |||
| 254 | public function doesAllSelectedMirrorIdsExist($selected_mirror_ids) { |
||
| 260 | |||
| 261 | public function unmirrorRepository($repository_id) { |
||
| 264 | |||
| 265 | public function mirrorRepositoryTo($repository_id, $selected_mirror_ids) { |
||
| 271 | |||
| 272 | public function removeAllDefaultMirrorsToProject(Project $project) { |
||
| 275 | |||
| 276 | public function addDefaultMirrorsToProject(Project $project, array $selected_mirror_ids) { |
||
| 283 | |||
| 284 | public function getDefaultMirrorIdsForProject(Project $project) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return bool |
||
| 290 | * @throws Git_Mirror_MirrorNoChangesException |
||
| 291 | * @throws Git_Mirror_MirrorNotFoundException |
||
| 292 | * @throws Git_Mirror_MissingDataException |
||
| 293 | */ |
||
| 294 | public function update($id, $url, $hostname, $ssh_key, $name) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | * @throws Git_Mirror_MirrorNotFoundException |
||
| 319 | */ |
||
| 320 | public function delete($id) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function deleteFromDefaultMirrors($deleted_mirror_id) { |
||
| 341 | |||
| 342 | public function deleteFromDefaultMirrorsInProjects(Git_Mirror_Mirror $mirror, array $project_ids) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return Git_Mirror_Mirror |
||
| 348 | * @throws Git_Mirror_MirrorNotFoundException |
||
| 349 | */ |
||
| 350 | public function fetch($id) { |
||
| 359 | |||
| 360 | public function getListOfMirrorIdsPerRepositoryForProject(Project $project) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return Git_Mirror_Mirror |
||
| 375 | */ |
||
| 376 | private function getInstanceFromRow(PFUser $owner, $row) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return PFUser |
||
| 388 | */ |
||
| 389 | private function getMirrorOwner($mirror_id) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return boolean |
||
| 397 | */ |
||
| 398 | public function duplicate($template_project_id, $new_project_id) { |
||
| 401 | } |
||
| 402 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.