| Conditions | 12 |
| Paths | 22 |
| Total Lines | 117 |
| Code Lines | 83 |
| 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 |
||
| 91 | public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string |
||
| 92 | { |
||
| 93 | $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID'); |
||
| 94 | $gedcomid = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF); |
||
| 95 | $default_xref = $gedcomid ?: $PEDIGREE_ROOT_ID; |
||
| 96 | |||
| 97 | $type = $this->getBlockSetting($block_id, 'type', 'pedigree'); |
||
| 98 | $xref = $this->getBlockSetting($block_id, 'pid', $default_xref); |
||
| 99 | |||
| 100 | extract($config, EXTR_OVERWRITE); |
||
| 101 | |||
| 102 | $individual = Registry::individualFactory()->make($xref, $tree); |
||
| 103 | |||
| 104 | $title = $this->title(); |
||
| 105 | |||
| 106 | if ($individual instanceof Individual) { |
||
| 107 | switch ($type) { |
||
| 108 | default: |
||
| 109 | case 'pedigree': |
||
| 110 | $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first(); |
||
| 111 | if ($module instanceof PedigreeChartModule) { |
||
| 112 | $title = $module->chartTitle($individual); |
||
| 113 | $chart_url = $module->chartUrl($individual, [ |
||
| 114 | 'ajax' => true, |
||
| 115 | 'generations' => $this->getBlockSetting($block_id, 'pedigree_generations', '3'), |
||
| 116 | 'layout' => $this->getBlockSetting( |
||
| 117 | $block_id, |
||
| 118 | 'pedigree_style', |
||
| 119 | PedigreeChartModule::DEFAULT_STYLE |
||
| 120 | ), |
||
| 121 | 'style' => $this->getBlockSetting( |
||
| 122 | $block_id, |
||
| 123 | 'pedigree_style', |
||
| 124 | PedigreeChartModule::DEFAULT_STYLE |
||
| 125 | ), |
||
| 126 | // Note: some modules use 'layout', others 'style' |
||
| 127 | ]); |
||
| 128 | $content = view('modules/charts/chart', [ |
||
| 129 | 'block_id' => $block_id, |
||
| 130 | 'chart_url' => $chart_url, |
||
| 131 | ]); |
||
| 132 | } else { |
||
| 133 | $title = I18N::translate('Pedigree'); |
||
| 134 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
| 135 | } |
||
| 136 | break; |
||
| 137 | |||
| 138 | case 'descendants': |
||
| 139 | $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); |
||
| 140 | |||
| 141 | if ($module instanceof DescendancyChartModule) { |
||
| 142 | $title = $module->chartTitle($individual); |
||
| 143 | $chart_url = $module->chartUrl($individual, [ |
||
| 144 | 'ajax' => true, |
||
| 145 | 'generations' => $this->getBlockSetting($block_id, 'descendants_generations', '2'), |
||
| 146 | 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, |
||
| 147 | ]); |
||
| 148 | $content = view('modules/charts/chart', [ |
||
| 149 | 'block_id' => $block_id, |
||
| 150 | 'chart_url' => $chart_url, |
||
| 151 | ]); |
||
| 152 | } else { |
||
| 153 | $title = I18N::translate('Descendants'); |
||
| 154 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
| 155 | } |
||
| 156 | |||
| 157 | break; |
||
| 158 | |||
| 159 | case 'hourglass': |
||
| 160 | $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); |
||
| 161 | |||
| 162 | if ($module instanceof HourglassChartModule) { |
||
| 163 | $title = $module->chartTitle($individual); |
||
| 164 | $chart_url = $module->chartUrl($individual, [ |
||
| 165 | 'ajax' => true, |
||
| 166 | 'generations' => $this->getBlockSetting($block_id, 'hourglass_generations', '2'), |
||
| 167 | ]); |
||
| 168 | $content = view('modules/charts/chart', [ |
||
| 169 | 'block_id' => $block_id, |
||
| 170 | 'chart_url' => $chart_url, |
||
| 171 | ]); |
||
| 172 | } else { |
||
| 173 | $title = I18N::translate('Hourglass chart'); |
||
| 174 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
| 175 | } |
||
| 176 | break; |
||
| 177 | |||
| 178 | case 'treenav': |
||
| 179 | $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); |
||
| 180 | |||
| 181 | if ($module instanceof InteractiveTreeModule) { |
||
| 182 | $title = I18N::translate('Interactive tree of %s', $individual->fullName()); |
||
| 183 | $tv = new TreeView(); |
||
| 184 | [$html, $js] = $tv->drawViewport($individual, 2); |
||
| 185 | $content = $html . '<script>' . $js . '</script>'; |
||
| 186 | } else { |
||
| 187 | $title = I18N::translate('Interactive tree'); |
||
| 188 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
| 189 | } |
||
| 190 | |||
| 191 | break; |
||
| 192 | } |
||
| 193 | } else { |
||
| 194 | $content = I18N::translate('You must select an individual and a chart type in the block preferences'); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($context !== self::CONTEXT_EMBED) { |
||
| 198 | return view('modules/block-template', [ |
||
| 199 | 'block' => Str::kebab($this->name()), |
||
| 200 | 'id' => $block_id, |
||
| 201 | 'config_url' => $this->configUrl($tree, $context, $block_id), |
||
| 202 | 'title' => $title, |
||
| 203 | 'content' => $content, |
||
| 204 | ]); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $content; |
||
| 208 | } |
||
| 327 |