Skip to content

Commit 3c0ed74

Browse files
authored
Introduce ClosureType::isStaticClosure() (#5699)
1 parent f830e4f commit 3c0ed74

26 files changed

Lines changed: 522 additions & 39 deletions

‎src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,15 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
227227
usedVariables: $cachedClosureData['usedVariables'],
228228
acceptsNamedArguments: TrinaryLogic::createYes(),
229229
mustUseReturnValue: $mustUseReturnValue,
230+
isStatic: TrinaryLogic::createFromBoolean($expr->static),
230231
);
231232
}
232233
if (self::$resolveClosureTypeDepth >= 2) {
233234
return new ClosureType(
234235
$parameters,
235236
$scope->getFunctionType($expr->returnType, false, false),
236237
$isVariadic,
238+
isStatic: TrinaryLogic::createFromBoolean($expr->static),
237239
);
238240
}
239241

@@ -453,6 +455,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
453455
usedVariables: $usedVariables,
454456
acceptsNamedArguments: TrinaryLogic::createYes(),
455457
mustUseReturnValue: $mustUseReturnValue,
458+
isStatic: TrinaryLogic::createFromBoolean($expr->static),
456459
);
457460
}
458461

‎src/PhpDoc/TypeNodeResolver.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
421421
case 'pure-closure':
422422
return ClosureType::createPure();
423423

424+
case 'static-closure':
425+
return new ClosureType(isStatic: TrinaryLogic::createYes());
426+
427+
case 'static-pure-closure':
428+
return new ClosureType(impurePoints: [], isStatic: TrinaryLogic::createYes());
429+
424430
case 'resource':
425431
$type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope);
426432

@@ -1083,7 +1089,7 @@ function (CallableTypeParameterNode $parameterNode) use ($nameScope, &$isVariadi
10831089
),
10841090
]);
10851091
} elseif ($mainType instanceof ClosureType) {
1086-
$closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, impurePoints: $mainType->getImpurePoints(), invalidateExpressions: $mainType->getInvalidateExpressions(), usedVariables: $mainType->getUsedVariables(), acceptsNamedArguments: $mainType->acceptsNamedArguments(), mustUseReturnValue: $mainType->mustUseReturnValue());
1092+
$closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, impurePoints: $mainType->getImpurePoints(), invalidateExpressions: $mainType->getInvalidateExpressions(), usedVariables: $mainType->getUsedVariables(), acceptsNamedArguments: $mainType->acceptsNamedArguments(), mustUseReturnValue: $mainType->mustUseReturnValue(), isStatic: $mainType->isStaticClosure());
10871093
if ($closure->isPure()->yes() && $returnType->isVoid()->yes()) {
10881094
return new ErrorType();
10891095
}

‎src/Reflection/Callables/CallableParametersAcceptor.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ public function mustUseReturnValue(): TrinaryLogic;
6060

6161
public function getAsserts(): Assertions;
6262

63+
public function isStaticClosure(): TrinaryLogic;
64+
6365
}

‎src/Reflection/Callables/FunctionCallableVariant.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,9 @@ public function getAsserts(): Assertions
179179
return $this->function->getAsserts();
180180
}
181181

182+
public function isStaticClosure(): TrinaryLogic
183+
{
184+
return TrinaryLogic::createNo();
185+
}
186+
182187
}

‎src/Reflection/ExtendedCallableFunctionVariant.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct(
3838
private TrinaryLogic $acceptsNamedArguments,
3939
private TrinaryLogic $mustUseReturnValue,
4040
private ?Assertions $assertions = null,
41+
private ?TrinaryLogic $isStatic = null,
4142
)
4243
{
4344
parent::__construct(
@@ -92,4 +93,9 @@ public function getAsserts(): Assertions
9293
return $this->assertions ?? Assertions::createEmpty();
9394
}
9495

96+
public function isStaticClosure(): TrinaryLogic
97+
{
98+
return $this->isStatic ?? TrinaryLogic::createMaybe();
99+
}
100+
95101
}

‎src/Reflection/GenericParametersAcceptorResolver.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public static function resolve(array $argTypes, ParametersAcceptor $parametersAc
135135
$originalParametersAcceptor->acceptsNamedArguments(),
136136
$originalParametersAcceptor->mustUseReturnValue(),
137137
$originalParametersAcceptor->getAsserts(),
138+
$originalParametersAcceptor->isStaticClosure(),
138139
);
139140
}
140141

‎src/Reflection/InaccessibleMethod.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,9 @@ public function getAsserts(): Assertions
9898
return Assertions::createEmpty();
9999
}
100100

101+
public function isStaticClosure(): TrinaryLogic
102+
{
103+
return TrinaryLogic::createNo();
104+
}
105+
101106
}

‎src/Reflection/InitializerExprTypeResolver.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
263263
TemplateTypeMap::createEmpty(),
264264
TemplateTypeVarianceMap::createEmpty(),
265265
acceptsNamedArguments: TrinaryLogic::createYes(),
266+
isStatic: TrinaryLogic::createYes(),
266267
);
267268
}
268269
if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) {
@@ -928,11 +929,13 @@ public function createFirstClassCallable(
928929
$impurePoints = [];
929930
$acceptsNamedArguments = TrinaryLogic::createYes();
930931
$mustUseReturnValue = TrinaryLogic::createMaybe();
932+
$isStaticClosure = TrinaryLogic::createMaybe();
931933
if ($variant instanceof CallableParametersAcceptor) {
932934
$throwPoints = $variant->getThrowPoints();
933935
$impurePoints = $variant->getImpurePoints();
934936
$acceptsNamedArguments = $variant->acceptsNamedArguments();
935937
$mustUseReturnValue = $variant->mustUseReturnValue();
938+
$isStaticClosure = $variant->isStaticClosure();
936939
} elseif ($function !== null) {
937940
$returnTypeForThrow = $variant->getReturnType();
938941
$throwType = $function->getThrowType();
@@ -976,6 +979,7 @@ public function createFirstClassCallable(
976979
acceptsNamedArguments: $acceptsNamedArguments,
977980
mustUseReturnValue: $mustUseReturnValue,
978981
assertions: $assertions,
982+
isStatic: $isStaticClosure,
979983
);
980984
}
981985

‎src/Reflection/ParametersAcceptorSelector.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
730730
$usedVariables = [];
731731
$acceptsNamedArguments = TrinaryLogic::createNo();
732732
$mustUseReturnValue = TrinaryLogic::createMaybe();
733+
$isStaticClosure = TrinaryLogic::createMaybe();
733734

734735
foreach ($acceptors as $acceptor) {
735736
$returnTypes[] = $acceptor->getReturnType();
@@ -747,6 +748,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
747748
$usedVariables = array_merge($usedVariables, $acceptor->getUsedVariables());
748749
$acceptsNamedArguments = $acceptsNamedArguments->or($acceptor->acceptsNamedArguments());
749750
$mustUseReturnValue = $mustUseReturnValue->or($acceptor->mustUseReturnValue());
751+
$isStaticClosure = $isStaticClosure->or($acceptor->isStaticClosure());
750752
}
751753
$isVariadic = $isVariadic || $acceptor->isVariadic();
752754

@@ -864,6 +866,7 @@ public static function combineAcceptors(array $acceptors): ExtendedParametersAcc
864866
$usedVariables,
865867
$acceptsNamedArguments,
866868
$mustUseReturnValue,
869+
isStatic: $isStaticClosure,
867870
);
868871
}
869872

@@ -902,6 +905,7 @@ private static function wrapAcceptor(ParametersAcceptor $acceptor): ExtendedPara
902905
$acceptor->acceptsNamedArguments(),
903906
$acceptor->mustUseReturnValue(),
904907
$acceptor->getAsserts(),
908+
$acceptor->isStaticClosure(),
905909
);
906910
}
907911

‎src/Reflection/ResolvedFunctionVariantWithCallable.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function __construct(
3030
private TrinaryLogic $acceptsNamedArguments,
3131
private TrinaryLogic $mustUseReturnValue,
3232
private ?Assertions $assertions = null,
33+
private ?TrinaryLogic $isStatic = null,
3334
)
3435
{
3536
}
@@ -124,4 +125,9 @@ public function getAsserts(): Assertions
124125
return $this->assertions ?? Assertions::createEmpty();
125126
}
126127

128+
public function isStaticClosure(): TrinaryLogic
129+
{
130+
return $this->isStatic ?? TrinaryLogic::createMaybe();
131+
}
132+
127133
}

0 commit comments

Comments
 (0)