Completed
Push — master ( ed1b88...1e7555 )
by Pol
10:33 queued 04:50
created

Shift   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 36.75 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 52.27%

Importance

Changes 0
Metric Value
wmc 12
cbo 1
dl 43
loc 117
ccs 23
cts 44
cp 0.5227
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setLength() 7 7 3
A key() 0 4 1
A current() 0 4 1
A next() 18 18 2
A rewind() 18 18 2
A valid() 0 4 1
A count() 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 drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Shift.
9
 */
10
class Shift extends Combinatorics implements \Iterator, \Countable
11
{
12
    /**
13
     * The key.
14
     *
15
     * @var int
16
     */
17
    protected $key = 0;
18
19
    /**
20
     * A copy of the dataset at a give time.
21
     *
22
     * @var array
23
     */
24
    protected $current;
25
26
    /**
27
     * Shift constructor.
28
     *
29
     * @param array $dataset
30
     *                       The dataset
31
     * @param int   $length
32
     *                       The shift length
33
     */
34 8
    public function __construct(array $dataset = [], $length = 1)
35
    {
36 8
        parent::__construct($dataset, $length);
37 8
        $this->current = $this->getDataset();
38 8
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 8 View Code Duplication
    public function setLength($length = null)
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...
44
    {
45 8
        $length = is_null($length) ? $this->datasetCount : $length;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $length. This often makes code more readable.
Loading history...
46 8
        $this->length = (abs($length) > $this->datasetCount) ? $this->datasetCount : $length;
47
48 8
        return $this;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function key()
55
    {
56
        return $this->key;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    public function current()
63
    {
64 4
        return $this->current;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 4 View Code Duplication
    public function next()
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...
71
    {
72 4
        $length = $this->getLength();
73 4
        $parameters = [];
74
75 4
        if ($length < 0) {
76
            $parameters[] = ['start' => abs($length), 'end' => null];
77
            $parameters[] = ['start' => 0, 'end' => abs($length)];
78
        } else {
79 4
            $parameters[] = ['start' => -1 * $length, 'end' => null];
80 4
            $parameters[] = ['start' => 0, 'end' => $this->datasetCount + $length * -1];
81
        }
82
83 4
        $this->current = array_merge(
84 4
          array_slice($this->current, $parameters[0]['start'], $parameters[0]['end']),
85 4
          array_slice($this->current, $parameters[1]['start'], $parameters[1]['end'])
86 4
        );
87 4
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 View Code Duplication
    public function rewind()
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...
93
    {
94
        $length = -1 * $this->getLength();
95
        $parameters = [];
96
97
        if ($length < 0) {
98
            $parameters[] = ['start' => abs($length), 'end' => null];
99
            $parameters[] = ['start' => 0, 'end' => abs($length)];
100
        } else {
101
            $parameters[] = ['start' => -1 * $length, 'end' => null];
102
            $parameters[] = ['start' => 0, 'end' => $this->datasetCount + $length * -1];
103
        }
104
105
        $this->current = array_merge(
106
          array_slice($this->current, $parameters[0]['start'], $parameters[0]['end']),
107
          array_slice($this->current, $parameters[1]['start'], $parameters[1]['end'])
108
      );
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function valid()
115
    {
116
        return true;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 8
    public function count()
123
    {
124 8
        return count($this->getDataset());
125
    }
126
}
127