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
Push — master ( 6a8fe9...bdbe09 )
by やかみ
02:52
created

Cache::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Kotori.php
4
 *
5
 * A Tiny Model-View-Controller PHP Framework
6
 *
7
 * This content is released under the Apache 2 License
8
 *
9
 * Copyright (c) 2015-2017 Kotori Technology. All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
/**
25
 * Caching Class
26
 *
27
 * @package     Kotori
28
 * @subpackage  Core
29
 * @author      Kokororin
30
 * @link        https://kotori.love
31
 */
32
namespace Kotori\Core;
33
34
use Kotori\Core\Container;
35
use Kotori\Debug\Hook;
36
use Psr\SimpleCache\CacheInterface;
37
38
class Cache implements CacheInterface
39
{
40
    /**
41
     * Valid cache drivers
42
     *
43
     * @var array
44
     */
45
    protected $validDrivers = [
46
        'dummy',
47
        'memcached',
48
        'redis',
49
    ];
50
51
    /**
52
     * Reference to the driver
53
     *
54
     * @var mixed
55
     */
56
    protected $adapter = 'dummy';
57
58
    /**
59
     * Cache key prefix
60
     *
61
     * @var string
62
     */
63
    public $keyPrefix = '';
64
65
    /**
66
     * Constructor
67
     *
68
     * Initialize class properties based on the configuration array.
69
     *
70
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
71
     */
72 1
    public function __construct()
73
    {
74 1
        $config = Container::get('config')->get('cache');
75 1
        if (isset($config['adapter'])) {
76 1
            $this->adapter = $config['adapter'];
77
        }
78
79 1
        if (isset($config['prefix'])) {
80 1
            $this->keyPrefix = $config['prefix'];
81
        }
82
83 1
        $className = '\\Kotori\\Core\\Cache\\' . ucfirst($this->adapter);
84 1
        $this->{$this->adapter} = new $className();
85
86 1
        if (!$this->isSupported($this->adapter)) {
87
            Container::get('logger')->error('Cache adapter "{adapter}" is unavailable. Cache is now using "Dummy" adapter.', ['adapter' => $this->adapter]);
88
            $this->adapter = 'dummy';
89
        }
90
91 1
        Hook::listen(__CLASS__);
92 1
    }
93
94
    /**
95
     * Fetches a value from the cache.
96
     *
97
     * @param  string $key
98
     * @param  mixed  $default
99
     * @return mixed
100
     */
101 6
    public function get($key, $default = null)
102
    {
103 6
        $value = $this->{$this->adapter}->get($this->keyPrefix . $key);
104 6
        return $value ? $value : $default;
105
    }
106
107
    /**
108
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
109
     *
110
     * @param  string   $key
111
     * @param  mixed    $value
112
     * @param  int      $ttl
113
     * @return boolean
114
     */
115 6
    public function set($key, $value, $ttl = null)
116
    {
117 6
        return $this->{$this->adapter}->set($this->keyPrefix . $key, $value, $ttl);
118
    }
119
120
    /**
121
     * Delete an item from the cache by its unique key.
122
     *
123
     * @param  string  $key
124
     * @return boolean
125
     */
126 3
    public function delete($key)
127
    {
128 3
        return $this->{$this->adapter}->delete($this->keyPrefix . $key);
129
    }
130
131
    /**
132
     * Wipes clean the entire cache's keys.
133
     *
134
     * @return boolean
135
     */
136 1
    public function clear()
137
    {
138 1
        return $this->{$this->adapter}->clear();
139
    }
140
141
    /**
142
     * Obtains multiple cache items by their unique keys.
143
     *
144
     * @param  iterable $keys
145
     * @param  mixed    $default
146
     * @return iterable
147
     */
148 2
    public function getMultiple($keys, $default = null)
149
    {
150 2
        $values = [];
151 2
        foreach ($keys as $key) {
152 2
            $values[$key] = $this->get($key, $default);
153
        }
154
155 2
        return $values;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $values; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::getMultiple of type Psr\SimpleCache\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
156
    }
157
158
    /**
159
     * Persists a set of key => value pairs in the cache, with an optional TTL.
160
     *
161
     * @param  iterable $values
162
     * @param  int      $ttl
163
     * @return boolean
164
     */
165 2
    public function setMultiple($values, $ttl = null)
166
    {
167 2
        $failTimes = 0;
168 2
        foreach ($values as $key => $value) {
169 2
            if (!$this->set($key, $value, $ttl)) {
170 2
                $failTimes++;
171
            }
172
        }
173
174 2
        return $failTimes == 0;
175
    }
176
177
    /**
178
     * Deletes multiple cache items in a single operation.
179
     *
180
     * @param  iterable $keys
181
     * @return boolean
182
     */
183 1
    public function deleteMultiple($keys)
184
    {
185 1
        $failTimes = 0;
186 1
        foreach ($keys as $key) {
187 1
            if (!$this->delete($key)) {
188 1
                $failTimes++;
189
            }
190
        }
191
192 1
        return $failTimes == 0;
193
    }
194
195
    /**
196
     * Determines whether an item is present in the cache.
197
     *
198
     * @param  string $key
199
     * @return boolean
200
     */
201 1
    public function has($key)
202
    {
203 1
        return (bool) $this->get($key);
204
    }
205
206
    /**
207
     * Is the requested driver supported in this environment?
208
     *
209
     * @param  string $driver
210
     * @return array
211
     */
212 1
    public function isSupported($driver)
213
    {
214 1
        static $support;
215
216 1
        if (!isset($support, $support[$driver])) {
217 1
            $support[$driver] = $this->{$driver}->isSupported();
218
        }
219
220 1
        return $support[$driver];
221
    }
222
223
}
224