CappedStorage   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 19.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 28
loc 141
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setMaxCacheSize() 0 4 1
A setCapWindow() 0 4 1
B put() 0 25 6
A hash() 0 4 1
A invalidate() 0 5 1
A get() 14 15 4
A getValue() 14 15 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace PHPDaemon\Cache;
3
4
/**
5
 * CappedStorage
6
 * @package PHPDaemon\Cache
7
 * @author  Vasily Zorin <[email protected]>
8
 */
9
abstract class CappedStorage
10
{
11
    use \PHPDaemon\Traits\ClassWatchdog;
12
    use \PHPDaemon\Traits\StaticObjectWatchdog;
13
14
    /**
15
     * @var callable Sorter function
16
     */
17
    public $sorter;
18
19
    /**
20
     * @var integer Maximum number of cached elements
21
     */
22
    public $maxCacheSize = 64;
23
24
    /**
25
     * @var integer Additional window to decrease number of sorter calls
26
     */
27
    public $capWindow = 16;
28
29
    /**
30
     * @var array Storage of cached items
31
     */
32
    public $cache = [];
33
34
    /**
35
     * Sets cache size
36
     * @param  integer $size Maximum number of elements
37
     * @return void
38
     */
39
    public function setMaxCacheSize($size)
40
    {
41
        $this->maxCacheSize = $size;
42
    }
43
44
    /**
45
     * Sets cap window
46
     * @param  integer $w Additional window to decrease number of sorter calls
47
     * @return void
48
     */
49
    public function setCapWindow($w)
50
    {
51
        $this->capWindow = $w;
52
    }
53
54
    /**
55
     * Puts element in cache
56
     * @param  string $key Key
57
     * @param  mixed $value Value
58
     * @param  integer $ttl Time to live
0 ignored issues
show
Documentation introduced by
Should the type for parameter $ttl not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
59
     * @return mixed
60
     */
61
    public function put($key, $value, $ttl = null)
62
    {
63
        $k = $this->hash($key);
64
        if (isset($this->cache[$k])) {
65
            $item = $this->cache[$k];
66
            $item->setValue($value);
67
            if ($ttl !== null) {
68
                $item->expire = microtime(true) + $ttl;
69
            }
70
            return $item;
71
        }
72
        $item = new Item($value);
73
        if ($ttl !== null) {
74
            $item->expire = microtime(true) + $ttl;
0 ignored issues
show
Documentation Bug introduced by
The property $expire was declared of type integer, but microtime(true) + $ttl is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
        }
76
        $this->cache[$k] = $item;
77
        $s = sizeof($this->cache);
78
        if ($s > $this->maxCacheSize + $this->capWindow) {
79
            uasort($this->cache, $this->sorter);
80
            for (; $s > $this->maxCacheSize; --$s) {
81
                array_pop($this->cache);
82
            }
83
        }
84
        return $item;
85
    }
86
87
    /**
88
     * Hash function
89
     * @param  string $key Key
90
     * @return integer
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92
    public function hash($key)
93
    {
94
        return $key;
95
    }
96
97
    /**
98
     * Invalidates cache element
99
     * @param  string $key Key
100
     * @return void
101
     */
102
    public function invalidate($key)
103
    {
104
        $k = $this->hash($key);
105
        unset($this->cache[$k]);
106
    }
107
108
    /**
109
     * Gets element by key
110
     * @param  string $key Key
111
     * @return object Item
112
     */
113 View Code Duplication
    public function get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $k = $this->hash($key);
116
        if (!isset($this->cache[$k])) {
117
            return null;
118
        }
119
        $item = $this->cache[$k];
120
        if (isset($item->expire)) {
121
            if (microtime(true) >= $item->expire) {
122
                unset($this->cache[$k]);
123
                return null;
124
            }
125
        }
126
        return $item;
127
    }
128
129
    /**
130
     * Gets value of element by key
131
     * @param  string $key Key
132
     * @return mixed
133
     */
134 View Code Duplication
    public function getValue($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $k = $this->hash($key);
137
        if (!isset($this->cache[$k])) {
138
            return null;
139
        }
140
        $item = $this->cache[$k];
141
        if (isset($item->expire)) {
142
            if (microtime(true) >= $item->expire) {
143
                unset($this->cache[$k]);
144
                return null;
145
            }
146
        }
147
        return $item->getValue();
148
    }
149
}
150