Completed
Pull Request — master (#40)
by Monse
02:20
created

Database::resetRate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 4
loc 4
ccs 0
cts 4
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Noxlogic\RateLimitBundle\Service\Storage;
4
5
6
use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
7
use Noxlogic\RateLimitBundle\Entity\PdoHandler;
8
9 View Code Duplication
class Database 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...
10
{
11
    //define client
12
    /**
13
     * @var PdoHandler
14
     */
15
    protected $client;
16
17
    public function __construct($client) {
18
        $this->client = $client;
19
    }
20
21
    public function getRateInfo($key) {
22
        $info = $this->client->fetch($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $info is correct as $this->client->fetch($key) (which targets Noxlogic\RateLimitBundle...ity\PdoHandler::fetch()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
23
24
        $rateLimitInfo = new RateLimitInfo();
25
        $rateLimitInfo->setLimit($info['limit']);
26
        $rateLimitInfo->setCalls($info['calls']);
27
        $rateLimitInfo->setResetTimestamp($info['reset']);
28
29
        return $rateLimitInfo;
30
    }
31
32
    public function limitRate($key) {
33
        $info = $this->client->fetch($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $info is correct as $this->client->fetch($key) (which targets Noxlogic\RateLimitBundle...ity\PdoHandler::fetch()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
        if ($info === false || !array_key_exists('limit', $info)) {
35
            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
        $info['calls']++;
39
40
        $expire = $info['reset'] - time();
41
42
        $this->client->save($key, $info, $expire);
0 ignored issues
show
Unused Code introduced by
The call to PdoHandler::save() has too many arguments starting with $expire.

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...
43
44
        return $this->getRateInfo($key);
45
    }
46
47
    public function createRate($key, $limit, $period) {
48
        $info          = array();
49
        $info['limit'] = $limit;
50
        $info['calls'] = 1;
51
        $info['reset'] = time() + $period;
52
53
        $this->client->save($key, $info);
54
55
        return $this->getRateInfo($key);
56
    }
57
58
    public function resetRate($key) {
59
        $this->client->delete($key);
60
        return true;
61
    }
62
63
    public function fetch($key){
64
        return $this->client->fetch($key);
65
    }
66
}