jaytaph /
RateLimitBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Noxlogic\RateLimitBundle\Service\Storage; |
||
| 4 | |||
| 5 | use Noxlogic\RateLimitBundle\Service\RateLimitInfo; |
||
| 6 | use Psr\SimpleCache\CacheInterface; |
||
| 7 | |||
| 8 | class SimpleCache implements StorageInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var CacheInterface |
||
| 12 | */ |
||
| 13 | protected $client; |
||
| 14 | |||
| 15 | 5 | public function __construct(CacheInterface $client) |
|
| 16 | { |
||
| 17 | 5 | $this->client = $client; |
|
| 18 | 5 | } |
|
| 19 | |||
| 20 | 1 | public function getRateInfo($key) |
|
| 21 | { |
||
| 22 | 1 | $info = $this->client->get($key); |
|
| 23 | 1 | if ($info === null || !array_key_exists('limit', $info)) { |
|
| 24 | return false; |
||
|
0 ignored issues
–
show
|
|||
| 25 | } |
||
| 26 | |||
| 27 | 1 | return $this->createRateInfo($info); |
|
| 28 | } |
||
| 29 | |||
| 30 | 2 | View Code Duplication | public function limitRate($key) |
|
0 ignored issues
–
show
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...
|
|||
| 31 | { |
||
| 32 | 2 | $info = $this->client->get($key); |
|
| 33 | 2 | if ($info === null || !array_key_exists('limit', $info)) { |
|
| 34 | 1 | return false; |
|
|
0 ignored issues
–
show
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 Loading history...
|
|||
| 35 | } |
||
| 36 | |||
| 37 | 1 | $info['calls']++; |
|
| 38 | 1 | $ttl = $info['reset'] - time(); |
|
| 39 | |||
| 40 | 1 | $this->client->set($key, $info, $ttl); |
|
| 41 | |||
| 42 | 1 | return $this->createRateInfo($info); |
|
| 43 | } |
||
| 44 | |||
| 45 | 1 | public function createRate($key, $limit, $period) |
|
| 46 | { |
||
| 47 | $info = [ |
||
| 48 | 1 | 'limit' => $limit, |
|
| 49 | 1 | 'calls' => 1, |
|
| 50 | 1 | 'reset' => time() + $period, |
|
| 51 | ]; |
||
| 52 | 1 | $this->client->set($key, $info, $period); |
|
| 53 | |||
| 54 | 1 | return $this->createRateInfo($info); |
|
| 55 | } |
||
| 56 | |||
| 57 | 1 | public function resetRate($key) |
|
| 58 | { |
||
| 59 | 1 | $this->client->delete($key); |
|
| 60 | |||
| 61 | 1 | return true; |
|
| 62 | } |
||
| 63 | |||
| 64 | 3 | View Code Duplication | private function createRateInfo(array $info) |
|
0 ignored issues
–
show
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...
|
|||
| 65 | { |
||
| 66 | 3 | $rateLimitInfo = new RateLimitInfo(); |
|
| 67 | 3 | $rateLimitInfo->setLimit($info['limit']); |
|
| 68 | 3 | $rateLimitInfo->setCalls($info['calls']); |
|
| 69 | 3 | $rateLimitInfo->setResetTimestamp($info['reset']); |
|
| 70 | |||
| 71 | 3 | return $rateLimitInfo; |
|
| 72 | } |
||
| 73 | } |
||
| 74 |
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.