Completed
Push — master ( 79c16c...fd607f )
by Steve
01:44
created

SpecialDiceFactory::buildDice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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...
21
            'steve' => [$this, 'buildSteveDice']
22
        ];
23
    }
24
25
26
    public function handlesType(string $type) : bool
27
    {
28
        return array_key_exists(
29
            $this->normaliseType($type),
30
            $this->diceTypeCallbacks
31
        );
32
    }
33
34
    /**
35
     * @param string $type
36
     * @param int $number
37
     * @return Dice[]
38
     */
39
    public function buildDice(string $type, int $number) : array
40
    {
41
        $function = $this->diceTypeCallbacks[$this->normaliseType($type)];
42
        return $function($type, $number);
43
    }
44
45
    private function buildSteveDice($_type, $diceCount)
46
    {
47
        $newDice = [];
48
        for ($i = 0; $i < $diceCount; $i++) {
49
            $newDice[] = new SteveDice();
50
        }
51
        return $newDice;
52
    }
53
54
    private function normaliseType(string $type): string
55
    {
56
        return strtolower($type);
57
    }
58
}
59