Completed
Push — master ( 604a9a...cf79ad )
by Tony Karavasilev (Тони
18:44
created

DisablePropertyOverloadingTrait::__set()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 3
nc 2
nop 2
crap 3.1406
1
<?php
2
3
/**
4
 * Trait implementation of disabled property overloading.
5
 */
6
7
namespace CryptoManana\Core\Traits\DataStructures;
8
9
use \CryptoManana\Core\Interfaces\DataStructures\PropertyOverloadingInterface as PropertyOverloading;
10
11
/**
12
 * Trait DisablePropertyOverloadingTrait - Reusable implementation of disabling property overloading.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\DataStructures\PropertyOverloadingInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\DataStructures
17
 *
18
 * @mixin PropertyOverloading
19
 */
20
trait DisablePropertyOverloadingTrait
21
{
22
    /**
23
     * Magic method invoked when attempting to get the value of an inaccessible or a non-existent property.
24
     *
25
     * @param mixed $name The property name.
26
     *
27
     * @return mixed The property value.
28
     */
29 562
    public function __get($name)
30
    {
31 562
        if (property_exists($this, $name)) {
32 562
            return $this->$name;
33
        } else {
34
            throw new \OutOfBoundsException('The property does not exist.');
35
        }
36
    }
37
38
    /**
39
     * Magic method invoked when attempting to create or modify of an inaccessible or a non-existent property.
40
     *
41
     * @param mixed $name The property name.
42
     * @param mixed $value The property value.
43
     *
44
     * @throws \Exception|\InvalidArgumentException The property does not exist or is from another type.
45
     */
46 50
    public function __set($name, $value)
47
    {
48 50
        if (property_exists($this, $name) && gettype($this->$name) === gettype($value)) {
49 50
            $this->$name = $value;
50
        } else {
51
            throw new \InvalidArgumentException('The property does not exist or is of an invalid type.');
52
        }
53 50
    }
54
55
    /**
56
     * Magic method invoked when checking of an inaccessible or a non-existent property is set.
57
     *
58
     * @param mixed $name The property name.
59
     *
60
     * @return bool The existence check result.
61
     */
62 2
    public function __isset($name)
63
    {
64 2
        return isset($this->$name);
65
    }
66
67
    /**
68
     * Magic method invoked when attempting to unset an inaccessible or a non-existent property.
69
     *
70
     * @param mixed $name The property name.
71
     *
72
     * @throws \Exception|\LogicException The object does not allow dynamic property removal.
73
     */
74
    public function __unset($name)
75
    {
76
        throw new \LogicException('This data structure does not allow deletion of properties.');
77
    }
78
}
79