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

Ratio::calculateRatio()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 32
Code Lines 22

Duplication

Lines 8
Ratio 25 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 8
loc 32
ccs 0
cts 29
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 22
nc 9
nop 1
crap 72
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
}