Completed
Push — master ( 1f3ae1...7a77f2 )
by Nikola
02:24
created

MaxSizeRotator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.24%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 57
ccs 20
cts 21
cp 0.9524
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B nominate() 0 38 5
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Rotator;
14
15
use RunOpenCode\Backup\Contract\BackupInterface;
16
use RunOpenCode\Backup\Contract\RotatorInterface;
17
use RunOpenCode\Backup\Utils\Filesize;
18
19
/**
20
 * Class MaxSizeRotator
21
 *
22
 * Rotator that nominates old backups when total backup size exceed maximum allowed backup size.
23
 *
24
 * @package RunOpenCode\Backup\Rotator
25
 */
26
final class MaxSizeRotator implements RotatorInterface
27
{
28
    /**
29
     * @var int
30
     */
31
    private $maxSize;
32
33
    /**
34
     * @param string|int $maxSize
35
     */
36 4
    public function __construct($maxSize)
37
    {
38 4
        $this->maxSize = Filesize::getBytes($maxSize);
0 ignored issues
show
Documentation Bug introduced by
It seems like \RunOpenCode\Backup\Util...ize::getBytes($maxSize) can also be of type double. However, the property $maxSize 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...
39 4
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 4
    public function nominate(array $backups)
45
    {
46 4
        $list = array();
47 4
        $currentSize = 0;
48
49
        /**
50
         * @var BackupInterface $backup
51
         */
52 4
        foreach ($backups as $backup) {
53 4
            $list[$backup->getCreatedAt()->getTimestamp()] = $backup;
54 4
            $currentSize += $backup->getSize();
55 4
        }
56
57 4
        if ($currentSize > $this->maxSize) {
58
59 4
            ksort($list);
60
61 4
            $nominations = array();
62
63
            /**
64
             * @var BackupInterface $backup
65
             */
66 4
            foreach ($list as $backup) {
67
68 4
                $nominations[] = $backup;
69 4
                $currentSize -= $backup->getSize();
70
71 4
                if ($currentSize <= $this->maxSize) {
72 4
                    break;
73
                }
74 4
            }
75
76 4
            return $nominations;
77
78
        } else {
79
            return array();
80
        }
81
    }
82
}
83