Position::isAdjacentTo()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace JMGQ\AStar\Example\Terrain;
4
5
use JMGQ\AStar\Node\NodeIdentifierInterface;
6
7
class Position implements NodeIdentifierInterface
8
{
9
    private int $row;
10
    private int $column;
11
12 51
    public function __construct(int $row, int $column)
13
    {
14 51
        $this->validateNonNegativeInteger($row);
15 50
        $this->validateNonNegativeInteger($column);
16
17 49
        $this->row = $row;
18 49
        $this->column = $column;
19 49
    }
20
21 40
    public function getRow(): int
22
    {
23 40
        return $this->row;
24
    }
25
26 36
    public function getColumn(): int
27
    {
28 36
        return $this->column;
29
    }
30
31 13
    public function isEqualTo(Position $other): bool
32
    {
33 13
        return $this->getRow() === $other->getRow() && $this->getColumn() === $other->getColumn();
34
    }
35
36 24
    public function isAdjacentTo(Position $other): bool
37
    {
38 24
        return abs($this->getRow() - $other->getRow()) <= 1 && abs($this->getColumn() - $other->getColumn()) <= 1;
39
    }
40
41 8
    public function getUniqueNodeId(): string
42
    {
43 8
        return $this->row . 'x' . $this->column;
44
    }
45
46 51
    private function validateNonNegativeInteger(int $integer): void
47
    {
48 51
        if ($integer < 0) {
49 2
            throw new \InvalidArgumentException("Invalid non negative integer: $integer");
50
        }
51 50
    }
52
}
53