NodeHashTable::contains()   A
last analyzed

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
/**
8
 * @template TState
9
 * @implements \IteratorAggregate<mixed, Node<TState>>
10
 * @implements NodeCollectionInterface<TState>
11
 * @internal
12
 */
13
class NodeHashTable implements \IteratorAggregate, NodeCollectionInterface
14
{
15
    /** @var Node<TState>[] */
16
    private array $nodes = [];
17
18
    /**
19
     * {@inheritdoc}
20
     * @return \ArrayIterator<array-key, Node<TState>>
21
     */
22 7
    public function getIterator(): \Traversable
23
    {
24 7
        return new \ArrayIterator($this->nodes);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 13
    public function extractBest(): ?Node
31
    {
32 13
        $bestNode = null;
33
34 13
        foreach ($this->nodes as $node) {
35 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

35
            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...
36 13
                $bestNode = $node;
37
            }
38
        }
39
40 13
        if ($bestNode !== null) {
41 13
            $this->remove($bestNode);
42
        }
43
44 13
        return $bestNode;
45
    }
46
47 10
    public function get(string $nodeId): ?Node
48
    {
49 10
        return $this->nodes[$nodeId] ?? null;
50
    }
51
52 20
    public function add(Node $node): void
53
    {
54 20
        $this->nodes[$node->getId()] = $node;
55 20
    }
56
57 14
    public function remove(Node $node): void
58
    {
59 14
        unset($this->nodes[$node->getId()]);
60 14
    }
61
62 13
    public function isEmpty(): bool
63
    {
64 13
        return empty($this->nodes);
65
    }
66
67 10
    public function contains(Node $node): bool
68
    {
69 10
        return isset($this->nodes[$node->getId()]);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 13
    public function clear(): void
76
    {
77 13
        $this->nodes = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type JMGQ\AStar\Node\Node of property $nodes.

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...
78 13
    }
79
}
80