Passed
Branch master (0b4094)
by Alexander
01:32
created

ApcuCache::has()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Yiisoft\Cache;
4
5
use DateInterval;
6
use DateTime;
7
use Psr\SimpleCache\CacheInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Cache\CacheInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
/**
10
 * ApcuCache provides APCu caching in terms of an application component.
11
 *
12
 * To use this application component, the [APCu PHP extension](http://www.php.net/apcu) must be loaded.
13
 * In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
14
 *
15
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports.
16
 */
17
class ApcuCache implements CacheInterface
18
{
19
    private const TTL_INFINITY = 0;
20
    private const TTL_EXPIRED = -1;
21
22 121
    public function get($key, $default = null)
23
    {
24 121
        $value = \apcu_fetch($key, $success);
25 121
        return $success ? $value : $default;
26
    }
27
28 137
    public function set($key, $value, $ttl = null): bool
29
    {
30 137
        $ttl = $this->normalizeTtl($ttl);
31 137
        if ($ttl < 0) {
32 2
            return $this->delete($key);
33
        }
34 135
        return \apcu_store($key, $value, $ttl);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_store($key, $value, $ttl) 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 22
    public function delete($key): bool
38
    {
39 22
        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...
40
    }
41
42 156
    public function clear(): bool
43
    {
44 156
        return \apcu_clear_cache();
45
    }
46
47 20
    public function getMultiple($keys, $default = null): iterable
48
    {
49 20
        $values = \apcu_fetch($this->iterableToArray($keys), $success) ?: [];
50 20
        return array_merge(array_fill_keys($this->iterableToArray($keys), $default), $values);
51
    }
52
53 26
    public function setMultiple($values, $ttl = null): bool
54
    {
55 26
        return \apcu_store($this->iterableToArray($values), null, $this->normalizeTtl($ttl)) === [];
56
    }
57
58 2
    public function deleteMultiple($keys): bool
59
    {
60 2
        return \apcu_delete($this->iterableToArray($keys)) === [];
61
    }
62
63 29
    public function has($key): bool
64
    {
65 29
        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...
66
    }
67
68
    /**
69
     * @noinspection PhpDocMissingThrowsInspection DateTime won't throw exception because constant string is passed as time
70
     *
71
     * Normalizes cache TTL handling `null` value and {@see DateInterval} objects.
72
     * @param int|DateInterval|null $ttl raw TTL.
73
     * @return int|null TTL value as UNIX timestamp or null meaning infinity
74
     */
75 165
    private function normalizeTtl($ttl): ?int
76
    {
77 165
        $normalizedTtl = $ttl;
78 165
        if ($ttl instanceof DateInterval) {
79 5
            $normalizedTtl = (new DateTime('@0'))->add($ttl)->getTimestamp();
80
        }
81
82 165
        return $normalizedTtl ?? static::TTL_INFINITY;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $normalizedTtl ?? static::TTL_INFINITY could return the type DateInterval which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
    /**
86
     * Converts iterable to array
87
     * @param iterable $iterable
88
     * @return array
89
     */
90 26
    private function iterableToArray(iterable $iterable): array
91
    {
92 26
        return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable;
93
    }
94
}
95