Completed
Push — master ( 0a935a...c739e1 )
by Patrick
02:00
created

Harmonic   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 9.23 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 6
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getSoundingPitch() 0 9 1
A getSeries() 3 10 2
A getSeriesAsStringLengths() 3 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
2
3
declare(strict_types=1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Harmonic
8
{
9
    private $halfStop;
10
    private $baseStop;
11
12
    /**
13
     * @param float $halfStop
14
     * @param float $baseStop
15
     */
16
    public function __construct(float $halfStop, float $baseStop = 1.0)
17
    {
18
        if ($halfStop > $baseStop) {
19
            throw new \InvalidArgumentException("The half-stop's string length cannot be longer than the base stop's.");
20
        }
21
22
        $this->baseStop = $baseStop;
23
        $this->halfStop = $halfStop;
24
    }
25
26
    /**
27
     * @param \ExtendedStrings\Strings\VibratingString $string
28
     *
29
     * @return float
30
     */
31
    public function getSoundingPitch(VibratingString $string): float
32
    {
33
        // Transpose the half-stop onto the new string length, which was formed
34
        // by the stop.
35
        $pseudoString = new VibratingString($string->getStoppedFrequency($this->baseStop));
36
        $pseudoHalfStop = $this->halfStop / $this->baseStop;
37
38
        return $pseudoString->getHarmonicSoundingFrequency($pseudoHalfStop);
39
    }
40
41
    /**
42
     * @param int $limit
43
     *
44
     * @return float[]
45
     */
46
    public static function getSeries(int $limit): array
47
    {
48
        $series = [];
49
        $base = 0;
50 View Code Duplication
        for ($denominator = 1; $denominator <= $limit; $denominator++) {
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...
51
            $base = $series[$denominator] = $base + 1 / $denominator;
52
        }
53
54
        return $series;
55
    }
56
57
    /**
58
     * @param int $limit
59
     *
60
     * @return float[]
61
     */
62
    public static function getSeriesAsStringLengths(int $limit): array
63
    {
64
        $series = [];
65 View Code Duplication
        for ($denominator = 1; $denominator <= $limit; $denominator++) {
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...
66
            $series[$denominator] = 1 / $denominator;
67
        }
68
69
        return $series;
70
    }
71
}
72