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));
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ protected function getRule(): Rule
return new UnusedVariableRule(self::getContainer()->getByType(ExprPrinter::class));
}

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

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

namespace Bug15394;

function x(): void {

$b = 1;
declare(ticks=1) {
echo $b;
}
}

function writeInsideReadAfter(): void
{
declare(ticks=1) {
$a = 1;
}

echo $a;
}

function overwrittenInside(): void
{
$a = 1;
declare(ticks=1) {
$a = 2;
}

echo $a;
}

function unusedInside(): void
{
declare(ticks=1) {
$a = 1;
}
}

function nested(): void
{
$a = 1;
declare(ticks=1) {
declare(ticks=2) {
echo $a;
}
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Functions/UnusedFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ protected function getRule(): Rule
return new UnusedFunctionParametersRule(self::getContainer()->getByType(UnusedParametersCheck::class));
}

public function testDeclareBlock(): void
{
$this->analyse([__DIR__ . '/data/unused-function-parameters-declare.php'], [
[
'Function UnusedFunctionParametersDeclare\\unusedWithDeclare() has an unused parameter $x.',
12,
],
]);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/unused-function-parameters.php'], [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace UnusedFunctionParametersDeclare;

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

function unusedWithDeclare(int $x): void
{
declare(ticks=1) {
echo 1;
}
}
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, Z_TYPE_P(variableFlow) == IS_NULL ? NULL : variableFlow, -1, mergedDependencies.raw());
}

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