| Conditions | 20 |
| Paths | 4116 |
| Total Lines | 199 |
| Code Lines | 133 |
| 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 |
||
| 247 | protected function chart(Individual $individual, string $style, int $width, int $generations): ResponseInterface |
||
| 248 | { |
||
| 249 | $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations); |
||
| 250 | |||
| 251 | $gen = $generations - 1; |
||
| 252 | $sosa = 2 ** $generations - 1; |
||
| 253 | |||
| 254 | // fan size |
||
| 255 | $fanw = 640 * $width / 100; |
||
| 256 | $cx = $fanw / 2 - 1; // center x |
||
| 257 | $cy = $cx; // center y |
||
| 258 | $rx = $fanw - 1; |
||
| 259 | $rw = $fanw / ($gen + 1); |
||
| 260 | $fanh = $fanw; // fan height |
||
| 261 | if ($style === self::STYLE_HALF_CIRCLE) { |
||
| 262 | $fanh = $fanh * ($gen + 1) / ($gen * 2); |
||
| 263 | } |
||
| 264 | if ($style === self::STYLE_THREE_QUARTER_CIRCLE) { |
||
| 265 | $fanh *= 0.86; |
||
| 266 | } |
||
| 267 | $scale = $fanw / 640; |
||
| 268 | |||
| 269 | // Create the image |
||
| 270 | $image = imagecreate((int) $fanw, (int) $fanh); |
||
| 271 | |||
| 272 | // Create colors |
||
| 273 | $transparent = imagecolorallocate($image, 0, 0, 0); |
||
|
|
|||
| 274 | imagecolortransparent($image, $transparent); |
||
| 275 | |||
| 276 | $theme = app(ModuleThemeInterface::class); |
||
| 277 | |||
| 278 | $foreground = $this->imageColor($image, $theme->parameter('chart-font-color')); |
||
| 279 | |||
| 280 | $backgrounds = [ |
||
| 281 | 'M' => $this->imageColor($image, $theme->parameter('chart-background-m')), |
||
| 282 | 'F' => $this->imageColor($image, $theme->parameter('chart-background-f')), |
||
| 283 | 'U' => $this->imageColor($image, $theme->parameter('chart-background-u')), |
||
| 284 | ]; |
||
| 285 | |||
| 286 | imagefilledrectangle($image, 0, 0, (int) $fanw, (int) $fanh, $transparent); |
||
| 287 | |||
| 288 | $fandeg = 90 * $style; |
||
| 289 | |||
| 290 | // Popup menus for each ancestor |
||
| 291 | $html = ''; |
||
| 292 | |||
| 293 | // Areas for the imagemap |
||
| 294 | $areas = ''; |
||
| 295 | |||
| 296 | // loop to create fan cells |
||
| 297 | while ($gen >= 0) { |
||
| 298 | // clean current generation area |
||
| 299 | $deg2 = 360 + ($fandeg - 180) / 2; |
||
| 300 | $deg1 = $deg2 - $fandeg; |
||
| 301 | imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $backgrounds['U'], IMG_ARC_PIE); |
||
| 302 | $rx -= 3; |
||
| 303 | |||
| 304 | // calculate new angle |
||
| 305 | $p2 = 2 ** $gen; |
||
| 306 | $angle = $fandeg / $p2; |
||
| 307 | $deg2 = 360 + ($fandeg - 180) / 2; |
||
| 308 | $deg1 = $deg2 - $angle; |
||
| 309 | // special case for rootid cell |
||
| 310 | if ($gen == 0) { |
||
| 311 | $deg1 = 90; |
||
| 312 | $deg2 = 360 + $deg1; |
||
| 313 | } |
||
| 314 | |||
| 315 | // draw each cell |
||
| 316 | while ($sosa >= $p2) { |
||
| 317 | if ($ancestors->has($sosa)) { |
||
| 318 | $person = $ancestors->get($sosa); |
||
| 319 | $name = $person->fullName(); |
||
| 320 | $addname = $person->alternateName(); |
||
| 321 | |||
| 322 | $text = I18N::reverseText($name); |
||
| 323 | if ($addname) { |
||
| 324 | $text .= "\n" . I18N::reverseText($addname); |
||
| 325 | } |
||
| 326 | |||
| 327 | $text .= "\n" . I18N::reverseText($person->getLifeSpan()); |
||
| 328 | |||
| 329 | $background = $backgrounds[$person->sex()]; |
||
| 330 | |||
| 331 | imagefilledarc($image, (int) $cx, (int) $cy, (int) $rx, (int) $rx, (int) $deg1, (int) $deg2, $background, IMG_ARC_PIE); |
||
| 332 | |||
| 333 | // split and center text by lines |
||
| 334 | $wmax = (int) ($angle * 7 / 7 * $scale); |
||
| 335 | $wmax = min($wmax, 35 * $scale); |
||
| 336 | if ($gen === 0) { |
||
| 337 | $wmax = min($wmax, 17 * $scale); |
||
| 338 | } |
||
| 339 | $text = $this->splitAlignText($text, (int) $wmax); |
||
| 340 | |||
| 341 | // text angle |
||
| 342 | $tangle = 270 - ($deg1 + $angle / 2); |
||
| 343 | if ($gen === 0) { |
||
| 344 | $tangle = 0; |
||
| 345 | } |
||
| 346 | |||
| 347 | // calculate text position |
||
| 348 | $deg = $deg1 + 0.44; |
||
| 349 | if ($deg2 - $deg1 > 40) { |
||
| 350 | $deg = $deg1 + ($deg2 - $deg1) / 11; |
||
| 351 | } |
||
| 352 | if ($deg2 - $deg1 > 80) { |
||
| 353 | $deg = $deg1 + ($deg2 - $deg1) / 7; |
||
| 354 | } |
||
| 355 | if ($deg2 - $deg1 > 140) { |
||
| 356 | $deg = $deg1 + ($deg2 - $deg1) / 4; |
||
| 357 | } |
||
| 358 | if ($gen === 0) { |
||
| 359 | $deg = 180; |
||
| 360 | } |
||
| 361 | $rad = deg2rad($deg); |
||
| 362 | $mr = ($rx - $rw / 4) / 2; |
||
| 363 | if ($gen > 0 && $deg2 - $deg1 > 80) { |
||
| 364 | $mr = $rx / 2; |
||
| 365 | } |
||
| 366 | $tx = $cx + $mr * cos($rad); |
||
| 367 | $ty = $cy + $mr * sin($rad); |
||
| 368 | if ($sosa === 1) { |
||
| 369 | $ty -= $mr / 2; |
||
| 370 | } |
||
| 371 | |||
| 372 | // print text |
||
| 373 | imagettftext( |
||
| 374 | $image, |
||
| 375 | 7, |
||
| 376 | $tangle, |
||
| 377 | (int) $tx, |
||
| 378 | (int) $ty, |
||
| 379 | $foreground, |
||
| 380 | Webtrees::ROOT_DIR . 'resources/fonts/DejaVuSans.ttf', |
||
| 381 | $text |
||
| 382 | ); |
||
| 383 | |||
| 384 | $areas .= '<area shape="poly" coords="'; |
||
| 385 | // plot upper points |
||
| 386 | $mr = $rx / 2; |
||
| 387 | $deg = $deg1; |
||
| 388 | while ($deg <= $deg2) { |
||
| 389 | $rad = deg2rad($deg); |
||
| 390 | $tx = round($cx + $mr * cos($rad)); |
||
| 391 | $ty = round($cy + $mr * sin($rad)); |
||
| 392 | $areas .= "$tx,$ty,"; |
||
| 393 | $deg += ($deg2 - $deg1) / 6; |
||
| 394 | } |
||
| 395 | // plot lower points |
||
| 396 | $mr = ($rx - $rw) / 2; |
||
| 397 | $deg = $deg2; |
||
| 398 | while ($deg >= $deg1) { |
||
| 399 | $rad = deg2rad($deg); |
||
| 400 | $tx = round($cx + $mr * cos($rad)); |
||
| 401 | $ty = round($cy + $mr * sin($rad)); |
||
| 402 | $areas .= "$tx,$ty,"; |
||
| 403 | $deg -= ($deg2 - $deg1) / 6; |
||
| 404 | } |
||
| 405 | // join first point |
||
| 406 | $mr = $rx / 2; |
||
| 407 | $deg = $deg1; |
||
| 408 | $rad = deg2rad($deg); |
||
| 409 | $tx = round($cx + $mr * cos($rad)); |
||
| 410 | $ty = round($cy + $mr * sin($rad)); |
||
| 411 | $areas .= "$tx,$ty"; |
||
| 412 | // add action url |
||
| 413 | $areas .= '" href="#' . $person->xref() . '"'; |
||
| 414 | $html .= '<div id="' . $person->xref() . '" class="fan_chart_menu">'; |
||
| 415 | $html .= '<div class="person_box"><div class="details1">'; |
||
| 416 | $html .= '<div class="charts">'; |
||
| 417 | $html .= '<a href="' . e($person->url()) . '" class="dropdown-item">' . $name . '</a>'; |
||
| 418 | foreach ($theme->individualBoxMenu($person) as $menu) { |
||
| 419 | $html .= '<a href="' . e($menu->getLink()) . '" class="dropdown-item p-1 ' . e($menu->getClass()) . '">' . $menu->getLabel() . '</a>'; |
||
| 420 | } |
||
| 421 | $html .= '</div>'; |
||
| 422 | $html .= '</div></div>'; |
||
| 423 | $html .= '</div>'; |
||
| 424 | $areas .= ' alt="' . strip_tags($person->fullName()) . '" title="' . strip_tags($person->fullName()) . '">'; |
||
| 425 | } |
||
| 426 | $deg1 -= $angle; |
||
| 427 | $deg2 -= $angle; |
||
| 428 | $sosa--; |
||
| 429 | } |
||
| 430 | $rx -= $rw; |
||
| 431 | $gen--; |
||
| 432 | } |
||
| 433 | |||
| 434 | ob_start(); |
||
| 435 | imagepng($image); |
||
| 436 | imagedestroy($image); |
||
| 437 | $png = ob_get_clean(); |
||
| 438 | |||
| 439 | return response(view('modules/fanchart/chart', [ |
||
| 440 | 'fanh' => $fanh, |
||
| 441 | 'fanw' => $fanw, |
||
| 442 | 'html' => $html, |
||
| 443 | 'areas' => $areas, |
||
| 444 | 'png' => $png, |
||
| 445 | 'title' => $this->chartTitle($individual), |
||
| 446 | ])); |
||
| 559 |