Passed
Push — master ( 2a0f07...98f021 )
by Jakub
02:32
created

TextCombatLogRender::renderItem()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 47
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 13

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 47
ccs 34
cts 34
cp 1
rs 6.6166
c 0
b 0
f 0
cc 13
nc 13
nop 1
crap 13

How to fix   Complexity   

Long Method

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:

1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Bridges\ApplicationLatte\ILatteFactory;
7
use Nette\Localization\ITranslator;
8
9
/**
10
 * TextCombatLogRender
11
 *
12
 * @property string $template
13
 */
14 1
final class TextCombatLogRender implements ICombatLogRender {
15 1
  use \Nette\SmartObject;
16
17
  /** @var \Latte\Engine */
18
  protected $latte;
19
  /** @var ITranslator */
20
  protected $translator;
21
  /** @var string */
22
  protected $template = __DIR__ . "/CombatLog.latte";
23
24
  public function __construct(ILatteFactory $latteFactory, ITranslator $translator) {
25 1
    $this->latte = $latteFactory->create();
26 1
    $this->translator = $translator;
27 1
  }
28
29
  /**
30
   * @throws \RuntimeException
31
   */
32
  public function setTemplate(string $template): void {
33
    if(!is_file($template)) {
34
      throw new \RuntimeException("File $template does not exist.");
35
    }
36
    $this->template = $template;
37
  }
38
39
  public function render(array $params): string {
40 1
    $params["render"] = $this;
41 1
    return $this->latte->renderToString($this->template, $params);
42
  }
43
44
  /**
45
   * @param CombatLogEntry|string $item
46
   */
47
  public function renderItem($item): string {
0 ignored issues
show
introduced by
Function's cyclomatic complexity (13) exceeds 10; consider refactoring the function
Loading history...
48 1
    if(!$item instanceof CombatLogEntry) {
49 1
      return $item;
50
    }
51 1
    $character1 = $item->character1->name;
52 1
    $character2 = $item->character2->name;
53 1
    $text = "";
54 1
    switch($item->action) {
55 1
      case CombatLogEntry::ACTION_ATTACK:
56 1
        if($item->result) {
57 1
          $text = $this->translator->translate("combat.log.attackHits", $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 147 characters
Loading history...
58 1
          if($item->character2->hitpoints < 1) {
59 1
            $text .= $this->translator->translate("combat.log.characterFalls");
60
          }
61
        } else {
62 1
          $text = $this->translator->translate("combat.log.attackFails", $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 148 characters
Loading history...
63
        }
64 1
        break;
65 1
      case CombatLogEntry::ACTION_SKILL_ATTACK:
66 1
        if($item->result) {
67 1
          $text = $this->translator->translate("combat.log.specialAttackHits", $item->amount, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 177 characters
Loading history...
68 1
          if($item->character2->hitpoints < 1) {
69 1
            $text .= $this->translator->translate("combat.log.characterFalls");
70
          }
71
        } else {
72 1
          $text = $this->translator->translate("combat.log.specialAttackFails", $item->amount, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 178 characters
Loading history...
73
        }
74 1
        break;
75 1
      case CombatLogEntry::ACTION_SKILL_SPECIAL:
76 1
        if($item->result) {
77 1
          $text = $this->translator->translate("combat.log.specialSkillSuccess", 0, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 167 characters
Loading history...
78
        } else {
79 1
          $text = $this->translator->translate("combat.log.specialSKillFailure", 0, ["character1" => $character1, "character2" => $character2, "name" => $item->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 167 characters
Loading history...
80
        }
81 1
        break;
82 1
      case CombatLogEntry::ACTION_HEALING:
83 1
        if($item->result) {
84 1
          $text = $this->translator->translate("combat.log.healingSuccess", $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 151 characters
Loading history...
85
        } else {
86 1
          $text = $this->translator->translate("combat.log.healingFailure", $item->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 151 characters
Loading history...
87
        }
88 1
        break;
89 1
      case CombatLogEntry::ACTION_POISON:
90 1
        $text = $this->translator->translate("combat.log.poison", $item->amount, ["character1" => $character1]);
91 1
        break;
92
    }
93 1
    return $text;
94
  }
95
}
96
?>