Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Analyser/StmtHandler/DeclareHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function processStmt(
$scope = $scope->enterDeclareStrictTypes();
}

$variableFlow = null;
if ($stmt->stmts !== null) {
$result = $nodeScopeResolver->processStmtNodesInternal($stmt, $stmt->stmts, $scope, $storage, $nodeCallback, $context);
$dependencies[] = $result->getDependencies();
Expand All @@ -69,9 +70,10 @@ public function processStmt(
$impurePoints = $result->getImpurePoints();
$alwaysTerminating = $result->isAlwaysTerminating();
$exitPoints = $result->getExitPoints();
$variableFlow = $result->getVariableFlow();
}

return new InternalStatementResult($scope, hasYield: $hasYield, isAlwaysTerminating: $alwaysTerminating, exitPoints: $exitPoints, throwPoints: $throwPoints, impurePoints: $impurePoints, dependencies: Dependencies::merge(...$dependencies));
return new InternalStatementResult($scope, hasYield: $hasYield, isAlwaysTerminating: $alwaysTerminating, exitPoints: $exitPoints, throwPoints: $throwPoints, impurePoints: $impurePoints, variableFlow: $variableFlow, dependencies: Dependencies::merge(...$dependencies));
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ public function testBug15225(): void
$this->analyse([__DIR__ . '/data/bug-15225.php'], []);
}

public function testBug15394(): void
{
$this->analyse([__DIR__ . '/data/bug-15394.php'], [
['Variable $z is never read.', 24],
]);
}

public function testFlowMessages(): void
{
$this->analyse([__DIR__ . '/data/unused-variable-flow-messages.php'], [
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-15394.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug15394;

function test(): void
{
$x = 1;
declare(ticks=1) {
echo $x;
}
}

function testAssignInsideDeclare(): void
{
declare(ticks=1) {
$y = 2;
}
echo $y;
}

function testUnusedInsideDeclare(): void
{
declare(ticks=1) {
$z = 3;
}
}
6 changes: 4 additions & 2 deletions turbo-ext/src/DeclareHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class DeclareHandler
if (UNEXPECTED(!pt_internal_statement_result_is_always_terminating(result.raw(), alwaysTerminating))) return zv::Val();
zv::Val exitPointsHold;
zval *exitPoints = pt_internal_statement_result_exit_points(result.raw(), exitPointsHold);
if (UNEXPECTED(exitPoints == NULL)) return zv::Val();
zv::Val variableFlowHold;
zval *variableFlow = pt_internal_statement_result_variable_flow(result.raw(), variableFlowHold);
if (UNEXPECTED(variableFlow == NULL)) return zv::Val();
{
zv::Val hold;
zval *resultDependencies = pt_internal_statement_result_dependencies(result.raw(), hold);
Expand All @@ -130,7 +132,7 @@ class DeclareHandler
}
zv::Val mergedDependencies = pt_dependencies_merge_list(dependencies.table());
if (UNEXPECTED(mergedDependencies.isUndef())) return zv::Val();
return pt_internal_statement_result_new(resultScope, hasYield, alwaysTerminating, exitPoints, throwPoints, impurePoints, NULL, NULL, -1, mergedDependencies.raw());
return pt_internal_statement_result_new(resultScope, hasYield, alwaysTerminating, exitPoints, throwPoints, impurePoints, NULL, variableFlow, -1, mergedDependencies.raw());
}

/* the statement-handler entry (Engine.h) */
Expand Down
Loading