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