Title::create()   F
last analyzed

Complexity

Conditions 15
Paths 16384

Size

Total Lines 40
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 37
cp 0
rs 2.7451
c 0
b 0
f 0
cc 15
eloc 34
nc 16384
nop 1
crap 240

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by Marcin.
4
 * Date: 09.12.2017
5
 * Time: 23:15
6
 */
7
8
namespace mrcnpdlk\Xmdb\Model\Omdb;
9
10
11
use Carbon\Carbon;
12
13
class Title extends \mrcnpdlk\Xmdb\Model\Title
14
{
15
    /**
16
     * @var
17
     */
18
    public $rated;
19
    /**
20
     * @var string
21
     */
22
    public $awards;
23
    /**
24
     * @var string
25
     */
26
    public $poster;
27
    /**
28
     * @var \mrcnpdlk\Xmdb\Model\Omdb\Rating[]
29
     */
30
    public $ratings = [];
31
    /**
32
     * @var int|null
33
     */
34
    public $metascore;
35
    /**
36
     * @var string
37
     */
38
    public $type;
39
    /**
40
     * @var integer
41
     */
42
    public $totalSeasons;
43
    /**
44
     * @var string | null
45
     */
46
    public $dvd;
47
    /**
48
     * @var string | null
49
     */
50
    public $boxOffice;
51
    /**
52
     * @var string | null
53
     */
54
    public $production;
55
    /**
56
     * @var string | null
57
     */
58
    public $website;
59
60
    /**
61
     * @param \stdClass $oData
62
     *
63
     * @return static
64
     */
65
    public static function create(\stdClass $oData)
66
    {
67
        $oTitle                   = new static();
68
        $oTitle->title            = $oData->Title;
69
        $oTitle->titleOrg         = $oData->Title;
70
        $oTitle->releaseYear      = $oData->Year;
71
        $oTitle->releaseDate      = $oData->Released !== 'N/A' ? Carbon::parse($oData->Released)->format('Y-m-d') : null;
72
        $oTitle->runtime          = (int)$oData->Runtime;
0 ignored issues
show
Documentation Bug introduced by
The property $runtime was declared of type string, but (int) $oData->Runtime is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
73
        $oTitle->genres           = explode(', ', $oData->Genre);
74
        $oTitle->directors        = $oData->Director === 'N/A' ? [] : explode(', ', $oData->Director);
75
        $oTitle->writers          = explode(', ', $oData->Writer);
76
        $oTitle->actors           = explode(', ', $oData->Actors);
77
        $oTitle->plot             = $oData->Plot;
78
        $oTitle->language         = $oData->Language;
79
        $oTitle->countriesDisplay = $oData->Country;
80
        $oTitle->awards           = $oData->Awards;
81
        $oTitle->poster           = $oData->Poster;
82
        $oTitle->metascore        = $oData->Metascore === 'N/A' ? null : (int)$oData->Metascore;
83
        $oTitle->imdbRating       = (float)$oData->imdbRating;
84
        $oTitle->imdbVotes        = (int)preg_replace('/,/', '', $oData->imdbVotes);
85
        $oTitle->imdbId           = $oData->imdbID;
86
        $oTitle->type             = $oData->Type;
87
        $oTitle->totalSeasons     = !isset($oData->totalSeasons) || $oData->totalSeasons === 'N/A' ? null : (int)$oData->totalSeasons;
88
        $oTitle->dvd              = isset($oData->DVD) && $oData->DVD !== 'N/A' ? Carbon::parse($oData->DVD)->format('Y-m-d') : null;
89
        $oTitle->boxOffice        = !isset($oData->BoxOffice) || $oData->BoxOffice === 'N/A' ? null : $oData->BoxOffice;
90
        $oTitle->production       = !isset($oData->Production) || $oData->Production === 'N/A' ? null : $oData->Production;
91
        $oTitle->website          = !isset($oData->Website) || $oData->Website === 'N/A' ? null : $oData->Website;
92
93
        $oTitle->genresDisplay    = implode(', ', $oTitle->genres);
94
        $oTitle->directorsDisplay = implode(', ', $oTitle->directors);
95
        $oTitle->writersDisplay   = implode(', ', $oTitle->writers);
96
        $oTitle->actorsDisplay    = implode(', ', $oTitle->actors);
97
        $oTitle->countries        = explode(', ', $oTitle->countriesDisplay);
98
99
        foreach ((array)$oData->Ratings as $rating) {
100
            $oTitle->ratings[] = new Rating($rating->Source, $rating->Value);
101
        }
102
103
        return $oTitle;
104
    }
105
}
106