FirstTrait::firstKey()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 12
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib\Traits;
7
8 View Code Duplication
trait FirstTrait
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * Returns the first element of this iterable or
12
     * returns $default when this iterable is empty
13
     *
14
     * @param mixed $default
15
     * @return mixed
16
     */
17 7
    public function first($default = null)
18
    {
19 7
        if ($this instanceof \Iterator) {
20 6
            $item = $default;
21 6
            foreach ($this as $item) {
22 3
                break;
23
            }
24 6
            return $item;
25
        }
26
27 1
        return null;
28
    }
29
30
    /**
31
     * Returns the key of the first element of this iterable or
32
     * returns $DEFAULT when this iterable is empty
33
     *
34
     * @param mixed $default
35
     * @return mixed
36
     */
37 7
    public function firstKey($default = null)
38
    {
39 7
        if ($this instanceof \Iterator) {
40 6
            $key = $default;
41 6
            foreach ($this as $key => $value) {
42 3
                break;
43
            }
44 6
            return $key;
45
        }
46
47 1
        return null;
48
    }
49
}
50