Test Failed
Pull Request — master (#16)
by Alexander
11:49 queued 09:08
created

SimpleCacheActionLogger::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Test\Support\SimpleCache;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Traversable;
9
use Yiisoft\Test\Support\SimpleCache\Exception\InvalidArgumentException;
10
11
/**
12
 * @template TCacheService as CacheInterface
13
 */
14
final class SimpleCacheActionLogger implements CacheInterface
15
{
16
    /** @var Action[] */
17
    private array $actions;
18
    /** @var TCacheService */
0 ignored issues
show
Bug introduced by
The type Yiisoft\Test\Support\SimpleCache\TCacheService 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...
19
    private CacheInterface $cacheService;
20
21
    /**
22
     * SimpleCacheActionLogger constructor.
23
     *
24
     * @param array $cacheData
25
     * @param TCacheService $cacheService
26
     */
27
    public function __construct(CacheInterface $cacheService, array $cacheData = [])
28
    {
29
        $this->cacheService = $cacheService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheService of type Psr\SimpleCache\CacheInterface is incompatible with the declared type Yiisoft\Test\Support\SimpleCache\TCacheService of property $cacheService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
        $this->cacheService->setMultiple($cacheData);
31
        $this->actions = [];
32
    }
33
34
    public function get($key, $default = null)
35
    {
36
        $this->actions[] = Action::createGetAction($key);
37
        return $this->cacheService->get($key, $default);
38
    }
39
40
    public function delete($key): bool
41
    {
42
        $this->actions[] = Action::createDeleteAction($key);
43
        return $this->cacheService->delete($key);
44
    }
45
46
    public function has($key): bool
47
    {
48
        $this->actions[] = Action::createHasAction($key);
49
        return $this->cacheService->has($key);
50
    }
51
52
    public function clear(): bool
53
    {
54
        $this->actions[] = Action::createClearAction();
55
        return $this->cacheService->clear();
56
    }
57
58
    public function set($key, $value, $ttl = null): bool
59
    {
60
        $this->actions[] = Action::createSetAction($key, $value, $ttl);
61
        return $this->cacheService->set($key, $value, $ttl);
62
    }
63
64
    public function getMultiple($keys, $default = null): iterable
65
    {
66
        $keys = $this->iterableToArray($keys);
67
        /** @psalm-var mixed $key */
68
        foreach ($keys as $key) {
69
            $this->actions[] = Action::createGetAction($key);
70
        }
71
        return $this->cacheService->getMultiple($keys, $default);
72
    }
73
74
    public function setMultiple($values, $ttl = null): bool
75
    {
76
        $values = $this->iterableToArray($values);
77
        /** @psalm-var mixed $value */
78
        foreach ($values as $key => $value) {
79
            $this->actions[] = Action::createSetAction($key, $value, $ttl);
80
        }
81
        return $this->cacheService->setMultiple($values, $ttl);
82
    }
83
84
    public function deleteMultiple($keys): bool
85
    {
86
        $keys = $this->iterableToArray($keys);
87
        /** @psalm-var mixed $key */
88
        foreach ($keys as $key) {
89
            $this->actions[] = Action::createDeleteAction($key);
90
        }
91
        return $this->cacheService->deleteMultiple($keys);
92
    }
93
94
    /**
95
     * @return Action[]
96
     */
97
    public function getActions(): array
98
    {
99
        return $this->actions;
100
    }
101
102
    /**
103
     * @return array<int, array{0: string, 1: mixed}>
104
     */
105
    public function getActionKeyList(): array
106
    {
107
        $result = [];
108
        foreach ($this->actions as $action) {
109
            $result[] = [$action->getAction(), $action->getKey()];
110
        }
111
        return $result;
112
    }
113
114
    /**
115
     * @return TCacheService
116
     */
117
    public function getCacheService(): CacheInterface
118
    {
119
        return $this->cacheService;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cacheService returns the type Psr\SimpleCache\CacheInterface which is incompatible with the documented return type Yiisoft\Test\Support\SimpleCache\TCacheService.
Loading history...
120
    }
121
122
    /**
123
     * @param mixed $iterable
124
     *
125
     * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException
126
     */
127
    private function iterableToArray($iterable): array
128
    {
129
        if (!is_iterable($iterable)) {
130
            throw new InvalidArgumentException(sprintf('Iterable is expected, got %s.', gettype($iterable)));
131
        }
132
        return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
133
    }
134
}
135