TrackInfo   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 65
Duplicated Lines 38.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 25
loc 65
rs 10
c 0
b 0
f 0
ccs 22
cts 34
cp 0.6471

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A assertValidName() 9 9 3
B assertThatTheseAreAllArtists() 16 16 5
A getName() 0 4 1
A getArtists() 0 4 1
A toString() 0 8 1
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace HansOtt\Lastify;
4
5
use InvalidArgumentException;
6
use HansOtt\Lastify\TrackInfo\Artist;
7
8
final class TrackInfo
9
{
10
    private $name;
11
12
    private $artists;
13
14 18
    public function __construct($name, array $artists = [])
15
    {
16 18
        $this->assertValidName($name);
17 15
        $this->assertThatTheseAreAllArtists($artists);
18 12
        $this->name = $name;
19 12
        $this->artists = $artists;
20 12
    }
21
22 18 View Code Duplication
    private function assertValidName($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
23
    {
24 18
        if (!is_string($name)) {
25
            throw new InvalidArgumentException('The name should be a string, instead got:' . gettype($name));
26
        }
27 18
        if (empty($name)) {
28 3
            throw new InvalidArgumentException('The name cannot be empty');
29
        }
30 15
    }
31
32 15 View Code Duplication
    private function assertThatTheseAreAllArtists(array $artists)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
33
    {
34 15
        if (empty($artists)) {
35 3
            throw new InvalidArgumentException('A track should always have an artist');
36
        }
37
38 12
        foreach ($artists as $artist) {
39 12
            if (! ($artist instanceof Artist)) {
40
                throw new \InvalidArgumentException(sprintf(
41
                    'Expected "%s", but instead got: "%s"',
42
                    Artist::class,
43
                    is_object($artist) ? get_class($artist) : gettype($artist)
44
                ));
45
            }
46 12
        }
47 12
    }
48
49
    public function getName()
50
    {
51
        return $this->name;
52
    }
53
54
    public function getArtists()
55
    {
56
        return $this->artists;
57
    }
58
59
    public function toString()
60
    {
61 3
        $artists = array_map(function (Artist $artist) {
62 3
            return $artist->getName();
63 3
        }, $this->artists);
64
65 3
        return sprintf('%s - %s', $this->name, implode(', ', $artists));
66
    }
67
68
    public function __toString()
69
    {
70
        return $this->toString();
71
    }
72
}
73