Skip to content

Commit 7d6bbec

Browse files
committed
Simplify property memoization for Flyweight pattern by replacing it with ??=
1 parent 888056e commit 7d6bbec

16 files changed

+109
-198
lines changed

src/Analyser/Ignore/IgnoreLexer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ final class IgnoreLexer
4141
*/
4242
public function tokenize(string $input): array
4343
{
44-
if ($this->regexp === null) {
45-
$this->regexp = $this->generateRegexp();
46-
}
44+
$this->regexp ??= $this->generateRegexp();
4745

4846
$matches = Strings::matchAll($input, $this->regexp, PREG_SET_ORDER);
4947

src/DependencyInjection/Type/LazyDynamicReturnTypeExtensionRegistryProvider.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ public function __construct(private Container $container)
2020

2121
public function getRegistry(): DynamicReturnTypeExtensionRegistry
2222
{
23-
if ($this->registry === null) {
24-
$this->registry = new DynamicReturnTypeExtensionRegistry(
25-
$this->container->getByType(ReflectionProvider::class),
26-
$this->container->getServicesByTag(BrokerFactory::DYNAMIC_METHOD_RETURN_TYPE_EXTENSION_TAG),
27-
$this->container->getServicesByTag(BrokerFactory::DYNAMIC_STATIC_METHOD_RETURN_TYPE_EXTENSION_TAG),
28-
$this->container->getServicesByTag(BrokerFactory::DYNAMIC_FUNCTION_RETURN_TYPE_EXTENSION_TAG),
29-
);
30-
}
31-
32-
return $this->registry;
23+
return $this->registry ??= new DynamicReturnTypeExtensionRegistry(
24+
$this->container->getByType(ReflectionProvider::class),
25+
$this->container->getServicesByTag(BrokerFactory::DYNAMIC_METHOD_RETURN_TYPE_EXTENSION_TAG),
26+
$this->container->getServicesByTag(BrokerFactory::DYNAMIC_STATIC_METHOD_RETURN_TYPE_EXTENSION_TAG),
27+
$this->container->getServicesByTag(BrokerFactory::DYNAMIC_FUNCTION_RETURN_TYPE_EXTENSION_TAG),
28+
);
3329
}
3430

3531
}

src/DependencyInjection/Type/LazyExpressionTypeResolverExtensionRegistryProvider.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ public function __construct(private Container $container)
1919

2020
public function getRegistry(): ExpressionTypeResolverExtensionRegistry
2121
{
22-
if ($this->registry === null) {
23-
$this->registry = new ExpressionTypeResolverExtensionRegistry(
24-
$this->container->getServicesByTag(BrokerFactory::EXPRESSION_TYPE_RESOLVER_EXTENSION_TAG),
25-
);
26-
}
27-
28-
return $this->registry;
22+
return $this->registry ??= new ExpressionTypeResolverExtensionRegistry(
23+
$this->container->getServicesByTag(BrokerFactory::EXPRESSION_TYPE_RESOLVER_EXTENSION_TAG),
24+
);
2925
}
3026

3127
}

src/DependencyInjection/Type/LazyOperatorTypeSpecifyingExtensionRegistryProvider.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ public function __construct(private Container $container)
1919

2020
public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry
2121
{
22-
if ($this->registry === null) {
23-
$this->registry = new OperatorTypeSpecifyingExtensionRegistry(
24-
$this->container->getServicesByTag(BrokerFactory::OPERATOR_TYPE_SPECIFYING_EXTENSION_TAG),
25-
);
26-
}
27-
28-
return $this->registry;
22+
return $this->registry ??= new OperatorTypeSpecifyingExtensionRegistry(
23+
$this->container->getServicesByTag(BrokerFactory::OPERATOR_TYPE_SPECIFYING_EXTENSION_TAG),
24+
);
2925
}
3026

3127
}

src/PhpDoc/LazyTypeNodeResolverExtensionRegistryProvider.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ public function __construct(private Container $container)
1717

1818
public function getRegistry(): TypeNodeResolverExtensionRegistry
1919
{
20-
if ($this->registry === null) {
21-
$this->registry = new TypeNodeResolverExtensionAwareRegistry(
22-
$this->container->getByType(TypeNodeResolver::class),
23-
$this->container->getServicesByTag(TypeNodeResolverExtension::EXTENSION_TAG),
24-
);
25-
}
26-
27-
return $this->registry;
20+
return $this->registry ??= new TypeNodeResolverExtensionAwareRegistry(
21+
$this->container->getByType(TypeNodeResolver::class),
22+
$this->container->getServicesByTag(TypeNodeResolverExtension::EXTENSION_TAG),
23+
);
2824
}
2925

3026
}

src/PhpDoc/ResolvedPhpDocBlock.php

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -733,65 +733,47 @@ public function getDeprecatedTag(): ?DeprecatedTag
733733

734734
public function isDeprecated(): bool
735735
{
736-
if ($this->isDeprecated === null) {
737-
$this->isDeprecated = $this->phpDocNodeResolver->resolveIsDeprecated(
738-
$this->phpDocNode,
739-
);
740-
}
741-
return $this->isDeprecated;
736+
return $this->isDeprecated ??= $this->phpDocNodeResolver->resolveIsDeprecated(
737+
$this->phpDocNode,
738+
);
742739
}
743740

744741
/**
745742
* @internal
746743
*/
747744
public function isNotDeprecated(): bool
748745
{
749-
if ($this->isNotDeprecated === null) {
750-
$this->isNotDeprecated = $this->phpDocNodeResolver->resolveIsNotDeprecated(
751-
$this->phpDocNode,
752-
);
753-
}
754-
return $this->isNotDeprecated;
746+
return $this->isNotDeprecated ??= $this->phpDocNodeResolver->resolveIsNotDeprecated(
747+
$this->phpDocNode,
748+
);
755749
}
756750

757751
public function isInternal(): bool
758752
{
759-
if ($this->isInternal === null) {
760-
$this->isInternal = $this->phpDocNodeResolver->resolveIsInternal(
761-
$this->phpDocNode,
762-
);
763-
}
764-
return $this->isInternal;
753+
return $this->isInternal ??= $this->phpDocNodeResolver->resolveIsInternal(
754+
$this->phpDocNode,
755+
);
765756
}
766757

767758
public function isFinal(): bool
768759
{
769-
if ($this->isFinal === null) {
770-
$this->isFinal = $this->phpDocNodeResolver->resolveIsFinal(
771-
$this->phpDocNode,
772-
);
773-
}
774-
return $this->isFinal;
760+
return $this->isFinal ??= $this->phpDocNodeResolver->resolveIsFinal(
761+
$this->phpDocNode,
762+
);
775763
}
776764

777765
public function hasConsistentConstructor(): bool
778766
{
779-
if ($this->hasConsistentConstructor === null) {
780-
$this->hasConsistentConstructor = $this->phpDocNodeResolver->resolveHasConsistentConstructor(
781-
$this->phpDocNode,
782-
);
783-
}
784-
return $this->hasConsistentConstructor;
767+
return $this->hasConsistentConstructor ??= $this->phpDocNodeResolver->resolveHasConsistentConstructor(
768+
$this->phpDocNode,
769+
);
785770
}
786771

787772
public function acceptsNamedArguments(): bool
788773
{
789-
if ($this->acceptsNamedArguments === null) {
790-
$this->acceptsNamedArguments = $this->phpDocNodeResolver->resolveAcceptsNamedArguments(
791-
$this->phpDocNode,
792-
);
793-
}
794-
return $this->acceptsNamedArguments;
774+
return $this->acceptsNamedArguments ??= $this->phpDocNodeResolver->resolveAcceptsNamedArguments(
775+
$this->phpDocNode,
776+
);
795777
}
796778

797779
public function getTemplateTypeMap(): TemplateTypeMap
@@ -826,33 +808,23 @@ public function isPure(): ?bool
826808

827809
public function isReadOnly(): bool
828810
{
829-
if ($this->isReadOnly === null) {
830-
$this->isReadOnly = $this->phpDocNodeResolver->resolveIsReadOnly(
831-
$this->phpDocNode,
832-
);
833-
}
834-
return $this->isReadOnly;
811+
return $this->isReadOnly ??= $this->phpDocNodeResolver->resolveIsReadOnly(
812+
$this->phpDocNode,
813+
);
835814
}
836815

837816
public function isImmutable(): bool
838817
{
839-
if ($this->isImmutable === null) {
840-
$this->isImmutable = $this->phpDocNodeResolver->resolveIsImmutable(
841-
$this->phpDocNode,
842-
);
843-
}
844-
return $this->isImmutable;
818+
return $this->isImmutable ??= $this->phpDocNodeResolver->resolveIsImmutable(
819+
$this->phpDocNode,
820+
);
845821
}
846822

847823
public function isAllowedPrivateMutation(): bool
848824
{
849-
if ($this->isAllowedPrivateMutation === null) {
850-
$this->isAllowedPrivateMutation = $this->phpDocNodeResolver->resolveAllowPrivateMutation(
851-
$this->phpDocNode,
852-
);
853-
}
854-
855-
return $this->isAllowedPrivateMutation;
825+
return $this->isAllowedPrivateMutation ??= $this->phpDocNodeResolver->resolveAllowPrivateMutation(
826+
$this->phpDocNode,
827+
);
856828
}
857829

858830
/**

src/Reflection/Annotations/AnnotationMethodReflection.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ public function getName(): string
6868

6969
public function getVariants(): array
7070
{
71-
if ($this->variants === null) {
72-
$this->variants = [
73-
new ExtendedFunctionVariant(
74-
$this->templateTypeMap,
75-
null,
76-
$this->parameters,
77-
$this->isVariadic,
78-
$this->returnType,
79-
$this->returnType,
80-
new MixedType(),
81-
),
82-
];
83-
}
84-
return $this->variants;
71+
return $this->variants ??= [
72+
new ExtendedFunctionVariant(
73+
$this->templateTypeMap,
74+
null,
75+
$this->parameters,
76+
$this->isVariadic,
77+
$this->returnType,
78+
$this->returnType,
79+
new MixedType(),
80+
),
81+
];
8582
}
8683

8784
public function getOnlyVariant(): ExtendedParametersAcceptor

src/Reflection/Php/PhpFunctionFromParserNodeReflection.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,17 @@ public function getName(): string
105105

106106
public function getVariants(): array
107107
{
108-
if ($this->variants === null) {
109-
$this->variants = [
110-
new ExtendedFunctionVariant(
111-
$this->getTemplateTypeMap(),
112-
$this->getResolvedTemplateTypeMap(),
113-
$this->getParameters(),
114-
$this->isVariadic(),
115-
$this->getReturnType(),
116-
$this->getPhpDocReturnType(),
117-
$this->getNativeReturnType(),
118-
),
119-
];
120-
}
121-
122-
return $this->variants;
108+
return $this->variants ??= [
109+
new ExtendedFunctionVariant(
110+
$this->getTemplateTypeMap(),
111+
$this->getResolvedTemplateTypeMap(),
112+
$this->getParameters(),
113+
$this->isVariadic(),
114+
$this->getReturnType(),
115+
$this->getPhpDocReturnType(),
116+
$this->getNativeReturnType(),
117+
),
118+
];
123119
}
124120

125121
public function getOnlyVariant(): ExtendedParametersAcceptor

src/Reflection/Php/PhpFunctionReflection.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,17 @@ public function getFileName(): ?string
9292

9393
public function getVariants(): array
9494
{
95-
if ($this->variants === null) {
96-
$this->variants = [
97-
new ExtendedFunctionVariant(
98-
$this->templateTypeMap,
99-
null,
100-
$this->getParameters(),
101-
$this->isVariadic(),
102-
$this->getReturnType(),
103-
$this->getPhpDocReturnType(),
104-
$this->getNativeReturnType(),
105-
),
106-
];
107-
}
108-
109-
return $this->variants;
95+
return $this->variants ??= [
96+
new ExtendedFunctionVariant(
97+
$this->templateTypeMap,
98+
null,
99+
$this->getParameters(),
100+
$this->isVariadic(),
101+
$this->getReturnType(),
102+
$this->getPhpDocReturnType(),
103+
$this->getNativeReturnType(),
104+
),
105+
];
110106
}
111107

112108
public function getOnlyVariant(): ExtendedParametersAcceptor

src/Reflection/Php/PhpMethodReflection.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,17 @@ private function getMethodNameWithCorrectCase(string $lowercaseMethodName, strin
198198
*/
199199
public function getVariants(): array
200200
{
201-
if ($this->variants === null) {
202-
$this->variants = [
203-
new ExtendedFunctionVariant(
204-
$this->templateTypeMap,
205-
null,
206-
$this->getParameters(),
207-
$this->isVariadic(),
208-
$this->getReturnType(),
209-
$this->getPhpDocReturnType(),
210-
$this->getNativeReturnType(),
211-
),
212-
];
213-
}
214-
215-
return $this->variants;
201+
return $this->variants ??= [
202+
new ExtendedFunctionVariant(
203+
$this->templateTypeMap,
204+
null,
205+
$this->getParameters(),
206+
$this->isVariadic(),
207+
$this->getReturnType(),
208+
$this->getPhpDocReturnType(),
209+
$this->getNativeReturnType(),
210+
),
211+
];
216212
}
217213

218214
public function getOnlyVariant(): ExtendedParametersAcceptor

src/Reflection/Php/PhpParameterReflection.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,11 @@ public function hasNativeType(): bool
104104

105105
public function getNativeType(): Type
106106
{
107-
if ($this->nativeType === null) {
108-
$this->nativeType = TypehintHelper::decideTypeFromReflection(
109-
$this->reflection->getType(),
110-
selfClass: $this->declaringClass,
111-
isVariadic: $this->isVariadic(),
112-
);
113-
}
114-
115-
return $this->nativeType;
107+
return $this->nativeType ??= TypehintHelper::decideTypeFromReflection(
108+
$this->reflection->getType(),
109+
selfClass: $this->declaringClass,
110+
isVariadic: $this->isVariadic(),
111+
);
116112
}
117113

118114
public function getDefaultValue(): ?Type

0 commit comments

Comments
 (0)