Skip to content
Merged
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
6 changes: 5 additions & 1 deletion src/Analyser/ExprHandler/PreDecHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$assignedScope,
beforeScope: $scope,
expr: $expr,
variableFlow: VariableFlow::sequence($varResult->getVariableFlow(), $valueFlowWrite !== null && $context->isValueConsumed() ? VariableFlow::inputs($valueFlowWrite->getId(), $context->getValueFlowTarget() !== null ? $context->getValueFlowTarget()->getId() : null) : null, VariableFlowBuilder::targetWrite($expr->var, VariableWrite::KIND_PRE_DEC, $assignedScope, $storage)),

@staabm staabm Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe there is some code-style rule we could enforce so such long expressions will be split over more lines by default - which would ease reading diffs in such lines (separate PR)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing! Agreed, a separate formatting-rule PR would keep this fix focused and make future diffs easier to read.

variableFlow: VariableFlow::sequence(
$varResult->getVariableFlow(),
VariableFlowBuilder::targetWrite($expr->var, VariableWrite::KIND_PRE_DEC, $assignedScope, $storage),
$valueFlowWrite !== null && $context->isValueConsumed() ? VariableFlowBuilder::targetRead($expr->var, $storage, true, $context->getValueFlowTarget() !== null ? $context->getValueFlowTarget()->getId() : null) : null,
),
hasYield: $varResult->hasYield(),
isAlwaysTerminating: $varResult->isAlwaysTerminating(),
throwPoints: $varResult->getThrowPoints(),
Expand Down
6 changes: 5 additions & 1 deletion src/Analyser/ExprHandler/PreIncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$assignedScope,
beforeScope: $scope,
expr: $expr,
variableFlow: VariableFlow::sequence($varResult->getVariableFlow(), $valueFlowWrite !== null && $context->isValueConsumed() ? VariableFlow::inputs($valueFlowWrite->getId(), $context->getValueFlowTarget() !== null ? $context->getValueFlowTarget()->getId() : null) : null, VariableFlowBuilder::targetWrite($expr->var, VariableWrite::KIND_PRE_INC, $assignedScope, $storage)),
variableFlow: VariableFlow::sequence(
$varResult->getVariableFlow(),
VariableFlowBuilder::targetWrite($expr->var, VariableWrite::KIND_PRE_INC, $assignedScope, $storage),
$valueFlowWrite !== null && $context->isValueConsumed() ? VariableFlowBuilder::targetRead($expr->var, $storage, true, $context->getValueFlowTarget() !== null ? $context->getValueFlowTarget()->getId() : null) : null,
),
hasYield: $varResult->hasYield(),
isAlwaysTerminating: $varResult->isAlwaysTerminating(),
throwPoints: $varResult->getThrowPoints(),
Expand Down
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 = '12acf37';
public const EXPECTED_EXTENSION_VERSION = '9f63407';

private static bool $active = false;

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

public function testBug15411(): void
{
$this->analyse([__DIR__ . '/data/bug-15411.php'], []);
}

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

namespace Bug15411;

function nextCookieValue(int $count): string
{
return (string) ++$count;
}

echo nextCookieValue(1);

function previousCookieValue(int $count): string
{
return (string) --$count;
}

echo previousCookieValue(1);

function nextAssignedValue(int $count): int
{
$value = ++$count;
return $value;
}

function previousAssignedValue(int $count): int
{
$value = --$count;
return $value;
}

/** @param array{count: int} $counts */
function nextOffsetValue(array $counts): int
{
return ++$counts['count'];
}

/** @param array{count: int} $counts */
function previousOffsetValue(array $counts): int
{
return --$counts['count'];
}
2 changes: 1 addition & 1 deletion turbo-ext/src/PreDecHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class PreDecHandler
zv::Val assignedScope = ptse::scopeOf(assignResult.raw());
if (UNEXPECTED(assignedScope.isUndef())) return zv::Val();

zv::Val variableFlow = ptse::incDecFlow(child.variableFlow.raw(), valueFlowWrite.raw(), context, var, pt_pdh_kind, assignedScope.raw(), storage);
zv::Val variableFlow = ptse::incDecFlow(child.variableFlow.raw(), valueFlowWrite.raw(), context, var, pt_pdh_kind, assignedScope.raw(), storage, true);
if (UNEXPECTED(variableFlow.isUndef())) return zv::Val();
zv::Val dependenciesHold;
zval *dependencies = pt_expression_result_dependencies(varResult.raw(), dependenciesHold);
Expand Down
2 changes: 1 addition & 1 deletion turbo-ext/src/PreIncHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class PreIncHandler
zv::Val assignedScope = ptse::scopeOf(assignResult.raw());
if (UNEXPECTED(assignedScope.isUndef())) return zv::Val();

zv::Val variableFlow = ptse::incDecFlow(child.variableFlow.raw(), valueFlowWrite.raw(), context, var, pt_pih_kind, assignedScope.raw(), storage);
zv::Val variableFlow = ptse::incDecFlow(child.variableFlow.raw(), valueFlowWrite.raw(), context, var, pt_pih_kind, assignedScope.raw(), storage, true);
if (UNEXPECTED(variableFlow.isUndef())) return zv::Val();
zv::Val dependenciesHold;
zval *dependencies = pt_expression_result_dependencies(varResult.raw(), dependenciesHold);
Expand Down
14 changes: 7 additions & 7 deletions turbo-ext/src/SimpleExprHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ inline zv::Val valueFlowContext(zval *context, zval *valueFlowWrite)
return pt_expression_context_enter_value_flow(deep.raw(), valueFlowWrite, false);
}

/* VariableFlow::sequence($varResult->getVariableFlow(), $valueFlowWrite !==
* null && $context->isValueConsumed() ? VariableFlow::inputs($valueFlowWrite->getId(),
* $context->getValueFlowTarget() !== null ? $context->getValueFlowTarget()->getId() : null)
* : null, VariableFlowBuilder::targetWrite($var, $kind, $assignedScope, $storage)) */
inline zv::Val incDecFlow(zval *varFlow, zval *valueFlowWrite, zval *context, zval *var, zend_long kind, zval *assignedScope, zval *storage)
/* Post-inc/dec consumes the inputs of the write; pre-inc/dec consumes the
* newly written value, so its target read must follow the target write. */
inline zv::Val incDecFlow(zval *varFlow, zval *valueFlowWrite, zval *context, zval *var, zend_long kind, zval *assignedScope, zval *storage, bool readsNewValue = false)
{
zv::Val inputsFlow = zv::Val::null();
if (Z_TYPE_P(valueFlowWrite) != IS_NULL) {
Expand All @@ -154,13 +152,15 @@ inline zv::Val incDecFlow(zval *varFlow, zval *valueFlowWrite, zval *context, zv
targetId = variableWriteId(target.raw());
if (UNEXPECTED(targetId.isUndef())) return zv::Val();
}
inputsFlow = pt_variable_flow_inputs(zval_get_long(writeId.raw()), targetId.raw());
inputsFlow = readsNewValue
? pt_variable_flow_builder_target_read(var, storage, true, targetId.raw())
: pt_variable_flow_inputs(zval_get_long(writeId.raw()), targetId.raw());
if (UNEXPECTED(inputsFlow.isUndef())) return zv::Val();
}
}
zv::Val targetWriteFlow = pt_variable_flow_builder_target_write(var, kind, assignedScope, storage, NULL);
if (UNEXPECTED(targetWriteFlow.isUndef())) return zv::Val();
zv::Args flows{varFlow, inputsFlow.raw(), targetWriteFlow.raw()};
zv::Args flows{varFlow, readsNewValue ? targetWriteFlow.raw() : inputsFlow.raw(), readsNewValue ? inputsFlow.raw() : targetWriteFlow.raw()};
return pt_variable_flow_sequence(3, flows);
}

Expand Down
Loading