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

Shift::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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