Release::__construct()   C
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 5.7204
c 0
b 0
f 0
cc 10
eloc 12
nc 512
nop 2

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
namespace MusicBrainz;
4
5
/**
6
 * Represents a MusicBrainz release object
7
 * @package MusicBrainz
8
 */
9
class Release
10
{
11
    /**
12
     * @var string
13
     */
14
    public $id;
15
    /**
16
     * @var string
17
     */
18
    public $title;
19
    /**
20
     * @var string
21
     */
22
    public $status;
23
    /**
24
     * @var string
25
     */
26
    public $quality;
27
    /**
28
     * @var string
29
     */
30
    public $language;
31
    /**
32
     * @var string
33
     */
34
    public $script;
35
    /**
36
     * @var string
37
     */
38
    public $date;
39
    /**
40
     * @var string
41
     */
42
    public $country;
43
    /**
44
     * @var string
45
     */
46
    public $barcode;
47
    /**
48
     * @var Artist[]
49
     */
50
    public $artists = array();
51
    /**
52
     * @var
53
     */
54
    protected $releaseDate;
55
    /**
56
     * @var array
57
     */
58
    private $data;
59
60
    /**
61
     * @param array       $release
62
     * @param MusicBrainz $brainz
63
     */
64
    public function __construct(array $release, MusicBrainz $brainz)
65
    {
66
        $this->data   = $release;
67
        $this->brainz = $brainz;
0 ignored issues
show
Bug introduced by
The property brainz does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
69
        $this->id       = isset($release['id']) ? (string)$release['id'] : '';
70
        $this->title    = isset($release['title']) ? (string)$release['title'] : '';
71
        $this->status   = isset($release['status']) ? (string)$release['status'] : '';
72
        $this->quality  = isset($release['quality']) ? (string)$release['quality'] : '';
73
        $this->language = isset($release['text-representation']['language']) ? (string)$release['text-representation']['language'] : '';
74
        $this->script   = isset($release['text-representation']['script']) ? (string)$release['text-representation']['script'] : '';
75
        $this->date     = isset($release['date']) ? (string)$release['date'] : '';
76
        $this->country  = isset($release['country']) ? (string)$release['country'] : '';
77
        $this->barcode  = isset($release['barcode']) ? (string)$release['barcode'] : '';
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getId()
84
    {
85
        return $this->id;
86
    }
87
88
    /**
89
     * Get's the earliest release date
90
     * @return \DateTime
91
     */
92
    public function getReleaseDate()
93
    {
94
        if (NULL != $this->releaseDate) {
95
            return $this->releaseDate;
96
        }
97
98
        // If there is no release date set, look through the release events
99
        if (!isset($this->data['date']) && isset($this->data['release-events'])) {
100
            return $this->getReleaseEventDates($this->data['release-events']);
101
        } elseif (isset($this->data['date'])) {
102
            return new \DateTime($this->data['date']);
103
        }
104
105
        return new \DateTime();
106
    }
107
108
    /**
109
     * @param array $releaseEvents
110
     *
111
     * @return \DateTime
112
     */
113
    public function getReleaseEventDates(array $releaseEvents)
114
    {
115
116
        $releaseDate = new \DateTime();
117
118
        foreach ($releaseEvents as $releaseEvent) {
119
            if (isset($releaseEvent['date'])) {
120
                $releaseDateTmp = new \DateTime($releaseEvent['date']);
121
122
                if ($releaseDateTmp < $releaseDate) {
123
                    $releaseDate = $releaseDateTmp;
124
                }
125
            }
126
        }
127
128
        return $releaseDate;
129
    }
130
}
131