Completed
Pull Request — master (#43)
by
unknown
08:27
created

Capacity::allocate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Ds\Traits;
3
4
/**
5
 * Common to structures that deal with an internal capacity. While none of the
6
 * PHP implementations actually make use of a capacity, it's important to keep
7
 * consistent with the extension.
8
 */
9
trait Capacity
10
{
11
    /**
12
     * @var integer internal capacity
13
     */
14
    private $capacity = self::MIN_CAPACITY;
15
16
    /**
17
     * Returns the current capacity.
18
     *
19
     * @return int
20
     */
21
    public function capacity(): int
22
    {
23
        return $this->capacity;
24
    }
25
26
    /**
27
     * Ensures that enough memory is allocated for a specified capacity. This
28
     * potentially reduces the number of reallocations as the size increases.
29
     *
30
     * @param int $capacity The number of values for which capacity should be
31
     *                      allocated. Capacity will stay the same if this value
32
     *                      is less than or equal to the current capacity.
33
     */
34
    public function allocate(int $capacity)
35
    {
36
        $this->capacity = max($capacity, $this->capacity);
37
    }
38
39
    /**
40
     * @return the structures growth factor.
0 ignored issues
show
Documentation introduced by
Should the return type not be double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
41
     */
42
    protected function getGrowthFactor(): float
43
    {
44
        return 2;
45
    }
46
47
    /**
48
     * @return float to multiply by when decreasing capacity.
49
     */
50
    protected function getDecayFactor(): float
51
    {
52
        return 0.5;
53
    }
54
55
    /**
56
     * @return float the ratio between size and capacity when capacity should be
57
     *               decreased.
58
     */
59
    protected function getTruncateThreshold(): float
60
    {
61
        return 0.25;
62
    }
63
64
    /**
65
     * Checks and adjusts capacity if required.
66
     */
67
    protected function checkCapacity()
68
    {
69
        if ($this->shouldIncreaseCapacity()) {
70
            $this->increaseCapacity();
71
        } else {
72
            if ($this->shouldDecreaseCapacity()) {
73
                $this->decreaseCapacity();
74
            }
75
        }
76
    }
77
78
    /**
79
     * Called when capacity should be increased to accommodate new values.
80
     */
81
    protected function increaseCapacity()
82
    {
83
        $this->capacity = max($this->count(), $this->capacity * $this->getGrowthFactor());
0 ignored issues
show
Bug introduced by
It seems like count() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 90 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
84
    }
85
86
    /**
87
     * Called when capacity should be decrease if it drops below a threshold.
88
     */
89
    protected function decreaseCapacity()
90
    {
91
        $this->capacity = max(self::MIN_CAPACITY, $this->capacity  * $this->getDecayFactor());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 94 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
92
    }
93
94
    /**
95
     * @return whether capacity should be increased.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97
    protected function shouldDecreaseCapacity(): bool
98
    {
99
        return count($this) <= $this->capacity * $this->getTruncateThreshold();
100
    }
101
102
    /**
103
     * @return whether capacity should be increased.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
104
     */
105
    protected function shouldIncreaseCapacity(): bool
106
    {
107
        return count($this) >= $this->capacity;
108
    }
109
}
110