DotProperty::id()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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