Completed
Push — master ( 945176...e11600 )
by Ekin
02:34
created

RedisCounter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 27
loc 60
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A increment() 9 9 2
A decrement() 9 9 2
A get() 0 11 3
A set() 9 9 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 declare(strict_types=1);
2
3
namespace ekinhbayar\GitAmp\Storage;
4
5
use Amp\Promise;
6
use Amp\Redis\Client;
7
use Amp\Redis\RedisException;
8
use function Amp\resolve;
9
10
class RedisCounter implements Counter {
11
    const SCRIPT_DECREMENT = <<<SCRIPT
12
local count = redis.call('decr', KEYS[1])
13
14
if count == 0 then
15
    redis.call('del', KEYS[1])
16
    return 0
17
else
18
    return count
19
end
20
SCRIPT;
21
22
    private $redis;
23
24
    public function __construct(Client $redis) {
25
        $this->redis = $redis;
26
    }
27
28 View Code Duplication
    public function increment(string $key): Promise {
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...
29
        return resolve(function () use ($key) {
30
            try {
31
                return yield $this->redis->incr($key);
32
            } catch (RedisException $e) {
33
                throw new StorageFailedException("Failed to increment counter.", 0, $e);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to increment counter. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
34
            }
35
        });
36
    }
37
38 View Code Duplication
    public function decrement(string $key): Promise {
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...
39
        return resolve(function () use ($key) {
40
            try {
41
                return yield $this->redis->eval(self::SCRIPT_DECREMENT, [$key], []);
42
            } catch (RedisException $e) {
43
                throw new StorageFailedException("Failed to decrement counter.", 0, $e);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to decrement counter. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
44
            }
45
        });
46
    }
47
48
    public function get(string $key): Promise {
49
        return resolve(function () use ($key) {
50
            try {
51
                $result = yield $this->redis->get($key);
52
53
                return empty($result) ? 0 : (int) $result;
54
            } catch (RedisException $e) {
55
                throw new StorageFailedException("Failed to get counter.", 0, $e);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to get counter. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
56
            }
57
        });
58
    }
59
60 View Code Duplication
    public function set(string $key, int $val): Promise {
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...
61
        return resolve(function () use ($key, $val) {
62
            try {
63
                return yield $this->redis->set($key, $val);
64
            } catch (RedisException $e) {
65
                throw new StorageFailedException("Failed to set counter value.", 0, $e);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failed to set counter value. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
66
            }
67
        });
68
    }
69
}
70