MultiCoordinateCommandOption::isQuadrilateral()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jackal\ImageMerge\Command\Options;
4
5
use Jackal\ImageMerge\ValueObject\Coordinate;
6
use Jackal\ImageMerge\ValueObject\Dimention;
7
8
/**
9
 * Class MultiCoordinateCommandOption
10
 * @package Jackal\ImageMerge\Command\Options
11
 */
12
class MultiCoordinateCommandOption extends AbstractCommandOption
13
{
14
    /**
15
     * @var Coordinate[]
16
     */
17
    private $args;
18
19
    /**
20
     * MultiCoordinateCommandOption constructor.
21
     * @param SingleCoordinateCommandOption[] $coords
22
     */
23
    public function __construct(array $coords)
24
    {
25
        $this->args = $coords;
0 ignored issues
show
Documentation Bug introduced by
It seems like $coords of type Jackal\ImageMerge\Comman...ordinateCommandOption[] is incompatible with the declared type Jackal\ImageMerge\ValueObject\Coordinate[] of property $args.

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...
26
    }
27
28
    private function getOddValues()
29
    {
30
        $arr = [];
31
        foreach ($this->toArray() as $k => $point) {
32
            if ($k % 2 == 1) {
33
                $arr[] = $point;
34
            }
35
        }
36
37
        return $arr;
38
    }
39
40
    private function getEvenValues()
41
    {
42
        $arr = [];
43
        foreach ($this->toArray() as $k => $point) {
44
            if ($k == 0 or ($k % 2 == 0)) {
45
                $arr[] = $point;
46
            }
47
        }
48
49
        return $arr;
50
    }
51
52
    /**
53
     * @return Coordinate[]
54
     */
55
    public function getCoordinates()
56
    {
57
        $coords = [];
58
        $points = $this->toArray();
59
        foreach ($points as $k => $coordinateCommandOption) {
60
            if ($k == 0) {
61
                $coords[] = new Coordinate($coordinateCommandOption, $points[$k + 1]);
62
            } else {
63
                if (($k % 2) == 0) {
64
                    $coords[] = new Coordinate($coordinateCommandOption, $points[$k + 1]);
65
                }
66
            }
67
        }
68
69
        return $coords;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function toArray()
76
    {
77
        $points = [];
78
        /** @var Coordinate $arg */
79
        foreach ($this->args as $arg) {
80
            $points[] = $arg->getX();
81
            $points[] = $arg->getY();
82
        }
83
84
        return $points;
85
    }
86
87
    /**
88
     * @return int
89
     */
90
    public function countPoints()
91
    {
92
        return count($this->args);
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98
    public function getMinX()
99
    {
100
        return min($this->getEvenValues());
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106
    public function getMinY()
107
    {
108
        return min($this->getOddValues());
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114
    public function getMaxX()
115
    {
116
        return max($this->getEvenValues());
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122
    public function getMaxY()
123
    {
124
        return max($this->getOddValues());
125
    }
126
127
    /**
128
     * @return Dimention
129
     */
130
    public function getCropDimention()
131
    {
132
        return new Dimention($this->getMaxX() - $this->getMinX(), $this->getMaxY() - $this->getMinY());
133
    }
134
135
    public function isQuadrilateral()
136
    {
137
        return $this->countPoints() == 4;
138
    }
139
}
140