Passed
Pull Request — master (#74)
by Evgeniy
02:06
created

NullCache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 16
eloc 25
c 2
b 1
f 0
dl 0
loc 82
ccs 38
cts 39
cp 0.9744
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A validateKey() 0 4 3
A validateKeysOfValues() 0 4 1
A clear() 0 3 1
A set() 0 4 1
A get() 0 4 1
A setMultiple() 0 5 1
A deleteMultiple() 0 5 1
A has() 0 4 1
A iterableToArray() 0 3 2
A delete() 0 4 1
A getMultiple() 0 5 1
A validateKeys() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache;
6
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
use Traversable;
9
use Yiisoft\Cache\Exception\InvalidArgumentException;
10
11
use function array_fill_keys;
12
use function array_keys;
13
use function array_map;
14
use function is_string;
15
use function iterator_to_array;
16
use function strpbrk;
17
18
/**
19
 * NullCache does not cache anything reporting success for all methods calls.
20
 *
21
 * By replacing it with some other cache component, one can quickly switch from non-caching mode to caching mode.
22
 *
23
 * See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that NullCache supports.
24
 */
25
final class NullCache implements CacheInterface
26
{
27 2
    public function get($key, $default = null)
28
    {
29 2
        $this->validateKey($key);
30 2
        return $default;
31
    }
32
33 2
    public function set($key, $value, $ttl = null): bool
34
    {
35 2
        $this->validateKey($key);
36 2
        return true;
37
    }
38
39 1
    public function delete($key): bool
40
    {
41 1
        $this->validateKey($key);
42 1
        return true;
43
    }
44
45 1
    public function clear(): bool
46
    {
47 1
        return true;
48
    }
49
50 1
    public function getMultiple($keys, $default = null): iterable
51
    {
52 1
        $keys = $this->iterableToArray($keys);
53 1
        $this->validateKeys($keys);
54 1
        return array_fill_keys($keys, $default);
55
    }
56
57 1
    public function setMultiple($values, $ttl = null): bool
58
    {
59 1
        $values = $this->iterableToArray($values);
60 1
        $this->validateKeysOfValues($values);
61 1
        return true;
62
    }
63
64 1
    public function deleteMultiple($keys): bool
65
    {
66 1
        $keys = $this->iterableToArray($keys);
67 1
        $this->validateKeys($keys);
68 1
        return true;
69
    }
70
71 1
    public function has($key): bool
72
    {
73 1
        $this->validateKey($key);
74 1
        return false;
75
    }
76
77
    /**
78
     * @param mixed $iterable
79
     * @return array
80
     */
81 3
    private function iterableToArray($iterable): array
82
    {
83 3
        return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
84
    }
85
86
    /**
87
     * @param mixed $key
88
     */
89 8
    private function validateKey($key): void
90
    {
91 8
        if (!is_string($key) || strpbrk($key, '{}()/\@:')) {
92
            throw new InvalidArgumentException('Invalid key value.');
93
        }
94 8
    }
95
96 3
    private function validateKeys(array $keys): void
97
    {
98 3
        foreach ($keys as $key) {
99 3
            $this->validateKey($key);
100
        }
101 3
    }
102
103 1
    private function validateKeysOfValues(array $values): void
104
    {
105 1
        $keys = array_map('strval', array_keys($values));
106 1
        $this->validateKeys($keys);
107 1
    }
108
}
109