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

DotNew   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 39
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 4 1
A run() 0 10 2
A checkClass() 0 9 2
A getArgValues() 0 8 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