Skip to content

Commit

Permalink
Process statements inside declare(ticks=...)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 30, 2023
1 parent a0e0dfb commit eb39381
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ private function processStmtNode(
if ($stmt instanceof Node\Stmt\Declare_) {
$hasYield = false;
$throwPoints = [];
$alwaysTerminating = false;
$exitPoints = [];
foreach ($stmt->declares as $declare) {
$nodeCallback($declare, $scope);
$nodeCallback($declare->value, $scope);
Expand All @@ -427,6 +429,17 @@ private function processStmtNode(

$scope = $scope->enterDeclareStrictTypes();
}

if ($stmt->stmts !== null) {
$result = $this->processStmtNodes($stmt, $stmt->stmts, $scope, $nodeCallback, $context);
$scope = $result->getScope();
$hasYield = $result->hasYield();
$throwPoints = $result->getThrowPoints();
$alwaysTerminating = $result->isAlwaysTerminating();
$exitPoints = $result->getExitPoints();
}

return new StatementResult($scope, $hasYield, $alwaysTerminating, $exitPoints, $throwPoints);
} elseif ($stmt instanceof Node\Stmt\Function_) {
$hasYield = false;
$throwPoints = [];
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,15 @@ public function testBug9765(): void
$this->analyse([__DIR__ . '/data/bug-9765.php'], []);
}

public function testBug10059(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-10059.php'], []);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-10059.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.1

namespace Bug10059;

use DateTimeImmutable;

class Foo
{
public function __construct(
private readonly DateTimeImmutable $startDateTime
) {
}

public function bar(): void
{
declare(ticks=5) {
echo $this->startDateTime->format('Y-m-d H:i:s.u'), PHP_EOL;
}
}
}

0 comments on commit eb39381

Please sign in to comment.