Skip to content

Commit 472e24d

Browse files
committed
Report @pure-unless-parameter-passed on a non-optional parameter
A non-optional parameter is always passed, so the tag can never keep the function or method pure. Report it as a misuse, mirroring the existing @pure-unless-callable-is-impure redundancy check. The declaration-side flag was dropped for functions: enterFunction() was never given the @pure-unless-parameter-passed parameters (the method path already passed them), so FunctionPurityCheck could not see it. Thread it through as well.
1 parent 4902e4b commit 472e24d

6 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public function processStmtNode(
779779
$throwPoints = [];
780780
$impurePoints = [];
781781
$this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback);
782-
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, , $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts,, $phpDocParameterOutTypes, , , , $pureUnlessCallableIsImpureParameters] = $this->getPhpDocs($scope, $stmt);
782+
[$templateTypeMap, $phpDocParameterTypes, $phpDocImmediatelyInvokedCallableParameters, $phpDocClosureThisTypeParameters, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, , $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts,, $phpDocParameterOutTypes, , , , $pureUnlessCallableIsImpureParameters, $pureUnlessParameterPassedParameters] = $this->getPhpDocs($scope, $stmt);
783783

784784
foreach ($stmt->params as $param) {
785785
$this->processParamNode($stmt, $param, $scope, $storage, $nodeCallback);
@@ -810,6 +810,7 @@ public function processStmtNode(
810810
$phpDocImmediatelyInvokedCallableParameters,
811811
$phpDocClosureThisTypeParameters,
812812
$pureUnlessCallableIsImpureParameters,
813+
$pureUnlessParameterPassedParameters,
813814
);
814815
$functionReflection = $functionScope->getFunction();
815816
if (!$functionReflection instanceof PhpFunctionFromParserNodeReflection) {

‎src/Rules/Pure/FunctionPurityCheck.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ public function check(
9191
))->identifier(sprintf('pure%s.redundantUnlessCallable', $identifier))->build();
9292
}
9393

94+
foreach ($parameters as $parameter) {
95+
if (!$parameter->isPureUnlessParameterPassedParameter()->yes()) {
96+
continue;
97+
}
98+
if ($parameter->isOptional()) {
99+
continue;
100+
}
101+
102+
$errors[] = RuleErrorBuilder::message(sprintf(
103+
'%s is marked @pure-unless-parameter-passed for parameter $%s, but $%s is not optional, so %s is never pure.',
104+
$functionDescription,
105+
$parameter->getName(),
106+
$parameter->getName(),
107+
lcfirst($functionDescription),
108+
))->identifier(sprintf('pure%s.nonOptionalParameterPassed', $identifier))->build();
109+
}
110+
94111
if ($isPure->yes()) {
95112
foreach ($parameters as $parameter) {
96113
if (!$parameter->passedByReference()->createsNewVariable()) {

‎tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ public function testPureUnlessParameterPassed(): void
430430
'Possibly impure call to function PureUnlessParameterPassedFunction\myReplace() in pure function PureUnlessParameterPassedFunction\pureCallingFirstClassCallablePassingCount().',
431431
343,
432432
],
433+
[
434+
'Function PureUnlessParameterPassedFunction\nonOptionalUnlessParameterPassed() is marked @pure-unless-parameter-passed for parameter $count, but $count is not optional, so function PureUnlessParameterPassedFunction\nonOptionalUnlessParameterPassed() is never pure.',
435+
350,
436+
],
433437
]);
434438
}
435439

‎tests/PHPStan/Rules/Pure/PureMethodRuleTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,15 @@ public function testBug14557(): void
407407
]);
408408
}
409409

410+
public function testPureUnlessParameterPassed(): void
411+
{
412+
$this->treatPhpDocTypesAsCertain = true;
413+
$this->analyse([__DIR__ . '/data/pure-unless-parameter-passed-method.php'], [
414+
[
415+
'Method PureUnlessParameterPassedMethod\Replacer::replace() is marked @pure-unless-parameter-passed for parameter $count, but $count is not optional, so method PureUnlessParameterPassedMethod\Replacer::replace() is never pure.',
416+
12,
417+
],
418+
]);
419+
}
420+
410421
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PureUnlessParameterPassedMethod;
4+
5+
class Replacer
6+
{
7+
8+
/**
9+
* @param-out int $count
10+
* @pure-unless-parameter-passed $count
11+
*/
12+
public function replace(string $subject, int &$count): string
13+
{
14+
$count = 1;
15+
16+
return $subject;
17+
}
18+
19+
/**
20+
* @param-out int $count
21+
* @pure-unless-parameter-passed $count
22+
*/
23+
public function replaceOptional(string $subject, int &$count = 0): string
24+
{
25+
$count = 1;
26+
27+
return $subject;
28+
}
29+
30+
}

‎tests/PHPStan/Rules/Pure/data/pure-unless-parameter-passed.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,16 @@ function pureCallingFirstClassCallablePassingCount(string $s): string
342342
// stays possibly impure.
343343
return $f($s, $count);
344344
}
345+
346+
/**
347+
* @param-out int $count
348+
* @pure-unless-parameter-passed $count
349+
*/
350+
function nonOptionalUnlessParameterPassed(string $subject, int &$count): string
351+
{
352+
// $count is not optional, so it is always passed and the tag can never keep
353+
// the function pure.
354+
$count = 1;
355+
356+
return $subject;
357+
}

0 commit comments

Comments
 (0)