Skip to content

Commit 1f0edd1

Browse files
committed
Wire @pure-unless-parameter-passed into the analyser
Thread the raw parameter flags through enterClassMethod()/enterFunction() and getPhpDocs(), and consult them in SimpleImpurePoint::resolvePureUnlessParameterPassedVerdict(): a call to a function flagged with @pure-unless-parameter-passed stays pure as long as none of those by-ref parameters received an argument.
1 parent 68fdb67 commit 1f0edd1

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,7 @@ public function enterTrait(ClassReflection $traitReflection): self
15561556
* @param array<string, bool> $immediatelyInvokedCallableParameters
15571557
* @param array<string, Type> $phpDocClosureThisTypeParameters
15581558
* @param array<string, bool> $phpDocPureUnlessCallableIsImpureParameters
1559+
* @param array<string, bool> $phpDocPureUnlessParameterPassedParameters
15591560
*/
15601561
public function enterClassMethod(
15611562
Node\Stmt\ClassMethod $classMethod,
@@ -1578,6 +1579,7 @@ public function enterClassMethod(
15781579
bool $isConstructor = false,
15791580
?ResolvedPhpDocBlock $resolvedPhpDocBlock = null,
15801581
array $phpDocPureUnlessCallableIsImpureParameters = [],
1582+
array $phpDocPureUnlessParameterPassedParameters = [],
15811583
): self
15821584
{
15831585
if (!$this->isInClass()) {
@@ -1614,6 +1616,7 @@ public function enterClassMethod(
16141616
$isConstructor,
16151617
$this->attributeReflectionFactory->fromAttrGroups($classMethod->attrGroups, InitializerExprContext::fromStubParameter($this->getClassReflection()->getName(), $this->getFile(), $classMethod)),
16161618
$phpDocPureUnlessCallableIsImpureParameters,
1619+
$phpDocPureUnlessParameterPassedParameters,
16171620
),
16181621
!$classMethod->isStatic(),
16191622
);
@@ -1704,6 +1707,7 @@ public function enterPropertyHook(
17041707
false,
17051708
$this->attributeReflectionFactory->fromAttrGroups($hook->attrGroups, InitializerExprContext::fromStubParameter($this->getClassReflection()->getName(), $this->getFile(), $hook)),
17061709
[],
1710+
[],
17071711
),
17081712
true,
17091713
);
@@ -1781,6 +1785,7 @@ private function getParameterAttributes(ClassMethod|Function_|PropertyHook $func
17811785
* @param array<string, bool> $immediatelyInvokedCallableParameters
17821786
* @param array<string, Type> $phpDocClosureThisTypeParameters
17831787
* @param array<string, bool> $pureUnlessCallableIsImpureParameters
1788+
* @param array<string, bool> $pureUnlessParameterPassedParameters
17841789
*/
17851790
public function enterFunction(
17861791
Node\Stmt\Function_ $function,
@@ -1799,6 +1804,7 @@ public function enterFunction(
17991804
array $immediatelyInvokedCallableParameters = [],
18001805
array $phpDocClosureThisTypeParameters = [],
18011806
array $pureUnlessCallableIsImpureParameters = [],
1807+
array $pureUnlessParameterPassedParameters = [],
18021808
): self
18031809
{
18041810
return $this->enterFunctionLike(
@@ -1825,6 +1831,7 @@ public function enterFunction(
18251831
$phpDocClosureThisTypeParameters,
18261832
$this->attributeReflectionFactory->fromAttrGroups($function->attrGroups, InitializerExprContext::fromStubParameter(null, $this->getFile(), $function)),
18271833
$pureUnlessCallableIsImpureParameters,
1834+
$pureUnlessParameterPassedParameters,
18281835
),
18291836
false,
18301837
);

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ public function processStmtNode(
875875
$throwPoints = [];
876876
$impurePoints = [];
877877
$this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback);
878-
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes, , , , $pureUnlessCallableIsImpureParameters] = $this->getPhpDocs($scope, $stmt);
878+
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes, , , , $pureUnlessCallableIsImpureParameters, $pureUnlessParameterPassedParameters] = $this->getPhpDocs($scope, $stmt);
879879

880880
foreach ($stmt->params as $param) {
881881
$this->processParamNode($stmt, $param, $scope, $storage, $nodeCallback);
@@ -913,6 +913,7 @@ public function processStmtNode(
913913
$isConstructor,
914914
null,
915915
$pureUnlessCallableIsImpureParameters,
916+
$pureUnlessParameterPassedParameters,
916917
);
917918

918919
if (!$scope->isInClass()) {
@@ -4943,7 +4944,7 @@ private function processNodesForCalledMethod($node, ExpressionResultStorage $sto
49434944
}
49444945

49454946
/**
4946-
* @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, ?ResolvedPhpDocBlock, array<string, bool>}
4947+
* @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, ?ResolvedPhpDocBlock, array<string, bool>, array<string, bool>}
49474948
*/
49484949
public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $node): array
49494950
{
@@ -4974,6 +4975,7 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
49744975
$functionName = null;
49754976
$phpDocParameterOutTypes = [];
49764977
$phpDocPureUnlessCallableIsImpureParameters = [];
4978+
$phpDocPureUnlessParameterPassedParameters = [];
49774979

49784980
if ($node instanceof Node\Stmt\ClassMethod) {
49794981
if (!$scope->isInClass()) {
@@ -5108,6 +5110,7 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
51085110
$selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null;
51095111
$varTags = $resolvedPhpDoc->getVarTags();
51105112
$phpDocPureUnlessCallableIsImpureParameters = $resolvedPhpDoc->getParamsPureUnlessCallableIsImpure();
5113+
$phpDocPureUnlessParameterPassedParameters = $resolvedPhpDoc->getParamsPureUnlessParameterPassed();
51115114
}
51125115

51135116
if ($acceptsNamedArguments && $scope->isInClass()) {
@@ -5131,7 +5134,7 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
51315134
}
51325135
}
51335136

5134-
return [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $docComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation, $resolvedPhpDoc, $phpDocPureUnlessCallableIsImpureParameters];
5137+
return [$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, $isReadOnly, $docComment, $asserts, $selfOutType, $phpDocParameterOutTypes, $varTags, $isAllowedPrivateMutation, $resolvedPhpDoc, $phpDocPureUnlessCallableIsImpureParameters, $phpDocPureUnlessParameterPassedParameters];
51355138
}
51365139

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

‎src/Reflection/Callables/SimpleImpurePoint.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public static function createFromVariant(FunctionReflection|ExtendedMethodReflec
7474
}
7575
}
7676

77+
if (!$certain && $scope !== null && $variant !== null) {
78+
$passedVerdict = self::resolvePureUnlessParameterPassedVerdict($variant, $args);
79+
if ($passedVerdict !== null && $passedVerdict->yes()) {
80+
// None of the @pure-unless-parameter-passed by-ref parameters
81+
// received an argument, so the call is pure.
82+
return null;
83+
}
84+
}
85+
7786
if ($function instanceof FunctionReflection) {
7887
if (isset(self::SIDE_EFFECT_FLIP_PARAMETERS[$function->getName()]) && $scope !== null) {
7988
[
@@ -203,6 +212,59 @@ public static function resolvePureUnlessCallableIsImpureVerdict(ParametersAccept
203212
return $verdict;
204213
}
205214

215+
/**
216+
* Purity verdict for parameters flagged with @pure-unless-parameter-passed:
217+
* the call stays pure as long as none of those (by-ref out) parameters
218+
* received an argument. Returns Yes when no flagged parameter was passed,
219+
* No when at least one was, and null when the variant has no such parameters
220+
* (so the caller keeps its current behavior).
221+
*
222+
* @param Arg[] $args
223+
*/
224+
public static function resolvePureUnlessParameterPassedVerdict(ParametersAcceptor $variant, array $args): ?TrinaryLogic
225+
{
226+
$parameters = $variant->getParameters();
227+
$verdict = null;
228+
229+
foreach ($parameters as $parameterIndex => $parameter) {
230+
if (!$parameter instanceof ExtendedParameterReflection) {
231+
continue;
232+
}
233+
if ($parameter->isPureUnlessParameterPassedParameter()->no()) {
234+
continue;
235+
}
236+
237+
$verdict ??= TrinaryLogic::createYes();
238+
239+
$matchedArg = null;
240+
$hasNamedParameter = false;
241+
foreach ($args as $i => $arg) {
242+
if ($arg->name !== null) {
243+
$hasNamedParameter = true;
244+
if ($arg->name->name === $parameter->getName()) {
245+
$matchedArg = $arg;
246+
break;
247+
}
248+
249+
continue;
250+
}
251+
252+
if (!$hasNamedParameter && $i === $parameterIndex) {
253+
$matchedArg = $arg;
254+
break;
255+
}
256+
}
257+
258+
if ($matchedArg === null) {
259+
continue;
260+
}
261+
262+
$verdict = $verdict->and(TrinaryLogic::createNo());
263+
}
264+
265+
return $verdict;
266+
}
267+
206268
/** @return ImpurePointIdentifier */
207269
public function getIdentifier(): string
208270
{

0 commit comments

Comments
 (0)