Completed
Push — develop ( e322b0...461e3c )
by Barry
02:41
created

Neighborhood   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 119
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getMooreNeighbors() 0 21 5
A wrapRow() 0 9 2
A getNeighbors() 0 7 1
B getNeumannNeighbors() 0 25 6
A wrapColumn() 0 9 2
1
<?php
2
3
namespace Barryvanveen\CCA;
4
5
class Neighborhood
6
{
7
    /** @var Config */
8
    protected $config;
9
10
    /** @var Coordinate */
11
    protected $coordinate;
12
13
    /**
14
     * Neighborhood constructor.
15
     *
16
     * @param Config     $config
17
     * @param Coordinate $coordinate
18
     */
19 21
    public function __construct(Config $config, Coordinate $coordinate)
20
    {
21 21
        $this->config = $config;
22
23 21
        $this->coordinate = $coordinate;
24 21
    }
25
26
    /**
27
     * @return Coordinate[]
28
     *
29
     * @throws \Barryvanveen\CCA\Exceptions\InvalidNeighborhoodTypeException
30
     */
31 21
    public function getNeighbors(): array
32
    {
33 21
        $neighborhoodType = $this->config->neighborhoodType();
34
35 21
        $method = 'get'.ucfirst($neighborhoodType).'Neighbors';
36
37 21
        return $this->{$method}($this->coordinate);
38
    }
39
40
    /**
41
     * Retrieve an array of Moore neighborhood neighbors for the given coordinate.
42
     *
43
     * @param Coordinate $coordinate
44
     *
45
     * @return Coordinate[]
46
     */
47 12
    protected function getMooreNeighbors(Coordinate $coordinate): array
48
    {
49 12
        $neigbors = [];
50
51 12
        $size = abs($this->config->neighborhoodSize());
52
53 12
        for ($rowOffset = -1 * $size; $rowOffset <= $size; $rowOffset++) {
54 12
            for ($columnOffset = -1 * $size; $columnOffset <= $size; $columnOffset++) {
55 12
                if ($rowOffset === 0 && $columnOffset === 0) {
56 12
                    continue;
57
                }
58
59 12
                $neigbors[] = new Coordinate(
60 12
                    $this->wrapRow($coordinate->row(), $rowOffset),
0 ignored issues
show
Bug introduced by
$rowOffset of type double is incompatible with the type integer expected by parameter $rowOffset of Barryvanveen\CCA\Neighborhood::wrapRow(). ( Ignorable by Annotation )

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

60
                    $this->wrapRow($coordinate->row(), /** @scrutinizer ignore-type */ $rowOffset),
Loading history...
61 12
                    $this->wrapColumn($coordinate->column(), $columnOffset),
0 ignored issues
show
Bug introduced by
$columnOffset of type double is incompatible with the type integer expected by parameter $columnOffset of Barryvanveen\CCA\Neighborhood::wrapColumn(). ( Ignorable by Annotation )

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

61
                    $this->wrapColumn($coordinate->column(), /** @scrutinizer ignore-type */ $columnOffset),
Loading history...
62 12
                    $this->config->columns()
63
                );
64
            }
65
        }
66
67 12
        return $neigbors;
68
    }
69
70
    /**
71
     * Retrieve an array of Neumann neighborhood neighbors for the given coordinate.
72
     *
73
     * @param Coordinate $coordinate
74
     *
75
     * @return Coordinate[]
76
     */
77 9
    protected function getNeumannNeighbors(Coordinate $coordinate): array
78
    {
79 9
        $neigbors = [];
80
81 9
        $size = abs($this->config->neighborhoodSize());
82
83 9
        for ($rowOffset = -1 * $size; $rowOffset <= $size; $rowOffset++) {
84 9
            for ($columnOffset = -1 * $size; $columnOffset <= $size; $columnOffset++) {
85 9
                if ($rowOffset === 0 && $columnOffset === 0) {
86 9
                    continue;
87
                }
88
89 9
                if ((abs($rowOffset) + abs($columnOffset)) > $this->config->neighborhoodSize()) {
90 9
                    continue;
91
                }
92
93 9
                $neigbors[] = new Coordinate(
94 9
                    $this->wrapRow($coordinate->row(), $rowOffset),
0 ignored issues
show
Bug introduced by
$rowOffset of type double is incompatible with the type integer expected by parameter $rowOffset of Barryvanveen\CCA\Neighborhood::wrapRow(). ( Ignorable by Annotation )

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

94
                    $this->wrapRow($coordinate->row(), /** @scrutinizer ignore-type */ $rowOffset),
Loading history...
95 9
                    $this->wrapColumn($coordinate->column(), $columnOffset),
0 ignored issues
show
Bug introduced by
$columnOffset of type double is incompatible with the type integer expected by parameter $columnOffset of Barryvanveen\CCA\Neighborhood::wrapColumn(). ( Ignorable by Annotation )

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

95
                    $this->wrapColumn($coordinate->column(), /** @scrutinizer ignore-type */ $columnOffset),
Loading history...
96 9
                    $this->config->columns()
97
                );
98
            }
99
        }
100
101 9
        return $neigbors;
102
    }
103
104 21
    protected function wrapRow(int $row, int $rowOffset): int
105
    {
106 21
        $row = $row + $rowOffset;
107
108 21
        if ($row < 0) {
109 21
            return $row + $this->config->rows();
110
        }
111
112 21
        return $row % $this->config->rows();
113
    }
114
115 21
    protected function wrapColumn(int $column, int $columnOffset): int
116
    {
117 21
        $column = $column + $columnOffset;
118
119 21
        if ($column < 0) {
120 21
            return $column + $this->config->columns();
121
        }
122
123 21
        return $column % $this->config->columns();
124
    }
125
}
126