Map::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
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\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