Completed
Branch master (5720e6)
by Scott
05:47 queued 02:57
created

DotNew::getArgValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
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 DotNew implements DesmondFunction
8
{
9
    use ArgumentHelper;
10
11 155
    public function id()
12
    {
13 155
        return '.new';
14
    }
15
16 14
    public function run(array $args)
17
    {
18 14
        if (count($args) == 0) {
19 1
            return $this->newReturnType('Object', new \stdClass());
20
        }
21 13
        $this->checkClass($args);
22 11
        $object = $args[0]->value();
23 11
        $args = $this->getArgValues($args);
24 11
        return $this->newReturnType('Object', new $object(...$args));
25
    }
26
27 13
    private function checkClass($args)
28
    {
29 13
        $this->expectArguments(
30 13
            '.new', [0 => ['Symbol', 'String']], $args
31
        );
32 12
        if (!class_exists($args[0]->value())) {
33 1
            throw new ArgumentException('".new": Class "' . $args[0]->value() . '" not found.');
34
        }
35 11
    }
36
37 11
    private function getArgValues($args)
38
    {
39 11
        array_shift($args);
40 11
        foreach ($args as $key => $arg) {
41 1
            $args[$key] = $arg->value();
42
        }
43 11
        return $args;
44
    }
45
}
46