| Total Complexity | 83 |
| Total Lines | 349 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LazyProxyTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LazyProxyTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | trait LazyProxyTrait |
||
| 22 | { |
||
| 23 | use LazyObjectTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Creates a lazy-loading virtual proxy. |
||
| 27 | * |
||
| 28 | * @param \Closure():object $initializer Returns the proxied object |
||
| 29 | * @param static|null $instance |
||
| 30 | */ |
||
| 31 | public static function createLazyProxy(\Closure $initializer, ?object $instance = null): static |
||
| 32 | { |
||
| 33 | if (self::class !== $class = $instance ? $instance::class : static::class) { |
||
| 34 | $skippedProperties = ["\0".self::class."\0lazyObjectState" => true]; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (!isset(Registry::$defaultProperties[$class])) { |
||
| 38 | Registry::$classReflectors[$class] ??= new \ReflectionClass($class); |
||
| 39 | $instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor(); |
||
| 40 | Registry::$defaultProperties[$class] ??= (array) $instance; |
||
| 41 | Registry::$classResetters[$class] ??= Registry::getClassResetters($class); |
||
| 42 | |||
| 43 | if (self::class === $class && \defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) { |
||
| 44 | Hydrator::$propertyScopes[$class] ??= $class::LAZY_OBJECT_PROPERTY_SCOPES; |
||
| 45 | } |
||
| 46 | } else { |
||
| 47 | $instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor(); |
||
| 48 | } |
||
| 49 | |||
| 50 | if (isset($instance->lazyObjectState)) { |
||
| 51 | $instance->lazyObjectState->initializer = $initializer; |
||
| 52 | unset($instance->lazyObjectState->realInstance); |
||
| 53 | |||
| 54 | return $instance; |
||
| 55 | } |
||
| 56 | |||
| 57 | $instance->lazyObjectState = new LazyObjectState($initializer); |
||
| 58 | |||
| 59 | foreach (Registry::$classResetters[$class] as $reset) { |
||
| 60 | $reset($instance, $skippedProperties ??= []); |
||
| 61 | } |
||
| 62 | |||
| 63 | return $instance; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns whether the object is initialized. |
||
| 68 | * |
||
| 69 | * @param bool $partial Whether partially initialized objects should be considered as initialized |
||
| 70 | */ |
||
| 71 | #[Ignore] |
||
| 72 | public function isLazyObjectInitialized(bool $partial = false): bool |
||
| 73 | { |
||
| 74 | return !isset($this->lazyObjectState) || isset($this->lazyObjectState->realInstance) || Registry::$noInitializerState === $this->lazyObjectState->initializer; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Forces initialization of a lazy object and returns it. |
||
| 79 | */ |
||
| 80 | public function initializeLazyObject(): parent |
||
| 81 | { |
||
| 82 | if ($state = $this->lazyObjectState ?? null) { |
||
| 83 | return $state->realInstance ??= ($state->initializer)(); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return bool Returns false when the object cannot be reset, ie when it's not a lazy object |
||
| 91 | */ |
||
| 92 | public function resetLazyObject(): bool |
||
| 93 | { |
||
| 94 | if (!isset($this->lazyObjectState) || Registry::$noInitializerState === $this->lazyObjectState->initializer) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | unset($this->lazyObjectState->realInstance); |
||
| 99 | |||
| 100 | return true; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function &__get($name): mixed |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | public function __set($name, $value): void |
||
| 180 | { |
||
| 181 | $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class); |
||
| 182 | $scope = null; |
||
| 183 | $instance = $this; |
||
| 184 | |||
| 185 | if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) { |
||
| 186 | $scope = Registry::getScopeForWrite($propertyScopes, $class, $name, $access >> 2); |
||
| 187 | |||
| 188 | if ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"])) { |
||
| 189 | if ($state = $this->lazyObjectState ?? null) { |
||
| 190 | $instance = $state->realInstance ??= ($state->initializer)(); |
||
| 191 | } |
||
| 192 | goto set_in_scope; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | if ($state = $this->lazyObjectState ?? null) { |
||
| 197 | $instance = $state->realInstance ??= ($state->initializer)(); |
||
| 198 | } elseif ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['set']) { |
||
| 199 | parent::__set($name, $value); |
||
| 200 | |||
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | set_in_scope: |
||
| 205 | |||
| 206 | if (null === $scope) { |
||
| 207 | $instance->$name = $value; |
||
| 208 | } else { |
||
| 209 | $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope); |
||
| 210 | $accessor['set']($instance, $name, $value); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | public function __isset($name): bool |
||
| 215 | { |
||
| 216 | $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class); |
||
| 217 | $scope = null; |
||
| 218 | $instance = $this; |
||
| 219 | |||
| 220 | if ([$class] = $propertyScopes[$name] ?? null) { |
||
| 221 | $scope = Registry::getScopeForRead($propertyScopes, $class, $name); |
||
| 222 | |||
| 223 | if (null === $scope || isset($propertyScopes["\0$scope\0$name"])) { |
||
| 224 | if ($state = $this->lazyObjectState ?? null) { |
||
| 225 | $instance = $state->realInstance ??= ($state->initializer)(); |
||
| 226 | } |
||
| 227 | goto isset_in_scope; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($state = $this->lazyObjectState ?? null) { |
||
| 232 | $instance = $state->realInstance ??= ($state->initializer)(); |
||
| 233 | } elseif ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['isset']) { |
||
| 234 | return parent::__isset($name); |
||
| 235 | } |
||
| 236 | |||
| 237 | isset_in_scope: |
||
| 238 | |||
| 239 | if (null === $scope) { |
||
| 240 | return isset($instance->$name); |
||
| 241 | } |
||
| 242 | $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope); |
||
| 243 | |||
| 244 | return $accessor['isset']($instance, $name); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function __unset($name): void |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | public function __clone(): void |
||
| 283 | { |
||
| 284 | if (!isset($this->lazyObjectState)) { |
||
| 285 | if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['clone']) { |
||
| 286 | parent::__clone(); |
||
| 287 | } |
||
| 288 | |||
| 289 | return; |
||
| 290 | } |
||
| 291 | |||
| 292 | $this->lazyObjectState = clone $this->lazyObjectState; |
||
| 293 | |||
| 294 | if (isset($this->lazyObjectState->realInstance)) { |
||
| 295 | $this->lazyObjectState->realInstance = clone $this->lazyObjectState->realInstance; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | public function __serialize(): array |
||
| 300 | { |
||
| 301 | $class = self::class; |
||
| 302 | $state = $this->lazyObjectState ?? null; |
||
| 303 | |||
| 304 | if (!$state && (Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['serialize']) { |
||
| 305 | $properties = parent::__serialize(); |
||
| 306 | } else { |
||
| 307 | $properties = (array) $this; |
||
| 308 | |||
| 309 | if ($state) { |
||
| 310 | unset($properties["\0$class\0lazyObjectState"]); |
||
| 311 | $properties["\0$class\0lazyObjectReal"] = $state->realInstance ??= ($state->initializer)(); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | if ($state || Registry::$parentMethods[$class]['serialize'] || !Registry::$parentMethods[$class]['sleep']) { |
||
| 316 | return $properties; |
||
| 317 | } |
||
| 318 | |||
| 319 | $scope = get_parent_class($class); |
||
| 320 | $data = []; |
||
| 321 | |||
| 322 | foreach (parent::__sleep() as $name) { |
||
| 323 | $value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$class\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null; |
||
| 324 | |||
| 325 | if (null === $k) { |
||
| 326 | trigger_error(\sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $name), \E_USER_NOTICE); |
||
| 327 | } else { |
||
| 328 | $data[$k] = $value; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | return $data; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function __unserialize(array $data): void |
||
| 336 | { |
||
| 337 | $class = self::class; |
||
| 338 | |||
| 339 | if ($instance = $data["\0$class\0lazyObjectReal"] ?? null) { |
||
| 340 | unset($data["\0$class\0lazyObjectReal"]); |
||
| 341 | |||
| 342 | foreach (Registry::$classResetters[$class] ??= Registry::getClassResetters($class) as $reset) { |
||
| 343 | $reset($this, $data); |
||
| 344 | } |
||
| 345 | |||
| 346 | if ($data) { |
||
| 347 | PublicHydrator::hydrate($this, $data); |
||
| 348 | } |
||
| 349 | $this->lazyObjectState = new LazyObjectState(Registry::$noInitializerState ??= static fn () => throw new \LogicException('Lazy proxy has no initializer.')); |
||
| 350 | $this->lazyObjectState->realInstance = $instance; |
||
| 351 | } elseif ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['unserialize']) { |
||
| 352 | parent::__unserialize($data); |
||
| 353 | } else { |
||
| 354 | PublicHydrator::hydrate($this, $data); |
||
| 355 | |||
| 356 | if (Registry::$parentMethods[$class]['wakeup']) { |
||
| 357 | parent::__wakeup(); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | public function __destruct() |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: