Completed
Push — master ( 230580...1e3540 )
by Alexander
02:33
created

ClassFieldAccess   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Test Coverage

Coverage 15.87%

Importance

Changes 0
Metric Value
wmc 23
lcom 4
cbo 3
dl 0
loc 210
ccs 10
cts 63
cp 0.1587
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getAccessType() 0 4 1
A getField() 0 4 1
A getValue() 0 6 1
A getValueToSet() 0 6 1
B ensureScopeRule() 0 22 7
A proceed() 0 8 2
A __invoke() 0 32 4
A getThis() 0 4 1
A getStaticPart() 0 4 1
A __toString() 0 9 2
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2011, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Aop\Framework;
13
14
use Go\Aop\AspectException;
15
use Go\Aop\Intercept\FieldAccess;
16
use Go\Aop\Support\AnnotatedReflectionProperty;
17
use ReflectionProperty;
18
use function get_class;
19
20
/**
21
 * Represents a field access joinpoint
22
 */
23
class ClassFieldAccess extends AbstractJoinpoint implements FieldAccess
24
{
25
26
    /**
27
     * Instance of object for accessing
28
     *
29
     * @var object
30
     */
31
    protected $instance;
32
33
    /**
34
     * Instance of reflection property
35
     *
36
     * @var ReflectionProperty
37
     */
38
    protected $reflectionProperty;
39
40
    /**
41
     * New value to set
42
     *
43
     * @var mixed
44
     */
45
    protected $newValue;
46
47
    /**
48
     * Access type for field access
49
     *
50
     * @var integer
51
     */
52
    private $accessType;
53
54
    /**
55
     * Copy of the original value of property
56
     *
57
     * @var mixed
58
     */
59
    private $value;
60
61
    /**
62
     * Constructor for field access
63
     *
64
     * @param $advices array List of advices for this invocation
65
     */
66 4
    public function __construct(string $className, string $fieldName, array $advices)
67
    {
68 4
        parent::__construct($advices);
69
70 4
        $this->reflectionProperty = $reflectionProperty = new AnnotatedReflectionProperty($className, $fieldName);
71
        // Give an access to protected field
72 4
        if ($reflectionProperty->isProtected()) {
73 4
            $reflectionProperty->setAccessible(true);
74
        }
75 4
    }
76
77
    /**
78
     * Returns the access type.
79
     */
80
    public function getAccessType(): int
81
    {
82
        return $this->accessType;
83
    }
84
85
    /**
86
     * Gets the field being accessed.
87
     *
88
     * @return AnnotatedReflectionProperty the property which is being accessed.
89
     */
90 3
    public function getField(): ReflectionProperty
91
    {
92 3
        return $this->reflectionProperty;
93
    }
94
95
    /**
96
     * Gets the current value of property
97
     */
98
    public function &getValue()
99
    {
100
        $value = &$this->value;
101
102
        return $value;
103
    }
104
105
    /**
106
     * Gets the value that must be set to the field.
107
     */
108
    public function &getValueToSet()
109
    {
110
        $newValue = &$this->newValue;
111
112
        return $newValue;
113
    }
114
115
    /**
116
     * Checks scope rules for accessing property
117
     */
118
    public function ensureScopeRule(int $stackLevel = 2): void
119
    {
120
        $property    = $this->reflectionProperty;
121
        $isProtected = $property->isProtected();
122
        $isPrivate   = $property->isPrivate();
123
        if ($isProtected || $isPrivate) {
124
            $backTrace     = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $stackLevel+1);
125
            $accessor      = $backTrace[$stackLevel] ?? [];
126
            $propertyClass = $property->class;
127
            if (isset($accessor['class'])) {
128
                // For private and protected properties its ok to access from the same class
129
                if ($accessor['class'] === $propertyClass) {
130
                    return;
131
                }
132
                // For protected properties its ok to access from any subclass
133
                if ($isProtected && is_subclass_of($accessor['class'], $propertyClass)) {
134
                    return;
135
                }
136
            }
137
            throw new AspectException("Cannot access property {$propertyClass}::{$property->name}");
138
        }
139
    }
140
141
    /**
142
     * Proceed to the next interceptor in the Chain
143
     *
144
     * @return void For field interceptor there is no return values
145
     */
146
    final public function proceed()
147
    {
148
        if (isset($this->advices[$this->current])) {
149
            $currentInterceptor = $this->advices[$this->current++];
150
151
            $currentInterceptor->invoke($this);
152
        }
153
    }
154
155
    /**
156
     * Invokes current field access with all interceptors
157
     *
158
     * @param object $instance Instance of object
159
     * @param integer $accessType Type of access: READ or WRITE
160
     * @param mixed $originalValue Original value of property
161
     * @param mixed $newValue New value to set
162
     *
163
     * @return mixed
164
     */
165
    final public function &__invoke(object $instance, int $accessType, &$originalValue, $newValue = NAN)
166
    {
167
        if ($this->level > 0) {
168
            $this->stackFrames[] = [$this->instance, $this->accessType, &$this->value, &$this->newValue];
169
        }
170
171
        try {
172
            ++$this->level;
173
174
            $this->current    = 0;
175
            $this->instance   = $instance;
176
            $this->accessType = $accessType;
177
            $this->value      = &$originalValue;
178
            $this->newValue   = $newValue;
179
180
            $this->proceed();
181
182
            if ($accessType === self::READ) {
183
                $result = &$this->value;
184
            } else {
185
                $result = &$this->newValue;
186
            }
187
188
            return $result;
189
        } finally {
190
            --$this->level;
191
192
            if ($this->level > 0) {
193
                [$this->instance, $this->accessType, $this->value, $this->newValue] = array_pop($this->stackFrames);
194
            }
195
        }
196
    }
197
198
    /**
199
     * Returns the object that holds the current joinpoint's static
200
     * part.
201
     *
202
     * @return object|null the object (can be null if the accessible object is
203
     * static).
204
     */
205
    final public function getThis()
206
    {
207
        return $this->instance;
208
    }
209
210
    /**
211
     * Returns the static part of this joinpoint.
212
     *
213
     * @return object
214
     */
215 1
    final public function getStaticPart()
216
    {
217 1
        return $this->getField();
218
    }
219
220
    /**
221
     * Returns a friendly description of current joinpoint
222
     */
223
    final public function __toString(): string
224
    {
225
        return sprintf(
226
            '%s(%s->%s)',
227
            $this->accessType === self::READ ? 'get' : 'set',
228
            get_class($this->instance),
229
            $this->reflectionProperty->name
230
        );
231
    }
232
}
233