GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch v2 (a88462)
by Hilari
02:38
created

RedisCache   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 128
rs 10
ccs 29
cts 34
cp 0.8529

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 8 2
A setItems() 0 8 2
A has() 0 4 1
A demand() 0 10 2
A get() 0 8 2
A getItems() 0 6 2
A delete() 0 4 1
A deleteAll() 0 4 1
A flush() 0 4 1
A getTimeToLive() 0 6 3
1
<?php
2
3
namespace Cmp\Cache\Backend;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\NotFoundException;
7
use Cmp\Cache\TimeToLiveAwareCache;
8
use Cmp\Cache\Traits\MultiCacheTrait;
9
use Redis;
10
11
/**
12
 * Class RedisCache
13
 * 
14
 * A redis powered backend for caching
15
 *
16
 * @package Cmp\Cache\Infrastureture\Backend
17
 */
18
class RedisCache implements Cache
19
{
20
    use MultiCacheTrait {
21
        MultiCacheTrait::setItems as setItemsTrait;
22
    }
23
24
    const DEFAULT_HOST    = '127.0.0.1';
25
    const DEFAULT_PORT    = 6379;
26
    const DEFAULT_DB      = 0;
27
    const DEFAULT_TIMEOUT = 0.0;
28
29
    /**
30
     * @var Redis
31
     */
32
    private $client;
33
34
    /**
35
     * RedisCache constructor.
36
     *
37
     * @param Redis $client
38
     */
39 1
    public function __construct(Redis $client)
40
    {
41 1
        $this->client = $client;
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function set($key, $value, $timeToLive = null)
48
    {
49 1
        if ($timeToLive > 0) {
50 1
            return $this->client->setex($key, $timeToLive, $value);
51
        }
52
53 1
        return $this->client->set($key, $value);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function setItems(array $items, $timeToLive = null)
60
    {
61 1
        if (!$timeToLive) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $timeToLive of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
62 1
            return $this->client->mset($items);
63
        }
64
65 1
        return $this->setItemsTrait($items, $timeToLive);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function has($key)
72
    {
73 1
        return $this->client->exists($key);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function demand($key)
80
    {
81 1
        $value = $this->client->get($key);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $value is correct as $this->client->get($key) (which targets Redis::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
83 1
        if (!$value) {
84 1
            throw new NotFoundException($key);
85
        }
86
87 1
        return $value;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function get($key, $default = null)
94
    {
95
        try {
96 1
            return $this->demand($key);
97 1
        } catch (NotFoundException $exception) {
98 1
            return $default;
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getItems(array $keys)
106
    {
107
        return array_walk($this->client->mget($keys), function(&$value) {
0 ignored issues
show
Bug introduced by
$this->client->mget($keys) cannot be passed to array_walk() as the parameter $array expects a reference.
Loading history...
108
            $value = $value === false ? null : $value;
109
        });
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function delete($key)
116
    {
117 1
        return (bool) $this->client->delete($key);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function deleteAll(array $keys)
124
    {
125
        return (bool) call_user_func_array([$this->client, 'delete', $keys]);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 1
    public function flush()
132
    {
133 1
        return $this->client->flushDB();
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function getTimeToLive($key)
140
    {
141 1
        $timeToLive = $this->client->ttl($key);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $timeToLive is correct as $this->client->ttl($key) (which targets Redis::ttl()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
142
143 1
        return false === $timeToLive || $timeToLive <= 0 ? null : $timeToLive; 
144
    }
145
}
146