Completed
Push — master ( 04fddd...f4fff3 )
by Marcin
06:19
created

Ratio::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 2
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;
9
10
use Oefenweb\DamerauLevenshtein\DamerauLevenshtein;
11
12
class Ratio
13
{
14
    /**
15
     * @var string
16
     */
17
    public $title;
18
    /**
19
     * @var string|null
20
     */
21
    public $directorsDisplay;
22
    /**
23
     * @var string|null
24
     */
25
    public $releaseYear;
26
    /**
27
     * @var \mrcnpdlk\Xmdb\Model\RatioElement[]
28
     */
29
    public $items = [];
30
31
    /**
32
     * Ratio constructor.
33
     *
34
     * @param string      $title
35
     * @param string|null $directorsDisplay
36
     * @param string|null $releaseYear
37
     */
38
    public function __construct(string $title, string $directorsDisplay = null, string $releaseYear = null)
39
    {
40
        $this->title            = $title;
41
        $this->directorsDisplay = $directorsDisplay;
42
        $this->releaseYear      = $releaseYear;
43
    }
44
45
    /**
46
     * @param \mrcnpdlk\Xmdb\Model\Imdb\Title[] $tTitles
47
     *
48
     * @return $this
49
     */
50
    public function calculateRatio(array $tTitles)
51
    {
52
        $this->items = [];
53
        foreach ($tTitles as $item) {
54
            $fTitleRatio    = 0;
55
            $fDirectorRatio = 0;
56
            $fYearRatio     = 0;
57 View Code Duplication
            if (!empty($this->title) && !empty($item->titleOrg)) {
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...
58
                $oL          = new DamerauLevenshtein($this->title, $item->titleOrg);
59
                $fTitleRatio = $oL->getRelativeDistance();
60
            }
61
            /** @noinspection IsEmptyFunctionUsageInspection */
62 View Code Duplication
            if (!empty($this->director) && !empty($item->directorsDisplay)) {
0 ignored issues
show
Bug introduced by
The property director does not seem to exist. Did you mean directorsDisplay?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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->directorsDisplay, $item->directorsDisplay);
64
                $fDirectorRatio = $oL->getRelativeDistance();
65
            }
66
            /** @noinspection IsEmptyFunctionUsageInspection */
67
            if (!empty($this->releaseYear) && !empty($item->releaseYear)) {
68
                $oL         = new DamerauLevenshtein($this->releaseYear, $item->releaseYear);
69
                $fYearRatio = $oL->getRelativeDistance();
70
            }
71
            $score = round(($fTitleRatio * 0.5 + $fDirectorRatio * 0.3 + $fYearRatio * 0.2) * 100, 3);
72
73
            $this->items[] = new RatioElement($score, $item);
74
75
        }
76
        usort(
77
            $this->items,
78
            function (RatioElement $itemOne, RatioElement $itemTwo) {
79
                return $itemTwo->score <=> $itemOne->score;
80
            });
81
82
        return $this;
83
    }
84
}
85