Completed
Push — master ( f29677...f615bc )
by Jakub
04:44
created

CombatAction::parse()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 44
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12.5487

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 27
cts 32
cp 0.8438
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 36
nc 12
nop 0
crap 12.5487

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\Localization\ITranslator,
7
    Nexendrie\Utils\Constants,
8
    Symfony\Component\OptionsResolver\OptionsResolver;
9
10
/**
11
 * Data structure for combat action
12
 *
13
 * @author Jakub Konečný
14
 * @property-read Character $character1
15
 * @property-read Character $character2
16
 * @property-read string $action
17
 * @property-read string $name
18
 * @property-read bool $result
19
 * @property-read int $amount
20
 * @property-read string $message
21
 */
22 1
class CombatAction {
23 1
  use \Nette\SmartObject;
24
  
25
  public const ACTION_ATTACK = "attack";
26
  public const ACTION_SKILL_ATTACK = "skill_attack";
27
  public const ACTION_SKILL_SPECIAL = "skill_special";
28
  public const ACTION_HEALING = "healing";
29
  public const ACTION_POISON = "poison";
30
  
31
  /** @var ITranslator */
32
  protected $translator;
33
  /** @var Character */
34
  protected $character1;
35
  /** @var Character */
36
  protected $character2;
37
  /** @var  string */
38
  protected $action;
39
  /** @var string */
40
  protected $name;
41
  /** @var bool */
42
  protected $result;
43
  /** @var int */
44
  protected $amount;
45
  /** @var string */
46
  protected $message;
47
  
48
  public function __construct(ITranslator $translator, array $action) {
49 1
    $requiredStats = ["action", "result", "character1", "character2",];
50 1
    $allStats = array_merge($requiredStats, ["amount", "name",]);
51 1
    $resolver = new OptionsResolver();
52 1
    $resolver->setDefined($allStats);
53 1
    $resolver->setRequired($requiredStats);
54 1
    $resolver->setAllowedTypes("action", "string");
55 1
    $resolver->setAllowedValues("action", function(string $value) {
56 1
      return in_array($value, $this->getAllowedActions(), true);
57 1
    });
58 1
    $resolver->setAllowedTypes("result", "bool");
59 1
    $resolver->setAllowedTypes("amount", "integer");
60 1
    $resolver->setDefault("amount", 0);
61 1
    $resolver->setAllowedTypes("name", "string");
62 1
    $resolver->setDefault("name", "");
63 1
    $resolver->setAllowedTypes("character1", Character::class);
64 1
    $resolver->setAllowedTypes("character2", Character::class);
65 1
    $action = $resolver->resolve($action);
66 1
    $this->translator = $translator;
67 1
    $this->action = $action["action"];
68 1
    $this->result = $action["result"];
69 1
    $this->amount = $action["amount"];
70 1
    $this->character1 = $action["character1"];
71 1
    $this->character2 = $action["character2"];
72 1
    $this->name = $action["name"];
73 1
    $this->parse();
74 1
  }
75
  
76
  /**
77
   * @return string[]
78
   */
79
  protected function getAllowedActions(): array {
80 1
    return Constants::getConstantsValues(static::class, "ACTION_");
81
  }
82
  
83
  public function getCharacter1(): Character {
84
    return $this->character1;
85
  }
86
  
87
  public function getCharacter2(): Character {
88
    return $this->character2;
89
  }
90
  
91
  public function getAction(): string {
92
    return $this->action;
93
  }
94
  
95
  public function getName(): string {
96
    return $this->name;
97
  }
98
  
99
  public function isResult(): bool {
100
    return $this->result;
101
  }
102
  
103
  public function getAmount(): int {
104
    return $this->amount;
105
  }
106
  
107
  public function getMessage(): string {
108
    return $this->message;
109
  }
110
  
111
  protected function parse(): void {
0 ignored issues
show
introduced by
Function's cyclomatic complexity (12) exceeds 10; consider refactoring the function
Loading history...
112 1
    $character1 = $this->character1->name;
113 1
    $character2 = $this->character2->name;
114 1
    $text = "";
115 1
    switch($this->action) {
116 1
      case static::ACTION_ATTACK:
117 1
        if($this->result) {
118 1
          $text = $this->translator->translate("combat.log.attackHits", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 147 characters
Loading history...
119 1
          if($this->character2->hitpoints < 1) {
120 1
            $text .= $this->translator->translate("combat.log.characterFalls");
121
          }
122
        } else {
123 1
          $text = $this->translator->translate("combat.log.attackFails", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 148 characters
Loading history...
124
        }
125 1
        break;
126 1
      case static::ACTION_SKILL_ATTACK:
127 1
        if($this->result) {
128 1
          $text = $this->translator->translate("combat.log.specialAttackHits", $this->amount, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 177 characters
Loading history...
129 1
          if($this->character2->hitpoints < 1) {
130 1
            $text .= $this->translator->translate("combat.log.characterFalls");
131
          }
132
        } else {
133 1
          $text = $this->translator->translate("combat.log.specialAttackFails", $this->amount, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 178 characters
Loading history...
134
        }
135 1
        break;
136 1
      case static::ACTION_SKILL_SPECIAL:
137 1
        if($this->result) {
138 1
          $text = $this->translator->translate("combat.log.specialSkillSuccess", 0, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 167 characters
Loading history...
139
        } else {
140
          $text = $this->translator->translate("combat.log.specialSKillFailure", 0, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 167 characters
Loading history...
141
        }
142 1
        break;
143 1
      case static::ACTION_HEALING:
144 1
        if($this->result) {
145
          $text = $this->translator->translate("combat.log.healingSuccess", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 151 characters
Loading history...
146
        } else {
147 1
          $text = $this->translator->translate("combat.log.healingFailure", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 151 characters
Loading history...
148
        }
149 1
        break;
150
      case static::ACTION_POISON:
151
        $text = $this->translator->translate("combat.log.poison", $this->amount, ["character1" => $character1]);
152
        break;
153
    }
154 1
    $this->message =  $text;
155 1
  }
156
  
157
  public function __toString(): string {
158
    return $this->message;
159
  }
160
}
161
?>