Skip to content

Commit

Permalink
Fix infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 2, 2020
1 parent 4cad0c6 commit 6d81881
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function accepts(Type $otherType, bool $strictTypes): TrinaryLogic

public function isSuperTypeOf(Type $otherType): TrinaryLogic
{
if ($otherType instanceof IntersectionType && $this->equals($otherType)) {
return TrinaryLogic::createYes();
}

$results = [];
foreach ($this->getTypes() as $innerType) {
$results[] = $innerType->isSuperTypeOf($otherType);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ public function testBug3468(): void
$this->assertCount(0, $errors);
}

public function testBug3686(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-3686.php');
$this->assertCount(0, $errors);
}

/**
* @param string $file
* @return \PHPStan\Analyser\Error[]
Expand Down
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3686.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Bug3686;

/**
* @param mixed $request
* @return void
*/
function fred($request)
{
$keys = '';
foreach ($request as $index)
{
foreach ($index as $field)
{
if (isset($keys[$field]))
{
$keys[$field] = 0;
}
}
}
}

/**
* @param int[][] $a
*/
function replaceStringWithZero(array $a) : string {
$keys = 'agfsdafsafdrew1231414';

foreach ($a as $b) {
foreach ($b as $c) {
if (isset($keys[$c])) {
$keys[$c] = "0";
}
}
}

return $keys;
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,22 @@ public function dataUnion(): array
UnionType::class,
'array(\'a\' => int, \'b\' => int)|array(\'b\' => int, \'c\' => int)',
],
[
[
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
],
IntersectionType::class,
'string&hasOffset(int)',
],
[
[
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
],
IntersectionType::class,
'\'abc\'&hasOffset(int)',
],
];
}

Expand Down Expand Up @@ -2734,6 +2750,14 @@ public function dataIntersect(): array
ConstantArrayType::class,
'array(\'a\' => int, \'b\' => int)',
],
[
[
new StringType(),
new HasOffsetType(new IntegerType()),
],
IntersectionType::class,
'string&hasOffset(int)',
],
];
}

Expand Down

0 comments on commit 6d81881

Please sign in to comment.