Skip to content

Commit 830c5cc

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents 6777f6d + 4e6a1a8 commit 830c5cc

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,6 +3609,15 @@ private function createConditionalExpressions(
36093609
}
36103610

36113611
foreach ($variableTypeGuards as $guardExprString => $guardHolder) {
3612+
if (
3613+
array_key_exists($exprString, $theirExpressionTypes)
3614+
&& $theirExpressionTypes[$exprString]->getCertainty()->yes()
3615+
&& array_key_exists($guardExprString, $theirExpressionTypes)
3616+
&& $theirExpressionTypes[$guardExprString]->getCertainty()->yes()
3617+
&& !$guardHolder->getType()->isSuperTypeOf($theirExpressionTypes[$guardExprString]->getType())->no()
3618+
) {
3619+
continue;
3620+
}
36123621
$conditionalExpression = new ConditionalExpressionHolder([$guardExprString => $guardHolder], $holder);
36133622
$conditionalExpressions[$exprString][$conditionalExpression->getKey()] = $conditionalExpression;
36143623
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug10085;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
/**
12+
* @param array<int, string> $foo
13+
* @param list<string> $bar
14+
*/
15+
public function sayHello(array $foo, array $bar): void
16+
{
17+
$a = $foo;
18+
if ($a === []) {
19+
$a = $bar;
20+
}
21+
22+
if ($a === []) {
23+
return;
24+
}
25+
26+
assertType('array<int, string>', $foo);
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug11328;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
enum Status: string {
10+
case Live = 's1';
11+
case Expired = 's2';
12+
}
13+
14+
/** @param Status[] $statuses */
15+
function check(array $statuses): void {
16+
$fromDeadline = null;
17+
$toDeadline = null;
18+
if (in_array(Status::Live, $statuses, true)) {
19+
$fromDeadline = (new \DateTimeImmutable())->setTime(23, 59, 59);
20+
}
21+
if (in_array(Status::Expired, $statuses, true)) {
22+
assertType('DateTimeImmutable|null', $fromDeadline);
23+
if ($fromDeadline === null) {
24+
$toDeadline = (new \DateTimeImmutable())->setTime(0, 0, 0);
25+
}
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14211;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/** @param array<mixed> $data */
10+
function DoSomithing(array $data): bool {
11+
12+
if (!isset($data['x']))
13+
return false;
14+
15+
$m = isset($data['y']);
16+
17+
if ($m) {
18+
assertType('true', $m); // ok: true
19+
}
20+
assertType('bool', $m); // ok: bool
21+
22+
if ($m) {
23+
assertType('true', $m); // <-- should not be: NEVER
24+
}
25+
26+
return true;
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14411Regression;
6+
7+
use function PHPStan\Testing\assertType;
8+
use function PHPStan\Testing\assertVariableCertainty;
9+
use PHPStan\TrinaryLogic;
10+
11+
interface OrderInterface {}
12+
13+
class Event
14+
{
15+
/** @return mixed */
16+
public function getSubject()
17+
{
18+
return new \stdClass();
19+
}
20+
}
21+
22+
function getOrder(Event|OrderInterface $event): OrderInterface
23+
{
24+
if ($event instanceof Event) {
25+
$order = $event->getSubject();
26+
assert($order instanceof OrderInterface);
27+
}
28+
29+
if ($event instanceof OrderInterface) {
30+
$order = $event;
31+
}
32+
33+
assertVariableCertainty(TrinaryLogic::createYes(), $order);
34+
35+
return $order;
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug14411;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/** @phpstan-impure */
10+
function get_mixed(): mixed {
11+
return random_int(0, 1) ? 'foo' : null;
12+
}
13+
14+
/** @phpstan-impure */
15+
function get_optional_int(): ?int {
16+
return random_int(0, 1) ? 42 : null;
17+
}
18+
19+
function (): void {
20+
$a = get_mixed();
21+
22+
if ($a !== null) {
23+
$b = $a;
24+
}
25+
else {
26+
$b = get_optional_int();
27+
}
28+
if ($b !== null) {
29+
assertType('mixed', $a);
30+
if ($a === null) {
31+
echo 'this is absolutely possible';
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)