Completed
Pull Request — master (#440)
by
unknown
25:11
created

PropertyInterceptionTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 18 3
A __set() 0 19 3
A __isset() 0 4 1
A __unset() 0 4 1
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
15
/**
16
 * Trait that holds all general logic for working with intercepted properties
17
 */
18
trait PropertyInterceptionTrait
19
{
20
    /**
21
     * Holds a collection of current values for intercepted properties
22
     *
23
     * @var array
24
     */
25
    private $__properties = [];
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function &__get($name)
31
    {
32
        if (\array_key_exists($name, $this->__properties)) {
33
            /** @var ClassFieldAccess $fieldAccess */
34
            $fieldAccess = self::$__joinPoints["prop:$name"];
35
            $fieldAccess->ensureScopeRule();
36
37
            $value = &$fieldAccess->__invoke($this, ClassFieldAccess::READ, $this->__properties[$name]);
38
        } elseif (\method_exists(\get_parent_class(), __FUNCTION__)) {
39
            $value = parent::__get($name);
40
        } else {
41
            trigger_error("Trying to access undeclared property {$name}");
42
43
            $value = null;
44
        }
45
46
        return $value;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function __set($name, $value)
53
    {
54
        if (\array_key_exists($name, $this->__properties)) {
55
            /** @var ClassFieldAccess $fieldAccess */
56
            $fieldAccess = self::$__joinPoints["prop:$name"];
57
            $fieldAccess->ensureScopeRule();
58
59
            $this->__properties[$name] = $fieldAccess->__invoke(
60
                $this,
61
                ClassFieldAccess::WRITE,
62
                $this->__properties[$name],
63
                $value
64
            );
65
        } elseif (\method_exists(\get_parent_class(), __FUNCTION__)) {
66
            parent::__set($name, $value);
67
        } else {
68
            $this->$name = $value;
69
        }
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function __isset($name)
76
    {
77
        return isset($this->__properties[$name]);
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function __unset($name)
84
    {
85
        $this->__properties[$name] = null;
86
    }
87
}
88