Passed
Push — master ( 310acb...1a0150 )
by Scott
02:48
created

Nth::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
namespace Desmond\functions\core;
3
use Desmond\functions\DesmondFunction;
4
use Desmond\ArgumentHelper;
5
use Desmond\data_types\ListType;
6
use Desmond\data_types\VectorType;
7
use Desmond\data_types\NumberType;
8
use Desmond\data_types\NilType;
9
use Desmond\exceptions\ArgumentException;
10
11
class Nth extends DesmondFunction
12
{
13
    use ArgumentHelper;
14
15 187
    public function id()
16
    {
17 187
        return 'nth';
18
    }
19
20 7
    public function run(array $args)
21
    {
22 7
        $this->expectArgs($args);
23 3
        $list = $args[0]->value();
24 3
        $index = $args[1]->value();
25 3
        if (!array_key_exists($index, $list)) {
26 1
            return new NilType();
27
        }
28 2
        return $list[$index];
29
    }
30
31 7
    private function expectArgs($args)
32
    {
33 7
        $this->expectArguments(
34 7
            'nth',
35 7
            [0 => ['List', 'Vector'], 1 => ['Number']],
36
            $args
37
        );
38 3
    }
39
}
40