Skip to content

Commit e99305e

Browse files
committed
Add pureUnlessCallableIsImpureParameters
1 parent f262eaa commit e99305e

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

src/Analyser/MutatingScope.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,7 @@ public function enterTrait(ClassReflection $traitReflection): self
30883088
* @param Type[] $parameterOutTypes
30893089
* @param array<string, bool> $immediatelyInvokedCallableParameters
30903090
* @param array<string, Type> $phpDocClosureThisTypeParameters
3091+
* @param array<string, bool> $phpDocPureUnlessCallableIsImpureParameters
30913092
*/
30923093
public function enterClassMethod(
30933094
Node\Stmt\ClassMethod $classMethod,
@@ -3108,6 +3109,7 @@ public function enterClassMethod(
31083109
array $immediatelyInvokedCallableParameters = [],
31093110
array $phpDocClosureThisTypeParameters = [],
31103111
bool $isConstructor = false,
3112+
array $phpDocPureUnlessCallableIsImpureParameters = [],
31113113
): self
31123114
{
31133115
if (!$this->isInClass()) {
@@ -3142,6 +3144,7 @@ public function enterClassMethod(
31423144
array_map(fn (Type $type): Type => $this->transformStaticType(TemplateTypeHelper::toArgument($type)), $phpDocClosureThisTypeParameters),
31433145
$isConstructor,
31443146
$this->attributeReflectionFactory->fromAttrGroups($classMethod->attrGroups, InitializerExprContext::fromStubParameter($this->getClassReflection()->getName(), $this->getFile(), $classMethod)),
3147+
$phpDocPureUnlessCallableIsImpureParameters,
31453148
),
31463149
!$classMethod->isStatic(),
31473150
);
@@ -3319,6 +3322,7 @@ private function getParameterAttributes(ClassMethod|Function_|PropertyHook $func
33193322
* @param Type[] $parameterOutTypes
33203323
* @param array<string, bool> $immediatelyInvokedCallableParameters
33213324
* @param array<string, Type> $phpDocClosureThisTypeParameters
3325+
* @param array<string, bool> $pureUnlessCallableIsImpureParameters
33223326
*/
33233327
public function enterFunction(
33243328
Node\Stmt\Function_ $function,
@@ -3336,6 +3340,7 @@ public function enterFunction(
33363340
array $parameterOutTypes = [],
33373341
array $immediatelyInvokedCallableParameters = [],
33383342
array $phpDocClosureThisTypeParameters = [],
3343+
array $pureUnlessCallableIsImpureParameters = [],
33393344
): self
33403345
{
33413346
return $this->enterFunctionLike(
@@ -3361,6 +3366,7 @@ public function enterFunction(
33613366
$immediatelyInvokedCallableParameters,
33623367
$phpDocClosureThisTypeParameters,
33633368
$this->attributeReflectionFactory->fromAttrGroups($function->attrGroups, InitializerExprContext::fromStubParameter(null, $this->getFile(), $function)),
3369+
$pureUnlessCallableIsImpureParameters,
33643370
),
33653371
false,
33663372
);

src/Analyser/NodeScopeResolver.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ private function processStmtNode(
651651
$throwPoints = [];
652652
$impurePoints = [];
653653
$this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback);
654-
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes] = $this->getPhpDocs($scope, $stmt);
654+
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $pureUnlessCallableIsImpureParameters] = $this->getPhpDocs($scope, $stmt);
655655

656656
foreach ($stmt->params as $param) {
657657
$this->processParamNode($stmt, $param, $scope, $nodeCallback);
@@ -6664,7 +6664,7 @@ private function processNodesForCalledMethod($node, string $fileName, MethodRefl
66646664
}
66656665

66666666
/**
6667-
* @return array{TemplateTypeMap, array<string, Type>, array<string, bool>, array<string, Type>, ?Type, ?Type, ?string, bool, bool, bool, bool|null, bool, bool, string|null, Assertions, ?Type, array<string, Type>, array<(string|int), VarTag>, bool}
6667+
* @return array{TemplateTypeMap, array<string, Type>, array<string, bool>, array<string, Type>, ?Type, ?Type, ?string, bool, bool, bool, bool|null, bool, bool, string|null, Assertions, ?Type, array<string, Type>, array<(string|int), VarTag>, bool, array<string, bool>}
66686668
*/
66696669
public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $node): array
66706670
{
@@ -6694,6 +6694,7 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
66946694
$resolvedPhpDoc = null;
66956695
$functionName = null;
66966696
$phpDocParameterOutTypes = [];
6697+
$phpDocPureUnlessCallableIsImpureParameters = [];
66976698

66986699
if ($node instanceof Node\Stmt\ClassMethod) {
66996700
if (!$scope->isInClass()) {
@@ -6821,9 +6822,10 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
68216822
$asserts = Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc);
68226823
$selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null;
68236824
$varTags = $resolvedPhpDoc->getVarTags();
6825+
$phpDocPureUnlessCallableIsImpureParameters = $resolvedPhpDoc->getParamsPureUnlessCallableIsImpure();
68246826
}
68256827

6826-
return [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $docComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation];
6828+
return [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $docComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation, $phpDocPureUnlessCallableIsImpureParameters];
68276829
}
68286830

68296831
private function transformStaticType(ClassReflection $declaringClass, Type $type): Type

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,17 @@ public function resolveParamPureUnlessCallableIsImpure(PhpDocNode $phpDocNode):
428428
$parameters = [];
429429
// TODO: implement phpstan/phpdoc-parser
430430
foreach ($phpDocNode->getTagsByName('@pure-unless-callable-impure') as $tag) {
431-
$value = preg_split('/\s/u', (string)$tag->value)[0] ?? null;
432-
if ($value !== null && str_starts_with($value, '$')) {
433-
$parameters[substr($value, 1)] = true;
431+
$value = preg_split('/\s/u', (string) $tag->value)[0] ?? null;
432+
if ($value === null || !str_starts_with($value, '$')) {
433+
continue;
434434
}
435+
436+
$parameters[substr($value, 1)] = true;
435437
}
436438

437439
return $parameters;
438440
}
439441

440-
441442
/**
442443
* @return array<string, ParamClosureThisTag>
443444
*/

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ private function inferAndCachePropertyTypes(
10711071
$classScope = $classScope->enterNamespace($namespace);
10721072
}
10731073
$classScope = $classScope->enterClass($declaringClass);
1074-
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes] = $this->nodeScopeResolver->getPhpDocs($classScope, $methodNode);
1074+
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation, $phpDocPureUnlessCallableIsImpureParameters] = $this->nodeScopeResolver->getPhpDocs($classScope, $methodNode);
10751075
$methodScope = $classScope->enterClassMethod(
10761076
$methodNode,
10771077
$templateTypeMap,
@@ -1090,6 +1090,7 @@ private function inferAndCachePropertyTypes(
10901090
$phpDocParameterOutTypes,
10911091
$phpDocImmediatelyInvokedCallableParameters,
10921092
$phpDocClosureThisTypeParameters,
1093+
$phpDocPureUnlessCallableIsImpureParameters,
10931094
);
10941095

10951096
$propertyTypes = [];

src/Reflection/Php/PhpFunctionFromParserNodeReflection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PhpFunctionFromParserNodeReflection implements FunctionReflection, Extende
4747
* @param array<string, bool> $immediatelyInvokedCallableParameters
4848
* @param array<string, Type> $phpDocClosureThisTypeParameters
4949
* @param list<AttributeReflection> $attributes
50+
* @param array<string, bool> $pureUnlessCallableIsImpureParameters
5051
*/
5152
public function __construct(
5253
FunctionLike $functionLike,
@@ -70,6 +71,7 @@ public function __construct(
7071
private array $immediatelyInvokedCallableParameters,
7172
private array $phpDocClosureThisTypeParameters,
7273
private array $attributes,
74+
private array $pureUnlessCallableIsImpureParameters,
7375
)
7476
{
7577
$this->functionLike = $functionLike;

src/Reflection/Php/PhpMethodFromParserNodeReflection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function __construct(
6868
array $phpDocClosureThisTypeParameters,
6969
private bool $isConstructor,
7070
array $attributes,
71+
array $pureUnlessCallableIsImpureParameters,
7172
)
7273
{
7374
if ($this->classMethod instanceof Node\PropertyHook) {
@@ -136,6 +137,7 @@ public function __construct(
136137
$immediatelyInvokedCallableParameters,
137138
$phpDocClosureThisTypeParameters,
138139
$attributes,
140+
$pureUnlessCallableIsImpureParameters,
139141
);
140142
}
141143

0 commit comments

Comments
 (0)