Skip to content

Commit 2154e33

Browse files
committed
Simplify property memoization for Flyweight pattern by replacing it with ??=
1 parent ef19d69 commit 2154e33

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/Reflection/Php/PhpMethodReflection.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,16 @@ public function getNamedArgumentsVariants(): ?array
226226
*/
227227
private function getParameters(): array
228228
{
229-
if ($this->parameters === null) {
230-
$this->parameters = array_map(fn (ReflectionParameter $reflection): PhpParameterReflection => new PhpParameterReflection(
231-
$this->initializerExprTypeResolver,
232-
$reflection,
233-
$this->phpDocParameterTypes[$reflection->getName()] ?? null,
234-
$this->getDeclaringClass(),
235-
$this->phpDocParameterOutTypes[$reflection->getName()] ?? null,
236-
$this->immediatelyInvokedCallableParameters[$reflection->getName()] ?? TrinaryLogic::createMaybe(),
237-
$this->phpDocClosureThisTypeParameters[$reflection->getName()] ?? null,
238-
$this->attributeReflectionFactory->fromNativeReflection($reflection->getAttributes(), InitializerExprContext::fromReflectionParameter($reflection)),
239-
), $this->reflection->getParameters());
240-
}
241-
242-
return $this->parameters;
229+
return $this->parameters ??= array_map(fn (ReflectionParameter $reflection): PhpParameterReflection => new PhpParameterReflection(
230+
$this->initializerExprTypeResolver,
231+
$reflection,
232+
$this->phpDocParameterTypes[$reflection->getName()] ?? null,
233+
$this->getDeclaringClass(),
234+
$this->phpDocParameterOutTypes[$reflection->getName()] ?? null,
235+
$this->immediatelyInvokedCallableParameters[$reflection->getName()] ?? TrinaryLogic::createMaybe(),
236+
$this->phpDocClosureThisTypeParameters[$reflection->getName()] ?? null,
237+
$this->attributeReflectionFactory->fromNativeReflection($reflection->getAttributes(), InitializerExprContext::fromReflectionParameter($reflection)),
238+
), $this->reflection->getParameters());
243239
}
244240

245241
private function isVariadic(): bool
@@ -342,14 +338,10 @@ private function getPhpDocReturnType(): Type
342338

343339
private function getNativeReturnType(): Type
344340
{
345-
if ($this->nativeReturnType === null) {
346-
$this->nativeReturnType = TypehintHelper::decideTypeFromReflection(
347-
$this->reflection->getReturnType(),
348-
selfClass: $this->declaringClass,
349-
);
350-
}
351-
352-
return $this->nativeReturnType;
341+
return $this->nativeReturnType ??= TypehintHelper::decideTypeFromReflection(
342+
$this->reflection->getReturnType(),
343+
selfClass: $this->declaringClass,
344+
);
353345
}
354346

355347
public function getDeprecatedDescription(): ?string

src/Rules/Properties/LazyReadWritePropertiesExtensionProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ public function __construct(private Container $container)
1818

1919
public function getExtensions(): array
2020
{
21-
if ($this->extensions === null) {
22-
$this->extensions = $this->container->getServicesByTag(ReadWritePropertiesExtensionProvider::EXTENSION_TAG);
23-
}
24-
25-
return $this->extensions;
21+
return $this->extensions ??= $this->container->getServicesByTag(ReadWritePropertiesExtensionProvider::EXTENSION_TAG);
2622
}
2723

2824
}

0 commit comments

Comments
 (0)