diff --git a/src/Analyser/StmtHandler/DeclareHandler.php b/src/Analyser/StmtHandler/DeclareHandler.php index 25518f529e8..b9fba510f09 100644 --- a/src/Analyser/StmtHandler/DeclareHandler.php +++ b/src/Analyser/StmtHandler/DeclareHandler.php @@ -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 @@ -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)); } } diff --git a/tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php index 2ceca6991c7..6257d611750 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php @@ -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'], []); diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-15394.php b/tests/PHPStan/Rules/DeadCode/data/bug-15394.php new file mode 100644 index 00000000000..f342e76a2fd --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-15394.php @@ -0,0 +1,47 @@ +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'], [ diff --git a/tests/PHPStan/Rules/Functions/data/unused-function-parameters-declare.php b/tests/PHPStan/Rules/Functions/data/unused-function-parameters-declare.php new file mode 100644 index 00000000000..3a3a7855c2d --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/unused-function-parameters-declare.php @@ -0,0 +1,17 @@ +