Completed
Push — master ( 3c7819...8c4383 )
by Scott
02:43
created

Cons::testArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 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 Cons implements DesmondFunction
8
{
9
    use ArgumentHelper;
10
11
    public function id()
12
    {
13
        return 'cons';
14
    }
15
16
    public function run(array $args)
17
    {
18
        $this->testArguments($args);
19
        $newList = $this->newReturnType(
20
            $this->collectionType($args[1]), [$args[0]]);
21
        $oldList = $args[1];
22
        foreach ($oldList->value() as $value) {
23
            $newList->set($value);
24
        }
25
        return $newList;
26
    }
27
28
    public function collectionType($collection)
29
    {
30
        return $this->isDesmondType('List', $collection) ? 'List' : 'Vector';
31
    }
32
33
    private function testArguments(array $args)
34
    {
35
        if (count($args) !== 2) {
36
            throw new ArgumentException('"cons" expects 2 arguments.');
37
        }
38
        $this->expectArguments(
39
            'cons',
40
            [1 => ['List', 'Vector']],
41
            $args
42
        );
43
    }
44
}
45