SeekableTrait::seek()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * File SeekableTrait.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Collection\Traits;
8
9
/**
10
 * Trait SeekableTrait
11
 *
12
 * @property array|mixed[] $elements
13
 * @package Epfremme\Collection\Traits
14
 * @uses \SeekableIterator
15
 */
16
trait SeekableTrait
17
{
18
    /**
19
     * Seek to a position within the collection
20
     *
21
     * @param $position
22
     * @return void
23
     */
24 5
    public function seek($position)
25
    {
26 5
        if (!array_key_exists($position, $this->elements)) {
27 1
            throw new \InvalidArgumentException(
28 1
                sprintf('Position %s does not exist in collection', $position)
29 1
            );
30
        }
31
32 4
        reset($this->elements);
33
34 4
        while (key($this->elements) !== $position) {
35 4
            next($this->elements);
36 4
        }
37 4
    }
38
}
39