| Conditions | 10 |
| Paths | 45 |
| Total Lines | 109 |
| Code Lines | 75 |
| 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 |
||
| 98 | public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string |
||
| 99 | { |
||
| 100 | $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); |
||
| 101 | $info_style = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); |
||
| 102 | |||
| 103 | extract($config, EXTR_OVERWRITE); |
||
| 104 | |||
| 105 | $query = DB::table('name') |
||
| 106 | ->where('n_file', '=', $tree->id()) |
||
| 107 | ->where('n_type', '<>', '_MARNM') |
||
| 108 | ->where('n_surn', '<>', '') |
||
| 109 | ->where('n_surn', '<>', Individual::NOMEN_NESCIO) |
||
| 110 | ->select([ |
||
| 111 | $this->binaryColumn('n_surn', 'n_surn'), |
||
| 112 | $this->binaryColumn('n_surname', 'n_surname'), |
||
| 113 | new Expression('COUNT(*) AS total'), |
||
| 114 | ]) |
||
| 115 | ->groupBy([ |
||
| 116 | $this->binaryColumn('n_surn'), |
||
| 117 | $this->binaryColumn('n_surname'), |
||
| 118 | ]); |
||
| 119 | |||
| 120 | /** @var array<array<int>> $top_surnames */ |
||
| 121 | $top_surnames = []; |
||
| 122 | |||
| 123 | foreach ($query->get() as $row) { |
||
| 124 | $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn; |
||
| 125 | $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn)); |
||
| 126 | |||
| 127 | $top_surnames[$row->n_surn][$row->n_surname] ??= 0; |
||
| 128 | $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total; |
||
| 129 | } |
||
| 130 | |||
| 131 | uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x)); |
||
| 132 | |||
| 133 | $top_surnames = array_slice($top_surnames, 0, $num, true); |
||
| 134 | |||
| 135 | // Find a module providing individual lists. |
||
| 136 | $module = $this->module_service |
||
| 137 | ->findByComponent(ModuleListInterface::class, $tree, Auth::user()) |
||
| 138 | ->first(static function (ModuleInterface $module): bool { |
||
| 139 | // The family list extends the individual list |
||
| 140 | return |
||
| 141 | $module instanceof IndividualListModule && |
||
| 142 | !$module instanceof FamilyListModule; |
||
| 143 | }); |
||
| 144 | |||
| 145 | switch ($info_style) { |
||
| 146 | case 'tagcloud': |
||
| 147 | uksort($top_surnames, I18N::comparator()); |
||
| 148 | $content = view('lists/surnames-tag-cloud', [ |
||
| 149 | 'module' => $module, |
||
| 150 | 'surnames' => $top_surnames, |
||
| 151 | 'totals' => true, |
||
| 152 | 'tree' => $tree, |
||
| 153 | ]); |
||
| 154 | break; |
||
| 155 | |||
| 156 | case 'list': |
||
| 157 | $content = view('lists/surnames-bullet-list', [ |
||
| 158 | 'module' => $module, |
||
| 159 | 'surnames' => $top_surnames, |
||
| 160 | 'totals' => true, |
||
| 161 | 'tree' => $tree, |
||
| 162 | ]); |
||
| 163 | break; |
||
| 164 | |||
| 165 | case 'array': |
||
| 166 | $content = view('lists/surnames-compact-list', [ |
||
| 167 | 'module' => $module, |
||
| 168 | 'surnames' => $top_surnames, |
||
| 169 | 'totals' => true, |
||
| 170 | 'tree' => $tree, |
||
| 171 | ]); |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 'table': |
||
| 175 | default: |
||
| 176 | uksort($top_surnames, I18N::comparator()); |
||
| 177 | $content = view('lists/surnames-table', [ |
||
| 178 | 'families' => false, |
||
| 179 | 'module' => $module, |
||
| 180 | 'order' => [[1, 'desc']], |
||
| 181 | 'surnames' => $top_surnames, |
||
| 182 | 'tree' => $tree, |
||
| 183 | ]); |
||
| 184 | break; |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($context !== self::CONTEXT_EMBED) { |
||
| 188 | $num = count($top_surnames); |
||
| 189 | if ($num === 1) { |
||
| 190 | // I18N: i.e. most popular surname. |
||
| 191 | $title = I18N::translate('Top surname'); |
||
| 192 | } else { |
||
| 193 | // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 |
||
| 194 | $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); |
||
| 195 | } |
||
| 196 | |||
| 197 | return view('modules/block-template', [ |
||
| 198 | 'block' => Str::kebab($this->name()), |
||
| 199 | 'id' => $block_id, |
||
| 200 | 'config_url' => $this->configUrl($tree, $context, $block_id), |
||
| 201 | 'title' => $title, |
||
| 202 | 'content' => $content, |
||
| 203 | ]); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $content; |
||
| 207 | } |
||
| 313 |