TrackCollection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 41.38%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 12
loc 69
rs 10
c 0
b 0
f 0
ccs 12
cts 29
cp 0.4138

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A assertThatTheseAreAllTracks() 12 12 4
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A createSyncProgress() 0 6 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 Iterator;
6
7
final class TrackCollection implements Iterator
8
{
9
    private $tracks;
10
11
    private $position = 0;
12
13
    /**
14
     * TrackCollection constructor.
15
     *
16
     * @param array $tracks An array of TrackInfo objects.
17
     */
18 6
    public function __construct(array $tracks = [])
19
    {
20 6
        $this->assertThatTheseAreAllTracks($tracks);
21 6
        $this->tracks = $tracks;
22 6
    }
23
24 6 View Code Duplication
    private function assertThatTheseAreAllTracks(array $tracks)
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...
25
    {
26 6
        foreach ($tracks as $track) {
27 6
            if (! ($track instanceof TrackInfo)) {
28
                throw new \InvalidArgumentException(sprintf(
29
                    'Expected "%s", but instead got: "%s"',
30
                    TrackInfo::class,
31
                    is_object($track) ? get_class($track) : gettype($track)
32
                ));
33
            }
34 6
        }
35 6
    }
36
37
    public function current()
38
    {
39
        return $this->tracks[$this->position];
40
    }
41
42
    public function next()
43
    {
44
        $this->position++;
45
    }
46
47
    public function key()
48
    {
49
        return $this->position;
50
    }
51
52
    public function valid()
53
    {
54
        return isset($this->tracks[$this->position]);
55
    }
56
57
    public function rewind()
58
    {
59
        $this->position = 0;
60
    }
61
62
    /**
63
     * Create a sync progress.
64
     *
65
     * @param SyncProgressCallback|null $callback
66
     *
67
     * @return SyncProgress
68
     */
69 3
    public function createSyncProgress(SyncProgressCallback $callback = null)
70
    {
71 3
        $total = count($this->tracks);
72
73 3
        return new SyncProgress(0, $total, $callback);
74
    }
75
}
76