1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Baleen\Migrations\Common\Event; |
21
|
|
|
|
22
|
|
|
use Baleen\Migrations\Exception\InvalidArgumentException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Progress. |
26
|
|
|
* |
27
|
|
|
* @author Gabriel Somoza <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class Progress |
30
|
|
|
{ |
31
|
|
|
/** @var int */ |
32
|
|
|
private $total; |
33
|
|
|
|
34
|
|
|
/** @var int */ |
35
|
|
|
private $current; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Progress constructor. |
39
|
|
|
* |
40
|
|
|
* @param int $total |
41
|
|
|
* @param int $current |
42
|
|
|
* |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
43 |
|
public function __construct($total, $current) |
46
|
|
|
{ |
47
|
43 |
|
if (!is_numeric($total) || (int) $total <= 0) { |
48
|
5 |
|
throw new InvalidArgumentException('Argument "total" must be an integer greater than zero.'); |
49
|
|
|
} |
50
|
38 |
|
$this->total = (int) $total; |
51
|
38 |
|
$this->update($current); |
52
|
34 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
20 |
|
public function getTotal() |
58
|
|
|
{ |
59
|
20 |
|
return $this->total; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
21 |
|
public function getCurrent() |
66
|
|
|
{ |
67
|
21 |
|
return $this->current; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Update the current progress |
72
|
|
|
* |
73
|
|
|
* @param $newProgress |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* |
77
|
|
|
* @throws InvalidArgumentException |
78
|
|
|
*/ |
79
|
38 |
|
public function update($newProgress) { |
80
|
38 |
|
if (!is_numeric($newProgress) || (int) $newProgress < 0 || (int) $newProgress > $this->total) { |
81
|
4 |
|
throw new InvalidArgumentException(sprintf( |
82
|
4 |
|
'Argument must be an integer between 0 (zero) and %s (total).', |
83
|
4 |
|
$this->total |
84
|
4 |
|
)); |
85
|
|
|
} |
86
|
34 |
|
$this->current = $newProgress; |
|
|
|
|
87
|
34 |
|
} |
88
|
|
|
} |
89
|
|
|
|
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.