| Total Complexity | 43 |
| Total Lines | 350 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PlaceHierarchyListModule 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 PlaceHierarchyListModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class PlaceHierarchyListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface |
||
| 54 | { |
||
| 55 | use ModuleListTrait; |
||
| 56 | |||
| 57 | protected const ROUTE_URL = '/tree/{tree}/place-list'; |
||
| 58 | |||
| 59 | /** @var int The default access level for this module. It can be changed in the control panel. */ |
||
| 60 | protected $access_level = Auth::PRIV_USER; |
||
| 61 | |||
| 62 | /** @var SearchService */ |
||
| 63 | private $search_service; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * PlaceHierarchy constructor. |
||
| 67 | * |
||
| 68 | * @param SearchService $search_service |
||
| 69 | */ |
||
| 70 | public function __construct(SearchService $search_service) |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initialization. |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function boot(): void |
||
| 81 | { |
||
| 82 | $router_container = app(RouterContainer::class); |
||
| 83 | assert($router_container instanceof RouterContainer); |
||
| 84 | |||
| 85 | $router_container->getMap() |
||
| 86 | ->get(static::class, static::ROUTE_URL, $this); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * How should this module be identified in the control panel, etc.? |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function title(): string |
||
| 95 | { |
||
| 96 | /* I18N: Name of a module/list */ |
||
| 97 | return I18N::translate('Place hierarchy'); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * A sentence describing what this module does. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function description(): string |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * CSS class for the URL. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function listMenuClass(): string |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param Tree $tree |
||
| 123 | * @param mixed[] $parameters |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function listUrl(Tree $tree, array $parameters = []): string |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return string[] |
||
| 136 | */ |
||
| 137 | public function listUrlAttributes(): array |
||
| 138 | { |
||
| 139 | return []; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param Tree $tree |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function listIsEmpty(Tree $tree): bool |
||
| 148 | { |
||
| 149 | return !DB::table('places') |
||
| 150 | ->where('p_file', '=', $tree->id()) |
||
| 151 | ->exists(); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Handle URLs generated by older versions of webtrees |
||
| 156 | * |
||
| 157 | * @param ServerRequestInterface $request |
||
| 158 | * |
||
| 159 | * @return ResponseInterface |
||
| 160 | */ |
||
| 161 | public function getListAction(ServerRequestInterface $request): ResponseInterface |
||
| 162 | { |
||
| 163 | return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams())); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param ServerRequestInterface $request |
||
| 168 | * |
||
| 169 | * @return ResponseInterface |
||
| 170 | */ |
||
| 171 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 257 | ]); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param Tree $tree |
||
| 262 | * |
||
| 263 | * @return array<array<Place>> |
||
| 264 | */ |
||
| 265 | private function getList(Tree $tree): array |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @param Place $place |
||
| 287 | * |
||
| 288 | * @return array{'tree':Tree,'col_class':string,'columns':array<array<Place>>,'place':Place}|null |
||
| 289 | */ |
||
| 290 | private function getHierarchy(Place $place): ?array |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param Place $place |
||
| 311 | * |
||
| 312 | * @return array{'breadcrumbs':array<Place>,'current':Place|null} |
||
| 313 | */ |
||
| 314 | private function breadcrumbs(Place $place): array |
||
| 333 | ]; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param Tree $tree |
||
| 338 | * @param Place $placeObj |
||
| 339 | * |
||
| 340 | * @return array<mixed> |
||
| 341 | */ |
||
| 342 | protected function mapData(Tree $tree, Place $placeObj): array |
||
| 403 | ] |
||
| 404 | ]; |
||
| 405 | } |
||
| 407 |