1 | <?php |
||
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 |
||
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) |
||
40 | |||
41 | /** |
||
42 | * @return float the structures growth factor. |
||
43 | */ |
||
44 | protected function getGrowthFactor(): float |
||
48 | |||
49 | /** |
||
50 | * @return float to multiply by when decreasing capacity. |
||
51 | */ |
||
52 | protected function getDecayFactor(): float |
||
56 | |||
57 | /** |
||
58 | * @return float the ratio between size and capacity when capacity should be |
||
59 | * decreased. |
||
60 | */ |
||
61 | protected function getTruncateThreshold(): float |
||
65 | |||
66 | /** |
||
67 | * Checks and adjusts capacity if required. |
||
68 | */ |
||
69 | protected function checkCapacity() |
||
79 | |||
80 | /** |
||
81 | * @param int $total |
||
82 | */ |
||
83 | protected function ensureCapacity(int $total) |
||
89 | |||
90 | /** |
||
91 | * @return bool whether capacity should be increased. |
||
92 | */ |
||
93 | protected function shouldIncreaseCapacity(): bool |
||
97 | |||
98 | protected function nextCapacity(): int |
||
102 | |||
103 | /** |
||
104 | * Called when capacity should be increased to accommodate new values. |
||
105 | */ |
||
106 | protected function increaseCapacity() |
||
113 | |||
114 | /** |
||
115 | * Called when capacity should be decrease if it drops below a threshold. |
||
116 | */ |
||
117 | protected function decreaseCapacity() |
||
124 | |||
125 | /** |
||
126 | * @return bool whether capacity should be increased. |
||
127 | */ |
||
128 | protected function shouldDecreaseCapacity(): bool |
||
132 | } |
||
133 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.