Skip to content

Commit

Permalink
Fix bug when specifying empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 11, 2020
1 parent 282da23 commit a73b48f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,25 +236,36 @@ public function specifyTypesInCondition(
$exprLeftType = $scope->getType($expr->left);
$exprRightType = $scope->getType($expr->right);

$types = null;

if (
$exprLeftType instanceof ConstantType
&& !$exprRightType instanceof ConstantType
&& !$expr->right instanceof Node\Scalar
) {
return $this->create(
$types = $this->create(
$expr->right,
$exprLeftType,
$context
);
}
if (
$exprRightType instanceof ConstantType
&& !$exprLeftType instanceof ConstantType
&& !$expr->left instanceof Node\Scalar
) {
return $this->create(
$leftType = $this->create(
$expr->left,
$exprRightType,
$context
);
if ($types !== null) {
$types = $types->unionWith($leftType);
} else {
$types = $leftType;
}
}

if ($types !== null) {
return $types;
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10530,6 +10530,11 @@ public function dataBug4206(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4206.php');
}

public function dataBugEmptyArray(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-empty-array.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -10721,6 +10726,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataInferPrivateTypedPropertyTypeFromConstructor
* @dataProvider dataBug4207
* @dataProvider dataBug4206
* @dataProvider dataBugEmptyArray
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPStan/Analyser/data/bug-empty-array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace EmptyArrayInProperty;

use function PHPStan\Analyser\assertType;

class Foo
{

/** @var string[] */
private $comments;

public function doFoo(): void
{
assertType('array<string>', $this->comments);
$this->comments = [];
assertType('array()', $this->comments);
if ($this->comments === []) {
assertType('array()', $this->comments);
return;
} else {
assertType('*NEVER*', $this->comments);
}

assertType('*NEVER*', $this->comments);
}

public function doBar(): void
{
assertType('array<string>', $this->comments);
$this->comments = [];
assertType('array()', $this->comments);
if ([] === $this->comments) {
assertType('array()', $this->comments);
return;
} else {
assertType('*NEVER*', $this->comments);
}

assertType('*NEVER*', $this->comments);
}

}

0 comments on commit a73b48f

Please sign in to comment.