Conditions | 12 |
Paths | 22 |
Total Lines | 108 |
Code Lines | 77 |
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 | 'style' => $this->getBlockSetting($block_id, 'pedigree_style', |
||
117 | PedigreeChartModule::DEFAULT_STYLE), |
||
118 | ]); |
||
119 | $content = view('modules/charts/chart', [ |
||
120 | 'block_id' => $block_id, |
||
121 | 'chart_url' => $chart_url, |
||
122 | ]); |
||
123 | } else { |
||
124 | $title = I18N::translate('Pedigree'); |
||
125 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
126 | } |
||
127 | break; |
||
128 | |||
129 | case 'descendants': |
||
130 | $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first(); |
||
131 | |||
132 | if ($module instanceof DescendancyChartModule) { |
||
133 | $title = $module->chartTitle($individual); |
||
134 | $chart_url = $module->chartUrl($individual, [ |
||
135 | 'ajax' => true, |
||
136 | 'generations' => $this->getBlockSetting($block_id, 'descendants_generations', '2'), |
||
137 | 'chart_style' => DescendancyChartModule::CHART_STYLE_TREE, |
||
138 | ]); |
||
139 | $content = view('modules/charts/chart', [ |
||
140 | 'block_id' => $block_id, |
||
141 | 'chart_url' => $chart_url, |
||
142 | ]); |
||
143 | } else { |
||
144 | $title = I18N::translate('Descendants'); |
||
145 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
146 | } |
||
147 | |||
148 | break; |
||
149 | |||
150 | case 'hourglass': |
||
151 | $module = $this->module_service->findByInterface(HourglassChartModule::class)->first(); |
||
152 | |||
153 | if ($module instanceof HourglassChartModule) { |
||
154 | $title = $module->chartTitle($individual); |
||
155 | $chart_url = $module->chartUrl($individual, [ |
||
156 | 'ajax' => true, |
||
157 | 'generations' => $this->getBlockSetting($block_id, 'hourglass_generations', '2'), |
||
158 | ]); |
||
159 | $content = view('modules/charts/chart', [ |
||
160 | 'block_id' => $block_id, |
||
161 | 'chart_url' => $chart_url, |
||
162 | ]); |
||
163 | } else { |
||
164 | $title = I18N::translate('Hourglass chart'); |
||
165 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
166 | } |
||
167 | break; |
||
168 | |||
169 | case 'treenav': |
||
170 | $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first(); |
||
171 | |||
172 | if ($module instanceof InteractiveTreeModule) { |
||
173 | $title = I18N::translate('Interactive tree of %s', $individual->fullName()); |
||
174 | $tv = new TreeView(); |
||
175 | [$html, $js] = $tv->drawViewport($individual, 2); |
||
176 | $content = $html . '<script>' . $js . '</script>'; |
||
177 | } else { |
||
178 | $title = I18N::translate('Interactive tree'); |
||
179 | $content = I18N::translate('The module “%s” has been disabled.', $title); |
||
180 | } |
||
181 | |||
182 | break; |
||
183 | } |
||
184 | } else { |
||
185 | $content = I18N::translate('You must select an individual and a chart type in the block preferences'); |
||
186 | } |
||
187 | |||
188 | if ($context !== self::CONTEXT_EMBED) { |
||
189 | return view('modules/block-template', [ |
||
190 | 'block' => Str::kebab($this->name()), |
||
191 | 'id' => $block_id, |
||
192 | 'config_url' => $this->configUrl($tree, $context, $block_id), |
||
193 | 'title' => $title, |
||
194 | 'content' => $content, |
||
195 | ]); |
||
196 | } |
||
197 | |||
198 | return $content; |
||
199 | } |
||
318 |