Point::getY()   A
last analyzed

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 0
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $x can also be of type string. However, the property $x is declared as type integer|double. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
34
        $this->y = $y;
0 ignored issues
show
Documentation Bug introduced by
It seems like $y can also be of type string. However, the property $y is declared as type integer|double. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
Since $delimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $delimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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());
0 ignored issues
show
Bug introduced by
Since $delimiter is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $delimiter to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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