Issues (2)

src/Graph/Graph.php (1 issue)

1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Pathfinder\Graph;
4
5
use function array_combine;
6
use function array_map;
7
use InvalidArgumentException;
8
use Stratadox\Pathfinder\Network;
9
use Stratadox\Pathfinder\Labels;
10
11
final class Graph implements Network
12
{
13
    /** @var Vertex[] */
14
    private $vertices;
15
    private $ids;
16
17
    private function __construct(Vertex ...$vertices)
18
    {
19
        /** @var Vertex[] */
20
        $verticesByLabel = array_combine(
21
            array_map(function (Vertex $vertex): string {
22
                return $vertex->label();
23
            }, $vertices),
24
            $vertices
25
        );
26
        $this->vertices = $verticesByLabel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $verticesByLabel can also be of type false. However, the property $vertices is declared as type Stratadox\Pathfinder\Graph\Vertex[]. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
27
        $this->ids = Ids::for(...$vertices);
28
    }
29
30
    public static function with(Vertex ...$vertices): Network
31
    {
32
        return new self(...$vertices);
33
    }
34
35
    public function all(): Labels
36
    {
37
        return $this->ids;
38
    }
39
40
    public function has(string $node): bool
41
    {
42
        return isset($this->vertices[$node]);
43
    }
44
45
    public function neighboursOf(string $source): Labels
46
    {
47
        $neighbours = [];
48
        foreach ($this->vertices[$source]->edges() as $edge) {
49
            $neighbours[] = $edge->target();
50
        }
51
        return Ids::consistingOf(...$neighbours);
52
    }
53
54
    public function areNeighbours(string $source, string $neighbour): bool
55
    {
56
        foreach ($this->vertices[$source]->edges() as $edge) {
57
            if ($neighbour === $edge->target()) {
58
                return true;
59
            }
60
        }
61
        return false;
62
    }
63
64
    public function movementCostBetween(string $source, string $neighbour): float
65
    {
66
        foreach ($this->vertices[$source]->edges() as $edge) {
67
            if ($neighbour === $edge->target()) {
68
                return $edge->cost();
69
            }
70
        }
71
        throw new InvalidArgumentException(
72
            "Vertices $source and $neighbour are not neighbours."
73
        );
74
    }
75
76
    public function hasNegativeEdgeCosts(): bool
77
    {
78
        foreach ($this->vertices as $vertex) {
79
            if ($vertex->hasNegativeEdges()) {
80
                return true;
81
            }
82
        }
83
        return false;
84
    }
85
}
86