Skip to content

Commit

Permalink
Test more cases related to bug #3636
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 12, 2020
1 parent efb11ec commit 71c51e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,7 @@ static function () use ($expr, $rightResult): MutatingScope {
} elseif ($expr instanceof Coalesce) {
$nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left, false);

if ($expr->left instanceof PropertyFetch) {
if ($expr->left instanceof PropertyFetch || $expr->left instanceof StaticPropertyFetch) {
$scope = $nonNullabilityResult->getScope();
} else {
$scope = $this->lookForEnterVariableAssign($nonNullabilityResult->getScope(), $expr->left);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ public function testBug3636(): void
{
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-3636.php'], []);
$this->analyse([__DIR__ . '/data/bug-3636.php'], [
[
'Property Bug3636\Bar::$date is never written, only read.',
22,
],
]);
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-3636.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,42 @@ public function getDate(): \DateTimeImmutable
}

}

class Bar
{

/** @var \DateTimeImmutable */
private $date;

public function getDate(): ?\DateTimeImmutable
{
return $this->date ?? null;
}

}

class Baz
{

/** @var string */
private $date;

public function getDate(): string
{
return $this->date ?? ($this->date = random_bytes(16));
}

}

class Lorem
{

/** @var string */
private static $date;

public function getDate(): string
{
return self::$date ?? (self::$date = random_bytes(16));
}

}

0 comments on commit 71c51e4

Please sign in to comment.