Completed
Push — master ( feabfc...e4bd18 )
by
unknown
10s
created

PsrCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 9
loc 73
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRateInfo() 0 9 2
A limitRate() 0 17 2
A createRate() 0 15 1
A resetRate() 0 6 1
A createRateInfo() 9 9 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 Noxlogic\RateLimitBundle\Service\Storage\StorageInterface;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
class PsrCache implements StorageInterface
10
{
11
    /**
12
     * @var CacheItemPoolInterface
13
     */
14
    protected $client;
15
16 5
    public function __construct(CacheItemPoolInterface $client)
17
    {
18 5
        $this->client = $client;
19 5
    }
20
21 1
    public function getRateInfo($key)
22
    {
23 1
        $item = $this->client->getItem($key);
24 1
        if (!$item->isHit()) {
25
            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...
26
        }
27
28 1
        return $this->createRateInfo($item->get());
29
    }
30
31 2
    public function limitRate($key)
32
    {
33 2
        $item = $this->client->getItem($key);
34 2
        if (!$item->isHit()) {
35 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...
36
        }
37
38 1
        $info = $item->get();
39
40 1
        $info['calls']++;
41 1
        $item->set($info);
42 1
        $item->expiresAfter($info['reset'] - time());
43
44 1
        $this->client->save($item);
45
46 1
        return $this->createRateInfo($info);
47
    }
48
49 1
    public function createRate($key, $limit, $period)
50
    {
51
        $info = [
52 1
            'limit' => $limit,
53 1
            'calls' => 1,
54 1
            'reset' => time() + $period,
55
        ];
56 1
        $item = $this->client->getItem($key);
57 1
        $item->set($info);
58 1
        $item->expiresAfter($period);
59
60 1
        $this->client->save($item);
61
62 1
        return $this->createRateInfo($info);
63
    }
64
65 1
    public function resetRate($key)
66
    {
67 1
        $this->client->deleteItem($key);
68
69 1
        return true;
70
    }
71
72 3 View Code Duplication
    private function createRateInfo(array $info)
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...
73
    {
74 3
        $rateLimitInfo = new RateLimitInfo();
75 3
        $rateLimitInfo->setLimit($info['limit']);
76 3
        $rateLimitInfo->setCalls($info['calls']);
77 3
        $rateLimitInfo->setResetTimestamp($info['reset']);
78
79 3
        return $rateLimitInfo;
80
    }
81
}
82