Passed
Push — main ( 3f5d4e...df4575 )
by James Ekow Abaka
01:53
created

EncapsulatedStack::purge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
namespace yentu\database;
3
4
class EncapsulatedStack
5
{
6
    private array $stack = [];
7
    
8
    public function push(DatabaseItem $encapsulated): void
9
    {
10
        $this->stack[] = $encapsulated;
11
    }
12
    
13
    public function pop(): DatabaseItem
14
    {
15
        return array_pop($this->stack);
16
    }
17
    
18
    public function top(): DatabaseItem
19
    {
20
        return end($this->stack);
21
    }
22
    
23
    public function hasItems(): bool
24
    {
25
        return !empty($this->stack);
26
    }
27
    
28
    public function purge(): void
29
    {
30
        for ($i = 0; $i < count($this->stack); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
31
            $item = array_pop($this->stack);
32
            $item->commit();
33
        }
34
    }    
35
}
36
37