TypedProperty::__get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
1
<?php
2
3
namespace Teto\Object;
4
5
/**
6
 * Typed property function for class
7
 *
8
 * @author    USAMI Kenta <[email protected]>
9
 * @copyright 2016 Baguette HQ
10
 * @license   http://www.apache.org/licenses/LICENSE-2.0
11
 */
12
trait TypedProperty
13
{
14
    use TypeAssert;
15
16
    /** @var array */
17
    private $properties = [];
18
19
    /**
20
     * Set property (magic method)
21
     *
22
     * @param  string $name
23
     * @param  mixed  $value
24
     * @throws \OutOfRangeException      If you set to undefined property
25
     * @throws \InvalidArgumentException If differed from the defined type
26
     * @throws \RangeException           If differed from the defined length
27
     * @see    http://php.net/manual/language.oop5.magic.php
28
     * @suppress PhanUndeclaredStaticProperty
29
     */
30 25
    public function __set($name, $value)
31
    {
32 25
        if (!isset(self::$property_types[$name])) {
0 ignored issues
show
Bug Best Practice introduced by
The property property_types does not exist on Teto\Object\TypedProperty. Since you implemented __get, consider adding a @property annotation.
Loading history...
33 1
            throw new \OutOfRangeException("Unexpected key:'$name'");
34
        }
35
36 24
        $type = TypeDefinition::parse(self::$property_types[$name]);
37
38 24
        if ($type->is_array) {
39 13
            self::assertArrayOrObject($value);
40 12
            $values = $value;
41
        } else {
42 11
            $values = [$value];
43
        }
44
45 23
        foreach ($values as $v) {
46 20
            self::assertValue($type->expected, $name, $v, $type->is_nullable);
47
        }
48 17
        if ($type->len !== null && count($value) !== $type->len) {
49 2
            throw new \RangeException("Unexpected length:{$name} (expects {$type->len})");
50
        }
51
52 15
        $this->properties[$name] = $value;
53 15
    }
54
55
    /**
56
     * Get property (magic method)
57
     *
58
     * @param  string $name
59
     * @return mixed
60
     * @see    http://php.net/manual/language.oop5.magic.php
61
     * @suppress PhanUndeclaredStaticProperty
62
     */
63 15
    public function __get($name)
64
    {
65 15
        if (!isset(self::$property_types[$name])) {
0 ignored issues
show
Bug Best Practice introduced by
The property property_types does not exist on Teto\Object\TypedProperty. Since you implemented __get, consider adding a @property annotation.
Loading history...
66
            throw new \OutOfRangeException("Unexpected key:'$name'");
67
        }
68
69 15
        return isset($this->properties[$name]) ? $this->properties[$name] : null;
70
    }
71
72
    /**
73
     * @param  string $name
74
     * @return bool
75
     * @see    http://php.net/manual/language.oop5.magic.php
76
     */
77 15
    public function __isset($name)
78
    {
79 15
        return isset($this->properties[$name]);
80
    }
81
82
    /**
83
     * @param string $name
84
     * @see   http://php.net/manual/language.oop5.magic.php
85
     * @suppress PhanUndeclaredStaticProperty
86
     */
87
    public function __unset($name)
88
    {
89
        if (isset(self::$property_types[$name])) {
0 ignored issues
show
Bug Best Practice introduced by
The property property_types does not exist on Teto\Object\TypedProperty. Since you implemented __get, consider adding a @property annotation.
Loading history...
90
            $this->properties[$name] = null;
91
        }
92
    }
93
94
    /**
95
     * @param array
96
     * @suppress PhanUndeclaredStaticProperty
97
     */
98
    private function setProperties(array $properties)
99
    {
100
        foreach (self::$property_types as $name => $_) {
0 ignored issues
show
Bug Best Practice introduced by
The property property_types does not exist on Teto\Object\TypedProperty. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
            if (isset($properties[$name])) {
102
                $this->$name = $properties[$name];
103
            }
104
        }
105
    }
106
}
107