Map::id()   A
last analyzed

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
namespace Desmond\functions\core;
3
use Desmond\functions\DesmondFunction;
4
use Desmond\ArgumentHelper;
5
use Desmond\exceptions\ArgumentException;
6
7
class Map extends DesmondFunction
8
{
9
    use ArgumentHelper;
10
11 334
    public function id()
12
    {
13 334
        return 'map';
14
    }
15
16 7
    public function run(array $args)
17
    {
18 7
        $args = $this->cloneArgs($args);
19 7
        $this->requireArgTypes($args);
20 5
        $collection = $args[0];
21 5
        $values = $collection->value();
22 5
        $function = $args[1];
23 5
        foreach ($values as $key => $value) {
24 5
            $collection->set(
25 5
                $function->run([$value]), $key);
26
        }
27 5
        return $collection;
28
    }
29
30 7
    private function requireArgTypes(array $args)
31
    {
32 7
        $this->expectArguments(
33 7
            'map',
34 7
            [['List', 'Vector', 'Hash']],
35 7
            $args
36
        );
37 6
        if (!isset($args[1]) || !($args[1] instanceof DesmondFunction)) {
38 1
            throw new ArgumentException('"map" expects argument 1 to be a function.');
39
        }
40 5
    }
41
}
42