Progress::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
crap 3
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $newProgress can also be of type double or string. However, the property $current 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...
87 34
    }
88
}
89