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

WinCache::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
eloc 1
nc 1
nop 1
ccs 0
cts 3
cp 0
crap 2
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
 * WinCache provides Windows Cache caching in terms of an application component.
11
 *
12
 * To use this application component, the [WinCache PHP extension](https://sourceforge.net/projects/wincache/)
13
 * must be loaded. Also note that "wincache.ucenabled" should be set to "1" in your php.ini file.
14
 *
15
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that are supported by WinCache.
16
 */
17
class WinCache implements CacheInterface
18
{
19
    private const TTL_INFINITY = 0;
20
    private const TTL_EXPIRED = -1;
21
22
    public function get($key, $default = null)
23
    {
24
        $value = \wincache_ucache_get($key, $success);
25
        return $success ? $value : $default;
26
    }
27
28
    public function set($key, $value, $ttl = null): bool
29
    {
30
        $ttl = $this->normalizeTtl($ttl);
31
        if ($ttl < 0) {
32
            return $this->delete($key);
33
        }
34
        return \wincache_ucache_set($key, $value, $ttl);
35
    }
36
37
    public function delete($key): bool
38
    {
39
        return \wincache_ucache_delete($key);
40
    }
41
42
    public function clear(): bool
43
    {
44
        return \wincache_ucache_clear();
45
    }
46
47
    public function getMultiple($keys, $default = null): iterable
48
    {
49
        $defaultValues = array_fill_keys($this->iterableToArray($keys), $default);
50
        return array_merge($defaultValues, \wincache_ucache_get($this->iterableToArray($keys)));
51
    }
52
53
    public function setMultiple($values, $ttl = null): bool
54
    {
55
        return \wincache_ucache_set($this->iterableToArray($values), null, $this->normalizeTtl($ttl)) === [];
56
    }
57
58
    public function deleteMultiple($keys): bool
59
    {
60
        $deleted = array_flip(\wincache_ucache_delete($keys));
0 ignored issues
show
Bug introduced by
wincache_ucache_delete($keys) of type boolean is incompatible with the type array expected by parameter $array of array_flip(). ( Ignorable by Annotation )

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

60
        $deleted = array_flip(/** @scrutinizer ignore-type */ \wincache_ucache_delete($keys));
Loading history...
61
        foreach ($keys as $expectedKey) {
62
            if (!isset($deleted[$expectedKey])) {
63
                return false;
64
            }
65
        }
66
        return true;
67
    }
68
69
    public function has($key): bool
70
    {
71
        return \wincache_ucache_exists($key);
72
    }
73
74
    /**
75
     * @noinspection PhpDocMissingThrowsInspection DateTime won't throw exception because constant string is passed as time
76
     *
77
     * Normalizes cache TTL handling `null` value and {@see DateInterval} objects.
78
     * @param int|DateInterval|null $ttl raw TTL.
79
     * @return int|null TTL value as UNIX timestamp or null meaning infinity
80
     */
81
    private function normalizeTtl($ttl): ?int
82
    {
83
        $normalizedTtl = $ttl;
84
        if ($ttl instanceof DateInterval) {
85
            $normalizedTtl = (new DateTime('@0'))->add($ttl)->getTimestamp();
86
        }
87
88
        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...
89
    }
90
91
    /**
92
     * Converts iterable to array
93
     * @param iterable $iterable
94
     * @return array
95
     */
96
    private function iterableToArray(iterable $iterable): array
97
    {
98
        return $iterable instanceof \Traversable ? iterator_to_array($iterable) : (array)$iterable;
99
    }
100
}
101