Passed
Push — master ( 5a87d9...5af04f )
by Mihail
03:04
created

MultiplesTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 24
c 2
b 0
f 1
dl 0
loc 83
ccs 29
cts 29
cp 1
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteMultiple() 0 9 2
A internalMultiGet() 0 8 2
A internalMultiSet() 0 7 3
A internalMultiDelete() 0 8 3
A getMultiple() 0 5 1
A setMultiple() 0 11 3
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Caching\Client;
14
15
use function Koded\Caching\{filter_keys, normalize_ttl};
0 ignored issues
show
Bug introduced by
The type Koded\Caching\normalize_ttl 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...
Bug introduced by
The type Koded\Caching\filter_keys 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...
16
17
/**
18
 * Trait MultiplesTrait implements all "multi" operations
19
 * separated for easy override in the specific cache classes.
20
 *
21
 */
22
trait MultiplesTrait
23
{
24 239
    public function getMultiple($keys, $default = null): iterable
25
    {
26 239
        $filtered = filter_keys($keys, false);
27
28 95
        return $this->internalMultiGet($filtered, $default);
29
    }
30
31 351
    public function setMultiple($values, $ttl = null): bool
32
    {
33 351
        $filtered = filter_keys($values, true);
34 207
        $ttl = normalize_ttl($ttl ?? $this->ttl);
35
36 127
        if ($ttl !== null && $ttl < 1) {
37
            // All items are considered expired and must be deleted
38 8
            return $this->deleteMultiple(array_keys($filtered));
39
        }
40
41 119
        return $this->internalMultiSet($filtered, $ttl);
42
    }
43
44 175
    public function deleteMultiple($keys): bool
45
    {
46 175
        $filtered = filter_keys($keys, false);
47
48 31
        if (empty($filtered)) {
49 8
            return true;
50
        }
51
52 31
        return $this->internalMultiDelete($filtered);
53
    }
54
55
    /*
56
     *
57
     * Override in specific cache classes.
58
     *
59
     */
60
61
    /**
62
     * @param array $keys
63
     * @param mixed $default
64
     *
65
     * @return iterable
66
     */
67 83
    private function internalMultiGet(array $keys, $default): iterable
68
    {
69 83
        $cached = [];
70 83
        foreach ($keys as $key) {
71 83
            $cached[$key] = $this->get($key, $default);
0 ignored issues
show
Bug introduced by
It seems like get() 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

71
            /** @scrutinizer ignore-call */ 
72
            $cached[$key] = $this->get($key, $default);
Loading history...
72
        }
73
74 83
        return $cached;
75
    }
76
77
    /**
78
     * @param array    $values
79
     * @param int|null $ttl
80
     *
81
     * @return bool
82
     */
83 104
    private function internalMultiSet(array $values, $ttl): bool
84
    {
85 104
        $cached = 0;
86 104
        foreach ($values as $key => $value) {
87 104
            $this->set($key, $value, $ttl) && ++$cached;
0 ignored issues
show
Bug introduced by
It seems like set() 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

87
            $this->/** @scrutinizer ignore-call */ 
88
                   set($key, $value, $ttl) && ++$cached;
Loading history...
88
        }
89 104
        return count($values) === $cached;
90
    }
91
92
    /**
93
     * @param array $keys
94
     *
95
     * @return bool
96
     */
97 27
    private function internalMultiDelete(array $keys): bool
98
    {
99 27
        $deleted = 0;
100 27
        foreach ($keys as $key) {
101 27
            $this->delete($key) && ++$deleted;
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Koded\Caching\Client\MultiplesTrait. Did you maybe mean deleteMultiple()? ( Ignorable by Annotation )

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

101
            $this->/** @scrutinizer ignore-call */ 
102
                   delete($key) && ++$deleted;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        }
103
104 27
        return count($keys) === $deleted;
105
    }
106
}
107