Skip to content
Open
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 @@ -44,6 +44,7 @@ public function processStmt(
$impurePoints = [];
$alwaysTerminating = false;
$exitPoints = [];
$variableFlow = null;
foreach ($stmt->declares as $declare) {
$nodeScopeResolver->callNodeCallback($nodeCallback, $declare, $scope, $storage);
// the value is a constant scalar - process it so its result is stored
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));
}

}
2 changes: 1 addition & 1 deletion src/Turbo/TurboExtensionEnabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
final class TurboExtensionEnabler
{

public const EXPECTED_EXTENSION_VERSION = '28c29b8';
public const EXPECTED_EXTENSION_VERSION = '1858fff';

private static bool $active = false;

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

public function testBug15394(): void
{
$this->analyse([__DIR__ . '/data/bug-15394.php'], [
['Variable $unused is never read.', 34],
['Value assigned to variable $value is never read before being overwritten.', 40],
]);
}

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

namespace Bug15394;

function readInBlock(): void
{
$value = 1;
declare(ticks=1) {
echo $value;
}
}

function writeInBlock(): void
{
declare(ticks=1) {
$value = 1;
}
echo $value;
}

function readInNestedBlock(): void
{
$value = 1;
declare(ticks=1) {
declare(ticks=2) {
echo $value;
}
}
}

function unusedInBlock(): void
{
declare(ticks=1) {
$unused = 1;
}
}

function overwrittenInBlock(): void
{
$value = 1;
declare(ticks=1) {
$value = 2;
}
echo $value;
}

function readAfterDeclareStatement(): void
{
$value = 1;
declare(ticks=1);
echo $value;
}

function readInAlternativeBlock(): void
{
$value = 1;
declare(ticks=1):
echo $value;
enddeclare;
}
5 changes: 4 additions & 1 deletion turbo-ext/src/DeclareHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class DeclareHandler
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 +133,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