PowerRegression   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 15.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 8
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B calculate() 8 36 6

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 Regression;
4
5
/**
6
 * Class PowerRegression
7
 *
8
 * @package Regression
9
 * @author  [email protected]
10
 */
11
class PowerRegression extends AbstractRegression implements InterfaceRegression
12
{
13
    /**
14
     * ExponentialRegression constructor.
15
     */
16
    public function __construct()
17
    {
18
        $this->sumIndex = [0, 0, 0, 0];
19
        $this->dimension = \count($this->sumIndex);
20
    }
21
22
    /**
23
     * @throws RegressionException
24
     */
25
    public function calculate()
26
    {
27
        if ($this->sourceSequence === null) {
28
            throw new RegressionException('The input sequence is not set');
29
        }
30
31
        if (\count($this->sourceSequence) < $this->dimension) {
32
            throw new RegressionException('The dimension of the sequence of at least ' . $this->dimension);
33
        }
34
35
        $k = 0;
36
37 View Code Duplication
        foreach ($this->sourceSequence as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
38
            if ($v[1] !== null) {
39
                $this->sumIndex[0] += \log($v[0]);
40
                $this->sumIndex[1] += \log($v[0]) * log($v[1]);
41
                $this->sumIndex[2] += \log($v[1]);
42
                $this->sumIndex[3] += \pow(\log($v[0]), 2);
43
            }
44
        }
45
46
        ++$k;
47
48
        $B = ($k * $this->sumIndex[1] - $this->sumIndex[2] * $this->sumIndex[0])
49
                / ($k * $this->sumIndex[3] - $this->sumIndex[0] * $this->sumIndex[0]);
50
        $A = \exp(($this->sumIndex[2] - $B * $this->sumIndex[0]) / $k);
51
52
        foreach ($this->sourceSequence as $i => $val) {
53
            $coordinate = [$val[0], $A * \pow($val[0], $B)];
54
            $this->resultSequence[] = $coordinate;
55
        }
56
57
        $this->equation = 'y = ' . \round($A, 2) . ' * x^' . \round($B, 2);
58
59
        $this->push();
60
    }
61
}
62