Passed
Pull Request — master (#27)
by Alexander
01:44
created

ApcuCache::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Yiisoft\Cache;
3
4
/**
5
 * ApcuCache provides APCu caching in terms of an application component.
6
 *
7
 * To use this application component, the [APCu PHP extension](http://www.php.net/apcu) must be loaded.
8
 * In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
9
 *
10
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports.
11
 */
12
final class ApcuCache extends SimpleCache
13
{
14
    private const TTL_INFINITY = 0;
15
16 3
    protected function hasValue(string $key): bool
17
    {
18 3
        return \apcu_exists($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_exists($key) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
19
    }
20
21 15
    protected function getValue(string $key, $default = null)
22
    {
23 15
        $value = \apcu_fetch($key, $success);
24 15
        return $success ? $value : $default;
25
    }
26
27 3
    protected function getValues(iterable $keys, $default = null): iterable
28
    {
29 3
        return \apcu_fetch($keys, $success) ?: [];
0 ignored issues
show
Bug introduced by
$keys of type iterable is incompatible with the type string|string[] expected by parameter $key of apcu_fetch(). ( Ignorable by Annotation )

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

29
        return \apcu_fetch(/** @scrutinizer ignore-type */ $keys, $success) ?: [];
Loading history...
30
    }
31
32 13
    protected function setValue(string $key, $value, ?int $ttl): bool
33
    {
34 13
        return \apcu_store($key, $value, $ttl ?? self::TTL_INFINITY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_store($key, ... ?? self::TTL_INFINITY) could return the type array which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
35
    }
36
37 5
    protected function setValues(iterable $values, ?int $ttl): bool
38
    {
39 5
        return \apcu_store($values, null, $ttl ?? self::TTL_INFINITY) === [];
0 ignored issues
show
Bug introduced by
$values of type iterable is incompatible with the type array|string expected by parameter $key of apcu_store(). ( Ignorable by Annotation )

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

39
        return \apcu_store(/** @scrutinizer ignore-type */ $values, null, $ttl ?? self::TTL_INFINITY) === [];
Loading history...
40
    }
41
42 1
    protected function deleteValue(string $key): bool
43
    {
44 1
        return \apcu_delete($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_delete($key) could return the type string[] which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
45
    }
46
47 17
    public function clear(): bool
48
    {
49 17
        return \apcu_clear_cache();
50
    }
51
52 1
    public function deleteValues(iterable $keys): bool
53
    {
54 1
        return \apcu_delete($keys) === [];
55
    }
56
}
57