Completed
Branch master (9cbb3f)
by Steve
04:25
created

SpecialDiceFactory::buildNDice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace MeadSteve\DiceApi\Dice\Factories;
4
5
use MeadSteve\DiceApi\Dice;
6
7
class SpecialDiceFactory implements DiceFactory
8
{
9
    /**
10
     * @var callable[]
11
     */
12
    private $diceTypeMappings;
13
14
    public function __construct()
15
    {
16
        $this->diceTypeMappings = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('steve' => functio...Api\Dice\FateDice(); }) of type array<string,object<Clos...te":"object<Closure>"}> is incompatible with the declared type array<integer,callable> of property $diceTypeMappings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
17
            'steve' => function () {
18
                return new Dice\SteveDice();
19
            },
20
            'fate'  => function () {
21
                return new Dice\FateDice();
22
            },
23
        ];
24
    }
25
26
27
    public function handlesType(string $type) : bool
28
    {
29
        return array_key_exists(
30
            $this->normaliseType($type),
31
            $this->diceTypeMappings
32
        );
33
    }
34
35
    /**
36
     * @param string $type
37
     * @param int $number
38
     * @return Dice[]
39
     */
40
    public function buildDice(string $type, int $number) : array
41
    {
42
        $diceConstructor = $this->diceTypeMappings[$this->normaliseType($type)];
43
        return $this->buildNDice($number, $diceConstructor);
44
    }
45
46
    private function normaliseType(string $type): string
47
    {
48
        return strtolower($type);
49
    }
50
51
    private function buildNDice($diceCount, callable $constructorFunction)
52
    {
53
        $newDice = [];
54
        for ($i = 0; $i < $diceCount; $i++) {
55
            $newDice[] = $constructorFunction();
56
        }
57
        return $newDice;
58
    }
59
}
60