Completed
Push — master ( c4f023...a712a8 )
by Marcin
03:37
created

Ratio   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 11.27 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 8
loc 71
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C calculateRatio() 8 32 8

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
 * Created by Marcin Pudełek <[email protected]>
4
 * Date: 11.12.2017
5
 * Time: 12:42
6
 */
7
8
namespace mrcnpdlk\Xmdb\Model\Imdb;
9
10
11
use Oefenweb\DamerauLevenshtein\DamerauLevenshtein;
12
13
class Ratio
14
{
15
    /**
16
     * @var string
17
     */
18
    public $title;
19
    /**
20
     * @var string|null
21
     */
22
    public $director;
23
    /**
24
     * @var string|null
25
     */
26
    public $year;
27
    /**
28
     * @var \mrcnpdlk\Xmdb\Model\Imdb\RatioElement[]
29
     */
30
    public $items = [];
31
32
    /**
33
     * Ratio constructor.
34
     *
35
     * @param string      $title
36
     * @param string|null $director
37
     * @param string|null $year
38
     */
39
    public function __construct(string $title, string $director = null, string $year = null)
40
    {
41
        $this->title    = $title;
42
        $this->director = $director;
43
        $this->year     = $year;
44
    }
45
46
    /**
47
     * @param \mrcnpdlk\Xmdb\Model\Imdb\Title[] $tTitles
48
     *
49
     * @return $this
50
     */
51
    public function calculateRatio(array $tTitles)
52
    {
53
        $this->items = [];
54
        foreach ($tTitles as $item) {
55
            $fTitleRatio    = 0;
56
            $fDirectorRatio = 0;
57
            $fYearRatio     = 0;
58
            if (null !== $this->title && null !== $item->titleOrg) {
59
                $oL          = new DamerauLevenshtein($this->title, $item->titleOrg);
60
                $fTitleRatio = $oL->getRelativeDistance();
61
            }
62 View Code Duplication
            if (null !== $this->director && null !== $item->directorDisplay) {
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...
63
                $oL             = new DamerauLevenshtein($this->director, $item->directorDisplay);
64
                $fDirectorRatio = $oL->getRelativeDistance();
65
            }
66 View Code Duplication
            if (null !== $this->year && null !== $item->year) {
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...
67
                $oL         = new DamerauLevenshtein($this->year, $item->year);
68
                $fYearRatio = $oL->getRelativeDistance();
69
            }
70
            $score = round((($fTitleRatio + $fDirectorRatio + $fYearRatio) / 3) * 100, 3);
71
72
            $this->items[] = new RatioElement($score, $item);
73
74
        }
75
        usort(
76
            $this->items,
77
            function (RatioElement $itemOne, RatioElement $itemTwo) {
78
                return $itemTwo->score <=> $itemOne->score;
79
            });
80
81
        return $this;
82
    }
83
}