Completed
Push — symfony-4 ( 875930...01c16a )
by
unknown
03:33
created

Redis   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 64
loc 64
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A getRateInfo() 14 14 4
A limitRate() 11 11 2
A createRate() 16 16 1
A resetRate() 6 6 1

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
namespace Noxlogic\RateLimitBundle\Service\Storage;
4
5
use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
6
use Predis\Client;
7
8 View Code Duplication
class Redis implements StorageInterface
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * @var \Predis\Client
12
     */
13
    protected $client;
14
15 5
    public function __construct(Client $client)
16
    {
17 5
        $this->client = $client;
18 5
    }
19
20 3
    public function getRateInfo($key)
21
    {
22 3
        $info = $this->client->hgetall($key);
23 3
        if (!isset($info['limit']) || !isset($info['calls']) || !isset($info['reset'])) {
24 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Noxlogic\RateLimitBundle...eInterface::getRateInfo of type Noxlogic\RateLimitBundle\Service\RateLimitInfo.

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...
25
        }
26
27 2
        $rateLimitInfo = new RateLimitInfo();
28 2
        $rateLimitInfo->setLimit($info['limit']);
29 2
        $rateLimitInfo->setCalls($info['calls']);
30 2
        $rateLimitInfo->setResetTimestamp($info['reset']);
31
32 2
        return $rateLimitInfo;
33
    }
34
35 2
    public function limitRate($key)
36
    {
37 2
        $info = $this->getRateInfo($key);
38 2
        if (!$info) {
39 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Noxlogic\RateLimitBundle...ageInterface::limitRate of type Noxlogic\RateLimitBundle\Service\RateLimitInfo.

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...
40
        }
41
42 1
        $this->client->hincrby($key, 'calls', 1);
43
44 1
        return $info;
45
    }
46
47 1
    public function createRate($key, $limit, $period)
48
    {
49 1
        $reset = time() + $period;
50
51 1
        $this->client->hset($key, 'limit', $limit);
52 1
        $this->client->hset($key, 'calls', 1);
53 1
        $this->client->hset($key, 'reset', $reset);
54 1
        $this->client->expire($key, $period);
55
56 1
        $rateLimitInfo = new RateLimitInfo();
57 1
        $rateLimitInfo->setLimit($limit);
58 1
        $rateLimitInfo->setCalls(1);
59 1
        $rateLimitInfo->setResetTimestamp($reset);
60
61 1
        return $rateLimitInfo;
62
    }
63
64 1
    public function resetRate($key)
65
    {
66 1
        $this->client->del($key);
67
68 1
        return true;
69
    }
70
71
}
72