AbstractLoom::diff()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Loom
4
 * 
5
 * @copyright   Copyright (c) 2015 Warrick Bayman.
6
 * @author		Warrick Bayman <[email protected]>
7
 * @license     MIT License http://opensource.org/licenses/MIT
8
 * 
9
 */
10
11
namespace Loom;
12
13
use Loom\Contracts\ArithmeticInterface;
14
use Loom\Contracts\ComparisonsInterface;
15
use Loom\Contracts\GettersInterface;
16
17
abstract class AbstractLoom implements GettersInterface, ComparisonsInterface, ArithmeticInterface
18
{
19
    /**
20
     * @var int
21
     */
22
    protected $ms = 0;
23
24
25
    /* -----------------------------------------------------------------------------------------------------------------
26
     * Getters
27
     * -----------------------------------------------------------------------------------------------------------------
28
     * Transform the Loom unit to different unit
29
     */
30
31
32
    /**
33
     * Get microseconds
34
     *
35
     * @return int
36
     */
37
    public function getMicroseconds()
38
    {
39
        return $this->ms * 1000;
40
    }
41
42
43
    /**
44
     * Get milliseconds
45
     *
46
     * @return int
47
     */
48
    public function getMilliseconds()
49
    {
50
        return $this->ms;
51
    }
52
53
54
    /**
55
     * Get seconds
56
     *
57
     * @return float
58
     */
59
    public function getSeconds()
60
    {
61
        return $this->ms / 1000;
62
    }
63
64
65
    /**
66
     * Get minutes
67
     *
68
     * @return float
69
     */
70
    public function getMinutes()
71
    {
72
        return $this->ms / 1000 / 60;
73
    }
74
75
76
    /**
77
     * Get hours
78
     *
79
     * @return float
80
     */
81
    public function getHours()
82
    {
83
        return $this->ms / 1000 / 60 / 60;
84
    }
85
86
87
    /**
88
     * Get days
89
     *
90
     * @return float
91
     */
92
    public function getDays()
93
    {
94
        return $this->ms / 1000 / 60 / 60 / 24;
95
    }
96
97
98
    /**
99
     * Get weeks
100
     *
101
     * @return float
102
     */
103
    public function getWeeks()
104
    {
105
        return $this->ms / 1000 / 60 / 60 / 24 / 7;
106
    }
107
108
109
    /**
110
     * Get months
111
     *
112
     * @param null|int $daysPerMonth
113
     *
114
     * @return float
115
     */
116
    public function getMonths($daysPerMonth = null)
117
    {
118
        return $this->ms / 1000 / 60 / 60 / 24 / (is_null($daysPerMonth) ? 365 / 12 : $daysPerMonth);
119
    }
120
121
122
    /**
123
     * Get years
124
     *
125
     * @param bool $solar
126
     *
127
     * @return float
128
     */
129
    public function getYears($solar = false)
130
    {
131
        $solarDays = 365.2421897;
132
        return $this->ms / 1000 / 60 / 60 / 24 / ($solar ? $solarDays : 365);
133
    }
134
135
136
137
138
    /**
139
     * Get a new DateTime instance
140
     *
141
     * @return \DateTime
142
     */
143
    public function getDateTime()
144
    {
145
        return new \DateTime('@' . $this->getSeconds());
146
    }
147
148
149
    /* -----------------------------------------------------------------------------------------------------------------
150
     * Comparisons
151
     * -----------------------------------------------------------------------------------------------------------------
152
     * Equality comparisons and validators.
153
     */
154
155
156
    /**
157
     * Test equality
158
     *
159
     * @param Loom $loom
160
     *
161
     * @return bool
162
     */
163
    public function eq(Loom $loom)
164
    {
165
        if ($loom->getMilliseconds() == $this->ms) {
166
            return true;
167
        }
168
        return false;
169
    }
170
171
172
    /**
173
     * Test not equal to
174
     *
175
     * @param Loom $loom
176
     *
177
     * @return bool
178
     */
179
    public function ne(Loom $loom)
180
    {
181
        return ($this->ms != $loom->getMilliseconds());
182
    }
183
184
185
    /**
186
     * Test less than
187
     *
188
     * @param Loom $loom
189
     *
190
     * @return bool
191
     */
192
    public function lt(Loom $loom)
193
    {
194
        return ($this->ms < $loom->getMilliseconds());
195
    }
196
197
198
    /**
199
     * Test less than or equal to
200
     *
201
     * @param Loom $loom
202
     *
203
     * @return bool
204
     */
205
    public function lte(Loom $loom)
206
    {
207
        return ($this->ms <= $loom->getMilliseconds());
208
    }
209
210
211
    /**
212
     * Test greater than
213
     *
214
     * @param Loom $loom
215
     *
216
     * @return bool
217
     */
218
    public function gt(Loom $loom)
219
    {
220
        return ($this->ms > $loom->getMilliseconds());
221
    }
222
223
224
    /**
225
     * Test greater than or equal to
226
     *
227
     * @param Loom $loom
228
     *
229
     * @return bool
230
     */
231
    public function gte(Loom $loom)
232
    {
233
        return ($this->ms >= $loom->getMilliseconds());
234
    }
235
236
237
    /**
238
     * Get the difference between
239
     *
240
     * @param Loom $loom
241
     *
242
     * @return Loom
243
     */
244
    public function diff(Loom $loom)
245
    {
246
        $diff = $loom->getMilliseconds() - $this->ms;
247
        if ($diff < 0) {
248
            $diff = -$diff;
249
        }
250
251
        return Loom::make()->fromMilliseconds($diff);
252
    }
253
254
255
    /**
256
     * Is between two units
257
     *
258
     * @param Loom $start
259
     * @param Loom $end
260
     * @param bool $inclusive
261
     *
262
     * @return bool
263
     */
264
    public function isBetween(Loom $start, Loom $end, $inclusive = false)
265
    {
266
        $startMs = $start->getMilliseconds();
267
        $endMs = $end->getMilliseconds();
268
269
        if ($inclusive) {
270
            return $this->getMilliseconds() >= $startMs && $this->getMilliseconds() <= $endMs;
271
        }
272
        return $this->getMilliseconds() > $startMs && $this->getMilliseconds() < $endMs;
273
    }
274
275
276
    /* -----------------------------------------------------------------------------------------------------------------
277
     * Arithmetic
278
     * -----------------------------------------------------------------------------------------------------------------
279
     * Add a Loom instance, or subtract a Loom instance.
280
     */
281
282
283
    /**
284
     * Add a Loom
285
     *
286
     * @param Loom|AbstractUnit $loom
287
     *
288
     * @return Loom
289
     */
290
    public function add($loom)
291
    {
292 View Code Duplication
        switch (get_parent_class($loom)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
293
            case 'Loom\Loom':
294
            case 'Loom\AbstractLoom':
295
                $this->ms = $this->ms + $loom->getMilliseconds();
0 ignored issues
show
Bug introduced by
The method getMilliseconds does only exist in Loom\Loom, but not in Loom\AbstractUnit.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
296
                break;
297
            case 'Loom\AbstractUnit':
298
                $this->ms = $this->ms + $loom->toMilliseconds();
0 ignored issues
show
Bug introduced by
The method toMilliseconds does only exist in Loom\AbstractUnit, but not in Loom\Loom.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
299
        }
300
301
        return $this;
302
    }
303
304
305
    /**
306
     * Subtract a Loom
307
     *
308
     * @param Loom|AbstractUnit $loom
309
     *
310
     * @return Loom
311
     */
312
    public function sub($loom)
313
    {
314 View Code Duplication
        switch(get_parent_class($loom)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
315
            case 'Loom\Loom':
316
            case 'Loom\AbstractLoom':
317
                $this->ms = $this->ms - $loom->getMilliseconds();
0 ignored issues
show
Bug introduced by
The method getMilliseconds does only exist in Loom\Loom, but not in Loom\AbstractUnit.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Documentation Bug introduced by
It seems like $this->ms - $loom->getMilliseconds() can also be of type double. However, the property $ms is declared as type integer. 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...
318
                break;
319
            case 'Loom\AbstractUnit':
320
                $this->ms = $this->ms - $loom->toMilliseconds();
0 ignored issues
show
Bug introduced by
The method toMilliseconds does only exist in Loom\AbstractUnit, but not in Loom\Loom.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Documentation Bug introduced by
It seems like $this->ms - $loom->toMilliseconds() can also be of type double. However, the property $ms is declared as type integer. 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...
321
                break;
322
        }
323
324
        if ($this->ms < 0) {
325
            $this->ms = 0;
326
        }
327
        return $this;
328
    }
329
330
331
    /**
332
     * Get time since
333
     *
334
     * @return Loom
335
     */
336
    public function since()
337
    {
338
        $now = (new \DateTime('now'))->getTimestamp();
339
        $since = $this->diff(Loom::make()->fromSeconds($now));
340
341
        return $since;
342
    }
343
344
345
    /**
346
     * Get time until
347
     *
348
     * @return Loom
349
     */
350
    public function until()
351
    {
352
        return $this->since();
353
    }
354
}
355