Passed
Push — master ( 1e7555...a49478 )
by Pol
03:09
created

Shift   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 6.25 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 12
cbo 1
dl 7
loc 112
ccs 24
cts 33
cp 0.7272
rs 10
c 0
b 0
f 0

9 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() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A count() 0 4 1
A doShift() 0 17 2

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 12
    public function __construct(array $dataset = [], $length = 1)
35
    {
36 12
        parent::__construct($dataset, $length);
37 12
        $this->current = $this->getDataset();
38 12
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 12 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 12
        $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 12
        $this->length = (abs($length) > $this->datasetCount) ? $this->datasetCount : $length;
47
48 12
        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 8
    public function next()
71
    {
72 8
        $this->doShift(1);
73 8
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function rewind()
79
    {
80
        $this->doShift(-1);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function valid()
87
    {
88
        return true;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 12
    public function count()
95
    {
96 12
        return count($this->getDataset());
97
    }
98
99
    /**
100
     * Internal function to do the shift.
101
     *
102
     * @param int $length
103
     */
104 8
    protected function doShift($length = 1)
105
    {
106 8
        $parameters = [];
107
108 8
        if ($length < 0) {
109
            $parameters[] = ['start' => abs($length), 'end' => null];
110
            $parameters[] = ['start' => 0, 'end' => abs($length)];
111
        } else {
112 8
            $parameters[] = ['start' => -1 * $length, 'end' => null];
113 8
            $parameters[] = ['start' => 0, 'end' => $this->datasetCount + $length * -1];
114
        }
115
116 8
        $this->current = array_merge(
117 8
        array_slice($this->current, $parameters[0]['start'], $parameters[0]['end']),
118 8
        array_slice($this->current, $parameters[1]['start'], $parameters[1]['end'])
119
      );
120 8
    }
121
}
122