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 ( af400a...61ccff )
by Hilari
02:07
created

ArrayCache::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
namespace Cmp\Cache\Infrastructure\Backend;
4
5
use Cmp\Cache\Domain\Cache;
6
use Cmp\Cache\Domain\Exceptions\CacheException;
7
use Cmp\Cache\Domain\Exceptions\ExpiredException;
8
use Cmp\Cache\Domain\Exceptions\NotFoundException;
9
10
/**
11
 * Class ArrayCache
12
 * 
13
 * A simple backend powered by an array in memory
14
 *
15
 * @package Cmp\Cache\Infrastureture\Backend
16
 */
17
class ArrayCache implements Cache
18
{
19
    /**
20
     * Stored items
21
     * 
22
     * @var array
23
     */
24
    private $items = [];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function delete($key)
30
    {
31
        if (array_key_exists($key, $this->items)) {
32
            unset($this->items[$key]);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function set($key, $value, $timeToLive = 0)
40
    {
41
        $this->items[$key] = [
42
            'value'      => $value,
43
            'expireTime' => $timeToLive === 0 ? $timeToLive : time() + $timeToLive,
44
        ];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function has($key)
51
    {
52
        if (!array_key_exists($key, $this->items)) {
53
            return false;
54
        }
55
56
        if ($this->hasExpired($key)) {
57
            return (bool) $this->delete($key);
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function get($key)
67
    {
68
        if (!array_key_exists($key, $this->items)) {
69
            throw new NotFoundException($key, static::class);
0 ignored issues
show
Unused Code introduced by
The call to NotFoundException::__construct() has too many arguments starting with static::class.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
        }
71
72
        if ($this->hasExpired($key)) {
73
            $this->delete($key);
74
            throw new ExpiredException($key, static::class);
0 ignored issues
show
Unused Code introduced by
The call to ExpiredException::__construct() has too many arguments starting with static::class.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function pull($key, $default = null)
84
    {
85
        try {
86
            return $this->get($key);
87
        } catch (CacheException $exception) {
88
            return $default;
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    private function hasExpired($key)
96
    {
97
        return $this->items[$key]['expireTime'] !== 0 && $this->items[$key]['expireTime'] < time();
98
    }
99
}
100