Completed
Push — master ( 970adc...5463db )
by Nikola
02:02
created

InMemoryStorage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 23.88 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 16
loc 67
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 3
A set() 0 7 1
A increment() 0 4 1
A has() 0 4 1
A hasExpired() 8 8 2
A ttl() 8 8 2

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
/**
3
 * This file is part of the Rate Limit package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace RateLimit\Storage;
14
15
/**
16
 * @author Nikola Posa <[email protected]>
17
 */
18
final class InMemoryStorage implements StorageInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $store = [];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 10
    public function get(string $key, $default = false)
29
    {
30
        if (
31 10
            !$this->has($key)
32 10
            || $this->hasExpired($key)
33
        ) {
34 8
            return $default;
35
        }
36
37 8
        return $this->store[$key]['data'];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 10
    public function set(string $key, $value, int $ttl)
44
    {
45 10
        $this->store[$key] = [
46 10
            'data' => $value,
47 10
            'expires' => time() + $ttl,
48
        ];
49 10
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 7
    public function increment(string $key, int $by)
55
    {
56 7
        $this->store[$key]['data'] += $by;
57 7
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 8 View Code Duplication
    public function ttl(string $key) : int
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...
63
    {
64 8
        if (!isset($this->store[$key]['expires'])) {
65 1
            return -1;
66
        }
67
68 7
        return max($this->store[$key]['expires'] - time(), 0);
69
    }
70
71 10
    private function has(string $key) : bool
72
    {
73 10
        return array_key_exists($key, $this->store);
74
    }
75
76 9 View Code Duplication
    private function hasExpired(string $key) : bool
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...
77
    {
78 9
        if (!isset($this->store[$key]['expires'])) {
79
            return false;
80
        }
81
82 9
        return time() > $this->store[$key]['expires'];
83
    }
84
}
85