Passed
Push — 1.x ( 52a2d2...558ce0 )
by Ulises Jeremias
02:27
created

Capacity::getDecayFactor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection\Traits;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
trait Capacity
12
{
13
    /**
14
     * @var integer internal capacity
15
     */
16
    private $capacity = self::MIN_CAPACITY;
17
18
    /**
19
     * Returns the current capacity.
20
     *
21
     * @return int
22
     */
23
    public function capacity(): int
24
    {
25
        return $this->capacity;
26
    }
27
28
    /**
29
     * Ensures that enough memory is allocated for a specified capacity. This
30
     * potentially reduces the number of reallocations as the size increases.
31
     *
32
     * @param int $capacity The number of values for which capacity should be
33
     *                      allocated. Capacity will stay the same if this value
34
     *                      is less than or equal to the current capacity.
35
     */
36
    public function allocate(int $capacity)
37
    {
38
        $this->capacity = max($capacity, $this->capacity);
39
    }
40
41
    /**
42
     * @return the structures growth factor.
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\Traits\the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    protected function getGrowthFactor(): float
45
    {
46
        return 2;
0 ignored issues
show
Bug Best Practice introduced by
The expression return 2 returns the type integer which is incompatible with the documented return type Mbh\Collection\Traits\the.
Loading history...
47
    }
48
49
    /**
50
     * @return float to multiply by when decreasing capacity.
51
     */
52
    protected function getDecayFactor(): float
53
    {
54
        return 0.5;
55
    }
56
57
    /**
58
     * @return float the ratio between size and capacity when capacity should be
59
     *               decreased.
60
     */
61
    protected function getTruncateThreshold(): float
62
    {
63
        return 0.25;
64
    }
65
66
    /**
67
     * Checks and adjusts capacity if required.
68
     */
69
    protected function checkCapacity()
70
    {
71
        if ($this->shouldIncreaseCapacity()) {
72
            $this->increaseCapacity();
73
        } else {
74
            if ($this->shouldDecreaseCapacity()) {
75
                $this->decreaseCapacity();
76
            }
77
        }
78
    }
79
80
    /**
81
     * Called when capacity should be increased to accommodate new values.
82
     */
83
    protected function increaseCapacity()
84
    {
85
        $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? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $this->capacity = max($this->/** @scrutinizer ignore-call */ count(), $this->capacity * $this->getGrowthFactor());
Loading history...
86
    }
87
88
    /**
89
     * Called when capacity should be decrease if it drops below a threshold.
90
     */
91
    protected function decreaseCapacity()
92
    {
93
        $this->capacity = max(self::MIN_CAPACITY, $this->capacity  * $this->getDecayFactor());
0 ignored issues
show
Bug introduced by
The constant Mbh\Collection\Traits\Capacity::MIN_CAPACITY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
    }
95
96
    /**
97
     * @return whether capacity should be increased.
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\Traits\whether was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
     */
99
    protected function shouldDecreaseCapacity(): bool
100
    {
101
        return count($this) <= $this->capacity * $this->getTruncateThreshold();
0 ignored issues
show
Bug Best Practice introduced by
The expression return count($this) <= $...>getTruncateThreshold() returns the type boolean which is incompatible with the documented return type Mbh\Collection\Traits\whether.
Loading history...
Bug introduced by
$this of type Mbh\Collection\Traits\Capacity is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        return count(/** @scrutinizer ignore-type */ $this) <= $this->capacity * $this->getTruncateThreshold();
Loading history...
102
    }
103
104
    /**
105
     * @return whether capacity should be increased.
106
     */
107
    protected function shouldIncreaseCapacity(): bool
108
    {
109
        return count($this) >= $this->capacity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return count($this) >= $this->capacity returns the type boolean which is incompatible with the documented return type Mbh\Collection\Traits\whether.
Loading history...
Bug introduced by
$this of type Mbh\Collection\Traits\Capacity is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
        return count(/** @scrutinizer ignore-type */ $this) >= $this->capacity;
Loading history...
110
    }
111
}
112