Skip to content

Commit 3d6461c

Browse files
committed
Fix isset() bug
1 parent 176237a commit 3d6461c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/Type/TypeCombinator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,13 @@ public static function intersect(Type ...$types): Type
618618
}
619619

620620
// transform A & (B & C) to A & B & C
621-
foreach ($types as $i => &$type) {
621+
for ($i = 0; $i < count($types); $i++) {
622+
$type = $types[$i];
622623
if (!($type instanceof IntersectionType)) {
623624
continue;
624625
}
625626

626-
array_splice($types, $i, 1, $type->getTypes());
627+
array_splice($types, $i--, 1, $type->getTypes());
627628
}
628629

629630
// transform IntegerType & ConstantIntegerType to ConstantIntegerType

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@ public function testNativePropertyTypes(): void
101101
]);
102102
}
103103

104+
public function testBug4290(): void
105+
{
106+
$this->analyse([__DIR__ . '/data/bug-4290.php'], []);
107+
}
108+
104109
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Bug4290;
4+
5+
class HelloWorld
6+
{
7+
public function test(): void
8+
{
9+
$array = self::getArray();
10+
11+
$data = array_filter([
12+
'status' => isset($array['status']) ? $array['status'] : null,
13+
'value' => isset($array['value']) ? $array['value'] : null,
14+
]);
15+
16+
if (count($data) === 0) {
17+
return;
18+
}
19+
20+
isset($data['status']) ? 1 : 0;
21+
}
22+
23+
/**
24+
* @return string[]
25+
*/
26+
public static function getArray(): array
27+
{
28+
return ['value' => '100'];
29+
}
30+
}

0 commit comments

Comments
 (0)