Recording::setReleases()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace MusicBrainz;
4
5
/**
6
 * Represents a MusicBrainz Recording object
7
 * @package MusicBrainz
8
 */
9
class Recording
10
{
11
    /**
12
     * @var string
13
     */
14
    public $id;
15
    /**
16
     * @var string
17
     */
18
    public $title;
19
    /**
20
     * @var int
21
     */
22
    public $score;
23
    /**
24
     * @var Release[]
25
     */
26
    public $releases = array();
27
    /**
28
     * @var array
29
     */
30
    private $data;
31
32
    /**
33
     * @param array       $recording
34
     * @param MusicBrainz $brainz
35
     */
36
    public function __construct(array $recording, MusicBrainz $brainz)
37
    {
38
        $this->data   = $recording;
39
        $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...
40
41
        $this->id       = (string)$recording['id'];
42
        $this->title    = (string)$recording['title'];
43
        $this->length   = (isset($recording['length'])) ? (int)$recording['length'] : 0;
0 ignored issues
show
Bug introduced by
The property length 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...
44
        $this->score    = (isset($recording['score'])) ? (int)$recording['score'] : 0;
45
        $this->artistID = $recording['artist-credit'][0]['artist']['id'];
0 ignored issues
show
Bug introduced by
The property artistID 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...
46
47
        if (isset($recording['releases'])) {
48
            $this->setReleases($recording['releases']);
49
        }
50
    }
51
52
    /**
53
     * @param array $releases
54
     *
55
     * @return $this
56
     */
57
    public function setReleases(array $releases)
58
    {
59
        foreach ($releases as $release) {
60
            array_push($this->releases, new Release($release, $this->brainz));
61
        }
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getScore()
70
    {
71
        return $this->score;
72
    }
73
74
    /**
75
     * @throws Exception
76
     * @return array
77
     */
78
    public function getReleaseDates()
79
    {
80
81
        if (empty($this->releases)) {
82
            throw new Exception('Could not find any releases in the recording');
83
        }
84
85
        $releaseDates = array();
86
87
        foreach ($this->releases as $release) {
88
            /** @var Release $release */
89
            array_push($releaseDates, $release->getReleaseDate());
90
        }
91
92
        asort($releaseDates);
93
94
        return $releaseDates;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getId()
101
    {
102
        return $this->id;
103
    }
104
105
    /**
106
     * @return Artist
107
     */
108
    public function getArtist()
109
    {
110
        $includes = array(
111
            'releases',
112
            'recordings',
113
            'release-groups',
114
            'user-ratings'
115
        );
116
117
        $artist = $this->brainz->lookup('artist', $this->artistID, $includes);
118
119
        return new Artist($artist, $this->brainz);
120
    }
121
122
    /**
123
     * @param string $format
124
     *
125
     * @return int|string
126
     */
127
    public function getLength($format = 'int')
128
    {
129
        switch ($format) {
130
            case 'short':
131
                return str_replace('.', ':', number_format(($this->length / 1000 / 60), 2));
132
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
133
            case 'long':
134
                return str_replace('.', 'm ', number_format(($this->length / 1000 / 60), 2)) . 's';
135
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
136
            case 'int':
137
            default:
138
                return $this->length;
139
        }
140
    }
141
}
142