Skip to content

Commit

Permalink
Fix isset() bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 30, 2020
1 parent 176237a commit 3d6461c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,13 @@ public static function intersect(Type ...$types): Type
}

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

array_splice($types, $i, 1, $type->getTypes());
array_splice($types, $i--, 1, $type->getTypes());
}

// transform IntegerType & ConstantIntegerType to ConstantIntegerType
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ public function testNativePropertyTypes(): void
]);
}

public function testBug4290(): void
{
$this->analyse([__DIR__ . '/data/bug-4290.php'], []);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-4290.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Bug4290;

class HelloWorld
{
public function test(): void
{
$array = self::getArray();

$data = array_filter([
'status' => isset($array['status']) ? $array['status'] : null,
'value' => isset($array['value']) ? $array['value'] : null,
]);

if (count($data) === 0) {
return;
}

isset($data['status']) ? 1 : 0;
}

/**
* @return string[]
*/
public static function getArray(): array
{
return ['value' => '100'];
}
}

0 comments on commit 3d6461c

Please sign in to comment.