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

SpecialDiceFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 53
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A handlesType() 0 7 1
A buildDice() 0 5 1
A normaliseType() 0 4 1
A buildNDice() 0 8 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