Skip to content

Commit 9df712a

Browse files
committed
Keep the variable flow of the declare body
Closes phpstan/phpstan#15394 Amp-Thread-ID: https://ampcode.com/threads/T-01a213dc-df05-74d5-92bf-216908d13882
1 parent 6b4cb63 commit 9df712a

7 files changed

Lines changed: 103 additions & 32 deletions

File tree

‎src/Analyser/StmtHandler/DeclareHandler.php‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ public function processStmt(
3939
): InternalStatementResult
4040
{
4141
$dependencies = [];
42-
$hasYield = false;
43-
$throwPoints = [];
44-
$impurePoints = [];
45-
$alwaysTerminating = false;
46-
$exitPoints = [];
4742
foreach ($stmt->declares as $declare) {
4843
$nodeScopeResolver->callNodeCallback($nodeCallback, $declare, $scope, $storage);
4944
// the value is a constant scalar - process it so its result is stored
@@ -60,18 +55,17 @@ public function processStmt(
6055
$scope = $scope->enterDeclareStrictTypes();
6156
}
6257

63-
if ($stmt->stmts !== null) {
64-
$result = $nodeScopeResolver->processStmtNodesInternal($stmt, $stmt->stmts, $scope, $storage, $nodeCallback, $context);
65-
$dependencies[] = $result->getDependencies();
66-
$scope = $result->getScope();
67-
$hasYield = $result->hasYield();
68-
$throwPoints = $result->getThrowPoints();
69-
$impurePoints = $result->getImpurePoints();
70-
$alwaysTerminating = $result->isAlwaysTerminating();
71-
$exitPoints = $result->getExitPoints();
58+
if ($stmt->stmts === null) {
59+
return new InternalStatementResult($scope, hasYield: false, isAlwaysTerminating: false, exitPoints: [], throwPoints: [], impurePoints: [], dependencies: Dependencies::merge(...$dependencies));
7260
}
7361

74-
return new InternalStatementResult($scope, hasYield: $hasYield, isAlwaysTerminating: $alwaysTerminating, exitPoints: $exitPoints, throwPoints: $throwPoints, impurePoints: $impurePoints, dependencies: Dependencies::merge(...$dependencies));
62+
// the declare body always runs, in the enclosing scope - unlike a block,
63+
// polluteScopeWithBlock does not apply, so the body's result is the
64+
// statement's result, including its variable flow
65+
$result = $nodeScopeResolver->processStmtNodesInternal($stmt, $stmt->stmts, $scope, $storage, $nodeCallback, $context);
66+
$dependencies[] = $result->getDependencies();
67+
68+
return $result->withDependencies(Dependencies::merge(...$dependencies));
7569
}
7670

7771
}

‎tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,14 @@ public function testBug15263(): void
443443
$this->analyse([__DIR__ . '/data/bug-15263.php'], []);
444444
}
445445

446+
public function testBug15394(): void
447+
{
448+
$this->analyse([__DIR__ . '/data/bug-15394.php'], [
449+
['Value assigned to variable $foo is never read before being overwritten.', 14],
450+
['Variable $foo is never read.', 24],
451+
]);
452+
}
453+
446454
public function testInferredBranchesStayPossible(): void
447455
{
448456
$this->analyse([__DIR__ . '/data/unused-variable-inferred-branches.php'], []);

‎tests/PHPStan/Rules/DeadCode/UnusedVariableRuleWithoutScopePollutionTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ public function testScopePollution(): void
3333
$this->analyse([__DIR__ . '/data/unused-variable-scope-pollution.php'], []);
3434
}
3535

36+
public function testBug15394(): void
37+
{
38+
// a declare body is not a block - it always runs, whatever polluteScopeWithBlock says
39+
$this->analyse([__DIR__ . '/data/bug-15394.php'], [
40+
['Value assigned to variable $foo is never read before being overwritten.', 14],
41+
['Variable $foo is never read.', 24],
42+
]);
43+
}
44+
3645
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15394;
4+
5+
function x(): void {
6+
$b = 1;
7+
declare(ticks=1) {
8+
echo $b;
9+
}
10+
}
11+
12+
function writeInside(int $a): void
13+
{
14+
$foo = 1;
15+
declare(ticks=1) {
16+
$foo = $a;
17+
}
18+
echo $foo;
19+
}
20+
21+
function neverRead(): void
22+
{
23+
declare(ticks=1) {
24+
$foo = 1;
25+
}
26+
}
27+
28+
function declareInBlock(): void
29+
{
30+
$b = 1;
31+
{
32+
declare(ticks=1) {
33+
echo $b;
34+
}
35+
}
36+
}
37+
38+
function blockInDeclare(): void
39+
{
40+
$b = 1;
41+
declare(ticks=1) {
42+
{
43+
echo $b;
44+
}
45+
}
46+
}

‎tests/PHPStan/Rules/Functions/UnusedFunctionParametersRuleTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ protected function getRule(): Rule
1717
return new UnusedFunctionParametersRule(self::getContainer()->getByType(UnusedParametersCheck::class));
1818
}
1919

20+
public function testDeclareBlock(): void
21+
{
22+
$this->analyse([__DIR__ . '/data/unused-function-parameters-declare.php'], [
23+
[
24+
'Function UnusedFunctionParametersDeclare\\unusedWithDeclare() has an unused parameter $x.',
25+
12,
26+
],
27+
]);
28+
}
29+
2030
public function testRule(): void
2131
{
2232
$this->analyse([__DIR__ . '/data/unused-function-parameters.php'], [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace UnusedFunctionParametersDeclare;
4+
5+
function readInsideDeclare(int $x): void
6+
{
7+
declare(ticks=1) {
8+
echo $x;
9+
}
10+
}
11+
12+
function unusedWithDeclare(int $x): void
13+
{
14+
declare(ticks=1) {
15+
echo 1;
16+
}
17+
}

‎turbo-ext/src/DeclareHandler.cpp‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,12 @@ class DeclareHandler
103103
if (UNEXPECTED(mergedDependencies.isUndef())) return zv::Val();
104104
return pt_internal_statement_result_new(scope, false, false, &emptyArray, &emptyArray, &emptyArray, NULL, NULL, -1, mergedDependencies.raw());
105105
}
106+
// the declare body always runs, in the enclosing scope - unlike a block,
107+
// polluteScopeWithBlock does not apply, so the body's result is the
108+
// statement's result, including its variable flow
106109
zv::Val stmtsHold = zv::Val::copyOf(zv::Ref(stmts));
107110
zv::Val result = pt_node_scope_resolver_process_stmt_nodes_internal(nodeScopeResolver, stmt, stmtsHold.raw(), scope, storage, nodeCallback, context);
108111
if (UNEXPECTED(result.isUndef())) return zv::Val();
109-
zv::Val resultScopeHold;
110-
zval *resultScope = pt_internal_statement_result_scope(result.raw(), resultScopeHold);
111-
if (UNEXPECTED(resultScope == NULL)) return zv::Val();
112-
bool hasYield;
113-
if (UNEXPECTED(!pt_internal_statement_result_has_yield(result.raw(), hasYield))) return zv::Val();
114-
zv::Val throwPointsHold;
115-
zval *throwPoints = pt_internal_statement_result_throw_points(result.raw(), throwPointsHold);
116-
if (UNEXPECTED(throwPoints == NULL)) return zv::Val();
117-
zv::Val impurePointsHold;
118-
zval *impurePoints = pt_internal_statement_result_impure_points(result.raw(), impurePointsHold);
119-
if (UNEXPECTED(impurePoints == NULL)) return zv::Val();
120-
bool alwaysTerminating;
121-
if (UNEXPECTED(!pt_internal_statement_result_is_always_terminating(result.raw(), alwaysTerminating))) return zv::Val();
122-
zv::Val exitPointsHold;
123-
zval *exitPoints = pt_internal_statement_result_exit_points(result.raw(), exitPointsHold);
124-
if (UNEXPECTED(exitPoints == NULL)) return zv::Val();
125112
{
126113
zv::Val hold;
127114
zval *resultDependencies = pt_internal_statement_result_dependencies(result.raw(), hold);
@@ -130,7 +117,7 @@ class DeclareHandler
130117
}
131118
zv::Val mergedDependencies = pt_dependencies_merge_list(dependencies.table());
132119
if (UNEXPECTED(mergedDependencies.isUndef())) return zv::Val();
133-
return pt_internal_statement_result_new(resultScope, hasYield, alwaysTerminating, exitPoints, throwPoints, impurePoints, NULL, NULL, -1, mergedDependencies.raw());
120+
return pt_internal_statement_result_with_dependencies(result.raw(), mergedDependencies.raw());
134121
}
135122

136123
/* the statement-handler entry (Engine.h) */

0 commit comments

Comments
 (0)