Completed
Push — master ( bd02ec...4ff445 )
by Dmytro
04:14
created

ExponentialStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 15.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 8
loc 52
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setInterval() 8 8 3
A setBase() 0 8 3
A process() 0 4 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
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Infrastructure\Strategy;
6
7
8
/**
9
 * Class ExponentialStrategy.
10
 */
11
final class ExponentialStrategy extends AbstractStrategy
12
{
13
    /** @var int */
14
    protected $interval;
15
16
    /** @var float */
17
    protected $base;
18
19
    /**
20
     * @param int $interval
21
     * @param float $base
22
     */
23 3
    public function __construct(int $interval = 5, float $base = 2.0)
24
    {
25 3
        $this->setInterval($interval);
26 2
        $this->setBase($base);
27 2
    }
28
29
    /**
30
     * @param int $interval
31
     */
32 3 View Code Duplication
    public function setInterval($interval)
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...
33
    {
34 3
        if (!is_int($interval) || (int) $interval < 0) {
35 1
            throw new \InvalidArgumentException('Interval should be positive integer');
36
        }
37
38 2
        $this->interval = (int) $interval;
39 2
    }
40
41
    /**
42
     * @param $base
43
     */
44 2
    public function setBase($base)
45
    {
46 2
        if (!is_numeric($base) || (float) $base < 0) {
47
            throw new \InvalidArgumentException('Base should be positive float');
48
        }
49
50 2
        $this->base = (float) $base;
51 2
    }
52
53
    /**
54
     * @param int $attempt
55
     *
56
     * @return int
57
     */
58 1
    public function process(int $attempt): int
59
    {
60 1
        return (int) ceil($this->interval + ($this->base ** $attempt));
61
    }
62
}