1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CGI-Calc package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cgi\Calc; |
13
|
|
|
|
14
|
|
|
class Point |
15
|
|
|
{ |
16
|
|
|
private static $delimiter = '|'; |
17
|
|
|
|
18
|
|
|
/** @var int|float */ |
19
|
|
|
private $x; |
20
|
|
|
|
21
|
|
|
/** @var int|float */ |
22
|
|
|
private $y; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param int $x |
26
|
|
|
* @param int $y |
27
|
|
|
*/ |
28
|
|
|
public function __construct($x, $y) |
29
|
|
|
{ |
30
|
|
|
if (false === is_numeric($x) || false === is_numeric($y)) { |
31
|
|
|
throw new \InvalidArgumentException('Number expected'); |
32
|
|
|
} |
33
|
|
|
$this->x = $x; |
|
|
|
|
34
|
|
|
$this->y = $y; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $hash |
39
|
|
|
* |
40
|
|
|
* @return Point |
41
|
|
|
*/ |
42
|
|
|
public static function parse($hash) |
43
|
|
|
{ |
44
|
|
|
$arr = explode(static::$delimiter, $hash); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return new self($arr[0], $arr[1]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function hash() |
53
|
|
|
{ |
54
|
|
|
return sprintf('%s%s%s', $this->getX(), static::$delimiter, $this->getY()); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return int|float |
59
|
|
|
*/ |
60
|
|
|
public function getX() |
61
|
|
|
{ |
62
|
|
|
return $this->x; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int|float |
67
|
|
|
*/ |
68
|
|
|
public function getY() |
69
|
|
|
{ |
70
|
|
|
return $this->y; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return int|float |
75
|
|
|
*/ |
76
|
|
|
public function length() |
77
|
|
|
{ |
78
|
|
|
return abs($this->getX()) + abs($this->getY()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return float |
83
|
|
|
*/ |
84
|
|
|
public function distance() |
85
|
|
|
{ |
86
|
|
|
return sqrt($this->getX() * $this->getX() + $this->getY() * $this->getY()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Point |
91
|
|
|
*/ |
92
|
|
|
public function left() |
93
|
|
|
{ |
94
|
|
|
return new self($this->getX() - 1, $this->getY()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Point |
99
|
|
|
*/ |
100
|
|
|
public function right() |
101
|
|
|
{ |
102
|
|
|
return new self($this->getX() + 1, $this->getY()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Point |
107
|
|
|
*/ |
108
|
|
|
public function up() |
109
|
|
|
{ |
110
|
|
|
return new self($this->getX(), $this->getY() + 1); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Point |
115
|
|
|
*/ |
116
|
|
|
public function down() |
117
|
|
|
{ |
118
|
|
|
return new self($this->getX(), $this->getY() - 1); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Point $by |
123
|
|
|
* |
124
|
|
|
* @return Point |
125
|
|
|
*/ |
126
|
|
|
public function move(Point $by) |
127
|
|
|
{ |
128
|
|
|
return new self($this->getX() + $by->getX(), $this->getY() + $by->getY()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Point $of |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function isLeft(Point $of) |
137
|
|
|
{ |
138
|
|
|
return $this->getX() < $of->getX(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Point $of |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isRight(Point $of) |
147
|
|
|
{ |
148
|
|
|
return $this->getX() > $of->getX(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Point $of |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function isAbove(Point $of) |
157
|
|
|
{ |
158
|
|
|
return $this->getY() > $of->getY(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param Point $of |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function isBelow(Point $of) |
167
|
|
|
{ |
168
|
|
|
return $this->getY() < $of->getY(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param Point $as |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function sameX(Point $as) |
177
|
|
|
{ |
178
|
|
|
return $this->getX() === $as->getX(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Point $as |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function sameY(Point $as) |
187
|
|
|
{ |
188
|
|
|
return $this->getY() === $as->getY(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param Point $other |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function equals(Point $other) |
197
|
|
|
{ |
198
|
|
|
return $this->x === $other->getX() && $this->y === $other->getY(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param Point $a |
203
|
|
|
* @param Point $b |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function betweenX(Point $a, Point $b) |
208
|
|
|
{ |
209
|
|
|
return $a->getX() <= $this->getX() && $this->getX() < $b->getX(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param Point $a |
214
|
|
|
* @param Point $b |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
public function betweenY(Point $a, Point $b) |
219
|
|
|
{ |
220
|
|
|
return $a->getY() <= $this->getY() && $this->getY() < $b->getY(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param Point|int $upTo |
225
|
|
|
* @param int|float $step |
226
|
|
|
* |
227
|
|
|
* @return \Traversable |
228
|
|
|
*/ |
229
|
|
View Code Duplication |
public function forXUpTo($upTo, $step = 1) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
if ($upTo instanceof self) { |
232
|
|
|
$upTo = $upTo->getX(); |
233
|
|
|
} elseif (false === is_int($upTo)) { |
234
|
|
|
throw new \InvalidArgumentException('UpTo argument must be Point or integer'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
for ($x = $this->getX(); $x <= $upTo; $x += $step) { |
238
|
|
|
yield $x; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param Point|int $upTo |
244
|
|
|
* @param int|float $step |
245
|
|
|
* |
246
|
|
|
* @return \Traversable |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function forYUpTo($upTo, $step = 1) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
if ($upTo instanceof self) { |
251
|
|
|
$upTo = $upTo->getY(); |
252
|
|
|
} elseif (false === is_int($upTo)) { |
253
|
|
|
throw new \InvalidArgumentException('UpTo argument must be Point or integer'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
for ($y = $this->getY(); $y <= $upTo; $y += $step) { |
257
|
|
|
yield $y; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param int $count |
263
|
|
|
* |
264
|
|
|
* @return \Traversable |
265
|
|
|
*/ |
266
|
|
|
public function forXTimes($count) |
267
|
|
|
{ |
268
|
|
|
if (false === is_int($count)) { |
269
|
|
|
throw new \InvalidArgumentException('Count argument must be integer'); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
foreach ($this->forXUpTo($this->getX() + $count - 1) as $x) { |
273
|
|
|
yield $x; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param int $count |
279
|
|
|
* |
280
|
|
|
* @return \Traversable |
281
|
|
|
*/ |
282
|
|
|
public function forYTimes($count) |
283
|
|
|
{ |
284
|
|
|
if (false === is_int($count)) { |
285
|
|
|
throw new \InvalidArgumentException('Count argument must be integer'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
foreach ($this->forYUpTo($this->getY() + $count - 1) as $y) { |
289
|
|
|
yield $y; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.