Coordinate::inFrame()   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 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Crossword\Features\Constructor\Scanner\Grid;
6
7
use JsonSerializable;
8
use Stringable;
9
10
/**
11
 * @psalm-immutable
12
 */
13
final class Coordinate implements JsonSerializable, Stringable
14
{
15
    private int $coordinateX;
16
    private int $coordinateY;
17
18
    public function __construct(int $coordinateX, int $coordinateY)
19
    {
20
        $this->coordinateX = $coordinateX;
21
        $this->coordinateY = $coordinateY;
22
    }
23
24 2
    public function inFrame(): bool
25
    {
26 2
        return 0 < $this->coordinateX && 0 < $this->coordinateY;
27
    }
28
29 1
    public function left(): self
30
    {
31 1
        return new self($this->coordinateX - 1, $this->coordinateY);
32
    }
33
34 1
    public function right(): self
35
    {
36 1
        return new self($this->coordinateX + 1, $this->coordinateY);
37
    }
38
39 1
    public function top(): self
40
    {
41 1
        return new self($this->coordinateX, $this->coordinateY + 1);
42
    }
43
44 1
    public function down(): self
45
    {
46 1
        return new self($this->coordinateX, $this->coordinateY - 1);
47
    }
48
49
    public function jsonSerialize(): array
50
    {
51
        return [
52
            'x' => $this->coordinateX,
53
            'y' => $this->coordinateY,
54
        ];
55
    }
56
57
    public function __toString(): string
58
    {
59
        return sprintf('%d.%d', $this->coordinateX, $this->coordinateY);
60
    }
61
}
62