| Conditions | 4 |
| Paths | 4 |
| Total Lines | 103 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 92 | public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string |
||
| 93 | { |
||
| 94 | $statistics = app(Statistics::class); |
||
| 95 | |||
| 96 | $show_last_update = $this->getBlockSetting($block_id, 'show_last_update', '1'); |
||
| 97 | $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1'); |
||
| 98 | $number_of_surnames = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES); |
||
| 99 | $stat_indi = $this->getBlockSetting($block_id, 'stat_indi', '1'); |
||
| 100 | $stat_fam = $this->getBlockSetting($block_id, 'stat_fam', '1'); |
||
| 101 | $stat_sour = $this->getBlockSetting($block_id, 'stat_sour', '1'); |
||
| 102 | $stat_media = $this->getBlockSetting($block_id, 'stat_media', '1'); |
||
| 103 | $stat_repo = $this->getBlockSetting($block_id, 'stat_repo', '1'); |
||
| 104 | $stat_surname = $this->getBlockSetting($block_id, 'stat_surname', '1'); |
||
| 105 | $stat_events = $this->getBlockSetting($block_id, 'stat_events', '1'); |
||
| 106 | $stat_users = $this->getBlockSetting($block_id, 'stat_users', '1'); |
||
| 107 | $stat_first_birth = $this->getBlockSetting($block_id, 'stat_first_birth', '1'); |
||
| 108 | $stat_last_birth = $this->getBlockSetting($block_id, 'stat_last_birth', '1'); |
||
| 109 | $stat_first_death = $this->getBlockSetting($block_id, 'stat_first_death', '1'); |
||
| 110 | $stat_last_death = $this->getBlockSetting($block_id, 'stat_last_death', '1'); |
||
| 111 | $stat_long_life = $this->getBlockSetting($block_id, 'stat_long_life', '1'); |
||
| 112 | $stat_avg_life = $this->getBlockSetting($block_id, 'stat_avg_life', '1'); |
||
| 113 | $stat_most_chil = $this->getBlockSetting($block_id, 'stat_most_chil', '1'); |
||
| 114 | $stat_avg_chil = $this->getBlockSetting($block_id, 'stat_avg_chil', '1'); |
||
| 115 | |||
| 116 | extract($config, EXTR_OVERWRITE); |
||
| 117 | |||
| 118 | if ($show_common_surnames) { |
||
| 119 | // Use the count of base surnames. |
||
| 120 | $top_surnames = DB::table('name') |
||
| 121 | ->where('n_file', '=', $tree->id()) |
||
| 122 | ->where('n_type', '<>', '_MARNM') |
||
| 123 | ->whereNotIn('n_surn', [Individual::NOMEN_NESCIO, '']) |
||
| 124 | ->groupBy(['n_surn']) |
||
| 125 | ->orderByDesc(new Expression('COUNT(n_surn)')) |
||
| 126 | ->take($number_of_surnames) |
||
| 127 | ->pluck('n_surn'); |
||
| 128 | |||
| 129 | $all_surnames = []; |
||
| 130 | |||
| 131 | foreach ($top_surnames as $top_surname) { |
||
| 132 | $variants = DB::table('name') |
||
| 133 | ->where('n_file', '=', $tree->id()) |
||
| 134 | ->where(new Expression('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) |
||
| 135 | ->groupBy(['surname']) |
||
| 136 | ->select([new Expression('n_surname /*! COLLATE utf8_bin */ AS surname'), new Expression('count(*) AS total')]) |
||
| 137 | ->pluck('total', 'surname') |
||
| 138 | ->all(); |
||
| 139 | |||
| 140 | $all_surnames[$top_surname] = $variants; |
||
| 141 | } |
||
| 142 | |||
| 143 | uksort($all_surnames, I18N::comparator()); |
||
| 144 | |||
| 145 | // Find a module providing individual lists |
||
| 146 | $module = $this->module_service |
||
| 147 | ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) |
||
| 148 | ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule); |
||
| 149 | |||
| 150 | $surnames = view('lists/surnames-compact-list', [ |
||
| 151 | 'module' => $module, |
||
| 152 | 'totals' => false, |
||
| 153 | 'surnames' => $all_surnames, |
||
| 154 | 'tree' => $tree, |
||
| 155 | ]); |
||
| 156 | } else { |
||
| 157 | $surnames = ''; |
||
| 158 | } |
||
| 159 | |||
| 160 | $content = view('modules/gedcom_stats/statistics', [ |
||
| 161 | 'show_last_update' => $show_last_update, |
||
| 162 | 'show_common_surnames' => $show_common_surnames, |
||
| 163 | 'number_of_surnames' => $number_of_surnames, |
||
| 164 | 'stat_indi' => $stat_indi, |
||
| 165 | 'stat_fam' => $stat_fam, |
||
| 166 | 'stat_sour' => $stat_sour, |
||
| 167 | 'stat_media' => $stat_media, |
||
| 168 | 'stat_repo' => $stat_repo, |
||
| 169 | 'stat_surname' => $stat_surname, |
||
| 170 | 'stat_events' => $stat_events, |
||
| 171 | 'stat_users' => $stat_users, |
||
| 172 | 'stat_first_birth' => $stat_first_birth, |
||
| 173 | 'stat_last_birth' => $stat_last_birth, |
||
| 174 | 'stat_first_death' => $stat_first_death, |
||
| 175 | 'stat_last_death' => $stat_last_death, |
||
| 176 | 'stat_long_life' => $stat_long_life, |
||
| 177 | 'stat_avg_life' => $stat_avg_life, |
||
| 178 | 'stat_most_chil' => $stat_most_chil, |
||
| 179 | 'stat_avg_chil' => $stat_avg_chil, |
||
| 180 | 'stats' => $statistics, |
||
| 181 | 'surnames' => $surnames, |
||
| 182 | ]); |
||
| 183 | |||
| 184 | if ($context !== self::CONTEXT_EMBED) { |
||
| 185 | return view('modules/block-template', [ |
||
| 186 | 'block' => Str::kebab($this->name()), |
||
| 187 | 'id' => $block_id, |
||
| 188 | 'config_url' => $this->configUrl($tree, $context, $block_id), |
||
| 189 | 'title' => $this->title(), |
||
| 190 | 'content' => $content, |
||
| 191 | ]); |
||
| 192 | } |
||
| 193 | |||
| 194 | return $content; |
||
| 195 | } |
||
| 316 |