Completed
Push — master ( ba922a...9c7654 )
by Milos
03:24
created

Path::isMonotonouslyIncreasingX()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Cgi\Calc\Point;
4
5
use Cgi\Calc\Point;
6
7
class Path implements \Countable
8
{
9
    /** @var Point[] */
10
    private $points;
11
12
    /**
13
     * @param Point[] $points
14
     */
15
    public function __construct(array $points)
16
    {
17
        $this->points = [];
18
        foreach ($points as $point) {
19
            if ($point instanceof Point) {
20
                $this->points[] = $point;
21
            } else {
22
                throw new \InvalidArgumentException('Expected instance of Point class');
23
            }
24
        }
25
    }
26
27
    /**
28
     * @return Point[]
29
     */
30
    public function getPoints()
31
    {
32
        return $this->points;
33
    }
34
35
    /**
36
     * @return bool
37
     */
38
    public function isMonotonouslyIncreasingX()
39
    {
40
        return count($this->points) > 1 && $this->getMonotony($this->getAllX()) === 1;
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    public function isMonotonouslyIncreasingY()
47
    {
48
        return count($this->points) > 1 && $this->getMonotony($this->getAllY()) === 1;
49
    }
50
51
    /**
52
     * @return int|float[]
53
     */
54
    public function getAllX()
55
    {
56
        return array_map(function (Point $p) {
57
            return $p->getX();
58
        }, $this->points);
59
    }
60
61
    /**
62
     * @return int|float[]
63
     */
64
    public function getAllY()
65
    {
66
        return array_map(function (Point $p) {
67
            return $p->getY();
68
        }, $this->points);
69
    }
70
71
    /**
72
     * @return int
73
     */
74
    public function count()
75
    {
76
        return count($this->points);
77
    }
78
79
    /**
80
     * @return Point|null
81
     */
82
    public function firstPoint()
83
    {
84
        return reset($this->points);
85
    }
86
87
    /**
88
     * @return Point|null
89
     */
90
    public function lastPoint()
91
    {
92
        return end($this->points);
93
    }
94
95
    /**
96
     * @return Line
97
     */
98
    public function firstLine()
99
    {
100
        return new Line($this->points[0], $this->points[1]);
101
    }
102
103
    public function lastLine()
104
    {
105
        $count = count($this->points);
106
        return new Line($this->points[$count-2], $this->points[$count-1]);
107
    }
108
109
    /**
110
     * @return LineIterator
111
     */
112
    public function getLineIterator()
113
    {
114
        return new LineIterator($this);
115
    }
116
117
    /**
118
     * @return \ArrayIterator
119
     */
120
    public function getPointIterator()
121
    {
122
        return new \ArrayIterator($this->points);
123
    }
124
125
    /**
126
     * @param int[] $arr
127
     *
128
     * @return int|null Null if not monotonous, -1 if monotony decreasing, +1 if monotony increasing
129
     */
130
    public function getMonotony(array $arr)
131
    {
132
        $count = 0;
133
        $previous = null;
134
        $sign = null; // 1 for increasing, -1 for decreasing
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
        foreach ($arr as $item) {
136
            $count++;
137
            if (2 === $count) {
138
                $sign = $previous < $item ? 1 : -1; // is second element greater or smaller then first
139
            } elseif ($sign > 0 && $item < $previous) { // is still increasing
140
                return null;
141
            } elseif ($sign < 0 && $item > $previous) { // is still decreasing
142
                return null;
143
            }
144
145
            $previous = $item;
146
        }
147
148
        return $sign;
149
    }
150
}
151