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\Environment; |
||
9 | use Stratadox\Pathfinder\Labels; |
||
10 | use Stratadox\Pathfinder\Position; |
||
11 | |||
12 | final class GeometricGraph implements Environment |
||
13 | { |
||
14 | /** @var GeometricVertex[] */ |
||
15 | private $vertices; |
||
16 | private $ids; |
||
17 | |||
18 | private function __construct(GeometricVertex ...$vertices) |
||
19 | { |
||
20 | /** @var GeometricVertex[] */ |
||
21 | $verticesByLabel = array_combine( |
||
22 | array_map(function (Vertex $vertex): string { |
||
23 | return $vertex->label(); |
||
24 | }, $vertices), |
||
25 | $vertices |
||
26 | ); |
||
27 | $this->vertices = $verticesByLabel; |
||
0 ignored issues
–
show
|
|||
28 | $this->ids = Ids::for(...$vertices); |
||
29 | } |
||
30 | |||
31 | public static function with(GeometricVertex ...$vertices): Environment |
||
32 | { |
||
33 | return new self(...$vertices); |
||
34 | } |
||
35 | |||
36 | public function all(): Labels |
||
37 | { |
||
38 | return $this->ids; |
||
39 | } |
||
40 | |||
41 | public function has(string $node): bool |
||
42 | { |
||
43 | return isset($this->vertices[$node]); |
||
44 | } |
||
45 | |||
46 | public function neighboursOf(string $source): Labels |
||
47 | { |
||
48 | $neighbours = []; |
||
49 | foreach ($this->vertices[$source]->edges() as $edge) { |
||
50 | $neighbours[] = $edge->target(); |
||
51 | } |
||
52 | return Ids::consistingOf(...$neighbours); |
||
53 | } |
||
54 | |||
55 | public function areNeighbours(string $source, string $neighbour): bool |
||
56 | { |
||
57 | foreach ($this->vertices[$source]->edges() as $edge) { |
||
58 | if ($neighbour === $edge->target()) { |
||
59 | return true; |
||
60 | } |
||
61 | } |
||
62 | return false; |
||
63 | } |
||
64 | |||
65 | public function movementCostBetween(string $source, string $neighbour): float |
||
66 | { |
||
67 | foreach ($this->vertices[$source]->edges() as $edge) { |
||
68 | if ($neighbour === $edge->target()) { |
||
69 | return $edge->cost(); |
||
70 | } |
||
71 | } |
||
72 | throw new InvalidArgumentException( |
||
73 | "Vertices $source and $neighbour are not neighbours." |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | public function positionOf(string $node): Position |
||
78 | { |
||
79 | return $this->vertices[$node]->position(); |
||
80 | } |
||
81 | |||
82 | public function hasNegativeEdgeCosts(): bool |
||
83 | { |
||
84 | foreach ($this->vertices as $vertex) { |
||
85 | if ($vertex->hasNegativeEdges()) { |
||
86 | return true; |
||
87 | } |
||
88 | } |
||
89 | return false; |
||
90 | } |
||
91 | } |
||
92 |
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 theid
property of an instance of theAccount
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.