Completed
Push — master ( 470d27...b902fc )
by Steve
01:39
created

src/Dice/Factories/SpecialDiceFactory.php (1 issue)

assigning incompatible types to properties.

Bug Documentation Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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