Passed
Push — master ( 606b80...d10f8a )
by Jakub
02:32
created

InitiativeFormulaParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 17
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A calculateInitiative() 0 14 2
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
/**
7
 * InitiativeFormulaParser
8
 *
9
 * @author Jakub Konečný
10
 */
11 1
final class InitiativeFormulaParser implements IInitiativeFormulaParser {
12 1
  use \Nette\SmartObject;
13
  
14
  public function calculateInitiative(string $formula, Character $character): int {
15 1
    $result = 0;
16
    $stats = [
17 1
      "INT" => $character->intelligence, "DEX" => $character->dexterity, "STR" => $character->strength, "CON" => $character->constitution,
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 138 characters
Loading history...
18 1
      "CHAR" => $character->charisma,
19
    ];
20 1
    $formula = str_replace(array_keys($stats), array_values($stats), $formula);
21 1
    preg_match_all("/^([1-9]+)d([1-9]+)/", $formula, $dices);
22 1
    for($i = 1; $i <= (int) $dices[1][0]; $i++) {
23 1
      $result += rand(1, (int) $dices[2][0]);
24
    }
25 1
    preg_match_all("/\+([0-9]+)\/([0-9]+)/", $formula, $ammendum);
26 1
    $result += (int) $ammendum[1][0] / (int) $ammendum[2][0];
27 1
    return (int) $result;
28
  }  
29
}
30
?>