Passed
Push — master ( 0c8393...d0b1a8 )
by Jose
02:37
created

NodeHashTable::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JMGQ\AStar\Node\Collection;
4
5
use JMGQ\AStar\Node\Node;
6
7
class NodeHashTable implements \IteratorAggregate, NodeCollectionInterface
8
{
9
    /** @var Node[] */
10
    private array $nodes = [];
11
12
    /**
13
     * {@inheritdoc}
14
     */
15 7
    public function getIterator(): iterable
16
    {
17 7
        return new \ArrayIterator($this->nodes);
18
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 13
    public function extractBest(): ?Node
24
    {
25 13
        $bestNode = null;
26
27 13
        foreach ($this->nodes as $node) {
28 13
            if ($bestNode === null || $node->getF() < $bestNode->getF()) {
0 ignored issues
show
Bug introduced by
The method getF() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
            if ($bestNode === null || $node->getF() < $bestNode->/** @scrutinizer ignore-call */ getF()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29 13
                $bestNode = $node;
30
            }
31
        }
32
33 13
        if ($bestNode !== null) {
34 13
            $this->remove($bestNode);
35
        }
36
37 13
        return $bestNode;
38
    }
39
40 10
    public function get(string $nodeId): ?Node
41
    {
42 10
        return $this->nodes[$nodeId] ?? null;
43
    }
44
45 20
    public function add(Node $node): void
46
    {
47 20
        $this->nodes[$node->getId()] = $node;
48 20
    }
49
50 14
    public function remove(Node $node): void
51
    {
52 14
        unset($this->nodes[$node->getId()]);
53 14
    }
54
55 13
    public function isEmpty(): bool
56
    {
57 13
        return empty($this->nodes);
58
    }
59
60 10
    public function contains(Node $node): bool
61
    {
62 10
        return isset($this->nodes[$node->getId()]);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 13
    public function clear(): void
69
    {
70 13
        $this->nodes = [];
71 13
    }
72
}
73