DotProperty   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 54
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 4 1
A run() 0 12 2
A getProperty() 0 9 3
A setProperty() 0 8 2
A checkArgs() 0 11 1
1
<?php
2
namespace Desmond\functions\core;
3
use Desmond\functions\DesmondFunction;
4
use Desmond\ArgumentHelper;
5
use Desmond\TypeHelper;
6
use Desmond\exceptions\ArgumentException;
7
8
class DotProperty extends DesmondFunction
9
{
10
    use ArgumentHelper;
11
    use TypeHelper;
12
13 334
    public function id()
14
    {
15 334
        return '.property';
16
    }
17
18 7
    public function run(array $args)
19
    {
20 7
        $this->checkArgs($args);
21 7
        $object = $args[0]->value();
22 7
        $property = $args[1]->value();
23
24 7
        if (isset($args[2])) {
25 3
            $this->setProperty($object, $property, $args[2]);
26
        }
27
28 6
        return $this->getProperty($object, $property);
29
    }
30
31 7
    private function checkArgs($args)
32
    {
33 7
        $this->expectArguments(
34 7
            '.property',
35
            [
36 7
                0 => ['Object', 'String', 'Symbol'],
37
                1 => ['Symbol', 'String']
38
            ],
39 7
            $args
40
        );
41 7
    }
42
43 6
    private function getProperty($object, $property)
44
    {
45 6
        if (!property_exists($object, $property)) {
46 2
            return $this->newReturnType('Nil');
47
        }
48 4
        return is_string($object)
49 1
            ? TypeHelper::fromPhpType($object::$$property)
50 4
            : TypeHelper::fromPhpType($object->$property);
51
    }
52
53 3
    private function setProperty($object, $property, $value)
54
    {
55 3
        if (is_string($object)) {
56 1
            throw new ArgumentException('.property: Static properties cannot be set from an external context.');
57
        }
58 2
        $object->$property = $value->value();
59 2
        return $this->newReturnType('Void');
60
    }
61
}
62