Completed
Push — 1.x ( ebfb64...c183a4 )
by Alexander
20s
created

PropertyInterceptionTrait::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 12
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2016, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Proxy;
12
13
use Go\Aop\Framework\ClassFieldAccess;
14
use Go\Aop\Intercept\FieldAccess;
15
16
/**
17
 * Trait that holds all general logic for working with intercepted properties
18
 */
19
trait PropertyInterceptionTrait
20
{
21
    /**
22
     * Holds a collection of current values for intercepted properties
23
     *
24
     * @var array
25
     */
26
    private $__properties = [];
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function &__get($name)
32
    {
33
        if (\array_key_exists($name, $this->__properties)) {
34
            /** @var ClassFieldAccess $fieldAccess */
35
            $fieldAccess = self::$__joinPoints["prop:$name"];
36
            $fieldAccess->ensureScopeRule();
37
38
            $value = &$fieldAccess->__invoke($this, FieldAccess::READ, $this->__properties[$name]);
39
        } elseif (\method_exists(\get_parent_class(), __FUNCTION__)) {
40
            $value = parent::__get($name);
41
        } else {
42
            trigger_error("Trying to access undeclared property {$name}");
43
44
            $value = null;
45
        }
46
47
        return $value;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function __set($name, $value)
54
    {
55
        if (\array_key_exists($name, $this->__properties)) {
56
            /** @var ClassFieldAccess $fieldAccess */
57
            $fieldAccess = self::$__joinPoints["prop:$name"];
58
            $fieldAccess->ensureScopeRule();
59
60
            $this->__properties[$name] = $fieldAccess->__invoke(
61
                $this,
62
                FieldAccess::WRITE,
63
                $this->__properties[$name],
64
                $value
65
            );
66
        } elseif (\method_exists(\get_parent_class(), __FUNCTION__)) {
67
            parent::__set($name, $value);
68
        } else {
69
            $this->$name = $value;
70
        }
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function __isset($name)
77
    {
78
        return isset($this->__properties[$name]);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function __unset($name)
85
    {
86
        $this->__properties[$name] = null;
87
    }
88
}
89