| Total Complexity | 49 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BranchesController 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.
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 BranchesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class BranchesController extends AbstractBaseController |
||
| 46 | { |
||
| 47 | /** @var ModuleService */ |
||
| 48 | protected $module_service; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * BranchesController constructor. |
||
| 52 | * |
||
| 53 | * @param ModuleService $module_service |
||
| 54 | */ |
||
| 55 | public function __construct(ModuleService $module_service) |
||
| 56 | { |
||
| 57 | $this->module_service = $module_service; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A form to request the page parameters. |
||
| 62 | * |
||
| 63 | * @param ServerRequestInterface $request |
||
| 64 | * |
||
| 65 | * @return ResponseInterface |
||
| 66 | */ |
||
| 67 | public function page(ServerRequestInterface $request): ResponseInterface |
||
| 68 | { |
||
| 69 | $module = $request->getAttribute('module'); |
||
| 70 | $action = $request->getAttribute('action'); |
||
| 71 | |||
| 72 | $surname = $request->getQueryParams()['surname'] ?? ''; |
||
| 73 | $soundex_std = (bool) ($request->getQueryParams()['soundex_std'] ?? false); |
||
| 74 | $soundex_dm = (bool) ($request->getQueryParams()['soundex_dm'] ?? false); |
||
| 75 | |||
| 76 | if ($surname !== '') { |
||
| 77 | /* I18N: %s is a surname */ |
||
| 78 | $title = I18N::translate('Branches of the %s family', e($surname)); |
||
| 79 | } else { |
||
| 80 | /* I18N: Branches of a family tree */ |
||
| 81 | $title = I18N::translate('Branches'); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this->viewResponse('branches-page', [ |
||
| 85 | 'soundex_dm' => $soundex_dm, |
||
| 86 | 'soundex_std' => $soundex_std, |
||
| 87 | 'surname' => $surname, |
||
| 88 | 'title' => $title, |
||
| 89 | 'module' => $module, |
||
| 90 | 'action' => $action, |
||
| 91 | ]); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param ServerRequestInterface $request |
||
| 96 | * |
||
| 97 | * @return ResponseInterface |
||
| 98 | */ |
||
| 99 | public function list(ServerRequestInterface $request): ResponseInterface |
||
| 100 | { |
||
| 101 | $tree = $request->getAttribute('tree'); |
||
| 102 | $user = $request->getAttribute('user'); |
||
| 103 | $params = $request->getQueryParams(); |
||
| 104 | $surname = $params['surname']; |
||
| 105 | $soundex_std = (bool) ($params['soundex_std'] ?? false); |
||
| 106 | $soundex_dm = (bool) ($params['soundex_dm'] ?? false); |
||
| 107 | |||
| 108 | // Highlight direct-line ancestors of this individual. |
||
| 109 | $self = Individual::getInstance($tree->getUserPreference($user, 'gedcomid'), $tree); |
||
| 110 | |||
| 111 | if ($surname !== '') { |
||
| 112 | $individuals = $this->loadIndividuals($tree, $surname, $soundex_dm, $soundex_std); |
||
| 113 | } else { |
||
| 114 | $individuals = []; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($self !== null) { |
||
| 118 | $ancestors = $this->allAncestors($self); |
||
| 119 | } else { |
||
| 120 | $ancestors = []; |
||
| 121 | } |
||
| 122 | |||
| 123 | // @TODO - convert this to use views |
||
| 124 | $html = view('branches-list', [ |
||
| 125 | 'branches' => $this->getPatriarchsHtml($tree, $individuals, $ancestors, $surname, $soundex_dm, $soundex_std), |
||
| 126 | ]); |
||
| 127 | |||
| 128 | return response($html); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Find all ancestors of an individual, indexed by the Sosa-Stradonitz number. |
||
| 133 | * |
||
| 134 | * @param Individual $individual |
||
| 135 | * |
||
| 136 | * @return Individual[] |
||
| 137 | */ |
||
| 138 | protected function allAncestors(Individual $individual): array |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Fetch all individuals with a matching surname |
||
| 165 | * |
||
| 166 | * @param Tree $tree |
||
| 167 | * @param string $surname |
||
| 168 | * @param bool $soundex_dm |
||
| 169 | * @param bool $soundex_std |
||
| 170 | * |
||
| 171 | * @return Individual[] |
||
| 172 | */ |
||
| 173 | private function loadIndividuals(Tree $tree, string $surname, bool $soundex_dm, bool $soundex_std): array |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * For each individual with no ancestors, list their descendants. |
||
| 220 | * |
||
| 221 | * @param Tree $tree |
||
| 222 | * @param Individual[] $individuals |
||
| 223 | * @param Individual[] $ancestors |
||
| 224 | * @param string $surname |
||
| 225 | * @param bool $soundex_dm |
||
| 226 | * @param bool $soundex_std |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getPatriarchsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std): string |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Generate a recursive list of descendants of an individual. |
||
| 249 | * If parents are specified, we can also show the pedigree (adopted, etc.). |
||
| 250 | * |
||
| 251 | * @param Tree $tree |
||
| 252 | * @param Individual[] $individuals |
||
| 253 | * @param Individual[] $ancestors |
||
| 254 | * @param string $surname |
||
| 255 | * @param bool $soundex_dm |
||
| 256 | * @param bool $soundex_std |
||
| 257 | * @param Individual $individual |
||
| 258 | * @param Family|null $parents |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | private function getDescendantsHtml(Tree $tree, array $individuals, array $ancestors, string $surname, bool $soundex_dm, bool $soundex_std, Individual $individual, Family $parents = null): string |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Do two surnames match? |
||
| 356 | * |
||
| 357 | * @param string $surname1 |
||
| 358 | * @param string $surname2 |
||
| 359 | * @param bool $soundex_std |
||
| 360 | * @param bool $soundex_dm |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | private function surnamesMatch(string $surname1, string $surname2, bool $soundex_std, bool $soundex_dm): bool |
||
| 365 | { |
||
| 366 | // One name sounds like another? |
||
| 367 | if ($soundex_std && Soundex::compare(Soundex::russell($surname1), Soundex::russell($surname2))) { |
||
| 368 | return true; |
||
| 369 | } |
||
| 370 | if ($soundex_dm && Soundex::compare(Soundex::daitchMokotoff($surname1), Soundex::daitchMokotoff($surname2))) { |
||
| 371 | return true; |
||
| 372 | } |
||
| 373 | |||
| 374 | // One is a substring of the other. e.g. Halen / Van Halen |
||
| 375 | return stripos($surname1, $surname2) !== false || stripos($surname2, $surname1) !== false; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Convert a SOSA number into a generation number. e.g. 8 = great-grandfather = 3 generations |
||
| 380 | * |
||
| 381 | * @param int $sosa |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | private static function sosaGeneration($sosa): string |
||
| 390 | } |
||
| 391 | } |
||
| 392 |