PsrCacheIterator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 67
ccs 23
cts 23
cp 1
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 6 1
A key() 0 7 1
A getTupleFromCache() 0 14 2
A rewind() 0 5 1
A current() 0 7 1
A valid() 0 3 2
A __construct() 0 1 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Iterator;
6
7
use Iterator;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Cache\CacheItemPoolInterface;
10
use ReturnTypeWillChange;
11
12
/**
13
 * @internal
14
 *
15
 * @template TKey
16
 * @template T
17
 *
18
 * @implements Iterator<TKey, T>
19
 */
20
final class PsrCacheIterator implements Iterator
21
{
22
    private int $key = 0;
23
24
    /**
25
     * @param Iterator<TKey, T> $iterator
26
     */
27 4
    public function __construct(private Iterator $iterator, private CacheItemPoolInterface $cache) {}
28
29
    /**
30
     * @return T
0 ignored issues
show
Bug introduced by
The type loophp\collection\Iterator\T 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...
31
     */
32 4
    #[ReturnTypeWillChange]
33
    public function current()
34
    {
35
        /** @var array{TKey, T} $data */
36 4
        $data = $this->getTupleFromCache($this->key)->get();
37
38 4
        return $data[1];
39
    }
40
41
    /**
42
     * @return TKey
0 ignored issues
show
Bug introduced by
The type loophp\collection\Iterator\TKey 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 3
    #[ReturnTypeWillChange]
45
    public function key()
46
    {
47
        /** @var array{TKey, T} $data */
48 3
        $data = $this->getTupleFromCache($this->key)->get();
49
50 3
        return $data[0];
51
    }
52
53 4
    public function next(): void
54
    {
55
        // This is mostly for iterator_count().
56 4
        $this->getTupleFromCache($this->key++);
57
58 4
        $this->iterator->next();
59
    }
60
61 4
    public function rewind(): void
62
    {
63
        // No call to $this->iterator->rewind() because we do not know if the inner
64
        // iterator can be rewinded or not.
65 4
        $this->key = 0;
66
    }
67
68 3
    public function valid(): bool
69
    {
70 3
        return $this->iterator->valid() || $this->cache->hasItem((string) $this->key);
71
    }
72
73 4
    private function getTupleFromCache(int $key): CacheItemInterface
74
    {
75 4
        $item = $this->cache->getItem((string) $key);
76
77 4
        if (false === $item->isHit()) {
78 4
            $item->set([
79 4
                $this->iterator->key(),
80 4
                $this->iterator->current(),
81 4
            ]);
82
83 4
            $this->cache->save($item);
84
        }
85
86 4
        return $item;
87
    }
88
}
89