Skip to content

Commit 6d81881

Browse files
committed
Fix infinite loop
1 parent 4cad0c6 commit 6d81881

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/Type/IntersectionType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function accepts(Type $otherType, bool $strictTypes): TrinaryLogic
5656

5757
public function isSuperTypeOf(Type $otherType): TrinaryLogic
5858
{
59+
if ($otherType instanceof IntersectionType && $this->equals($otherType)) {
60+
return TrinaryLogic::createYes();
61+
}
62+
5963
$results = [];
6064
foreach ($this->getTypes() as $innerType) {
6165
$results[] = $innerType->isSuperTypeOf($otherType);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ public function testBug3468(): void
256256
$this->assertCount(0, $errors);
257257
}
258258

259+
public function testBug3686(): void
260+
{
261+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-3686.php');
262+
$this->assertCount(0, $errors);
263+
}
264+
259265
/**
260266
* @param string $file
261267
* @return \PHPStan\Analyser\Error[]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bug3686;
4+
5+
/**
6+
* @param mixed $request
7+
* @return void
8+
*/
9+
function fred($request)
10+
{
11+
$keys = '';
12+
foreach ($request as $index)
13+
{
14+
foreach ($index as $field)
15+
{
16+
if (isset($keys[$field]))
17+
{
18+
$keys[$field] = 0;
19+
}
20+
}
21+
}
22+
}
23+
24+
/**
25+
* @param int[][] $a
26+
*/
27+
function replaceStringWithZero(array $a) : string {
28+
$keys = 'agfsdafsafdrew1231414';
29+
30+
foreach ($a as $b) {
31+
foreach ($b as $c) {
32+
if (isset($keys[$c])) {
33+
$keys[$c] = "0";
34+
}
35+
}
36+
}
37+
38+
return $keys;
39+
}

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,22 @@ public function dataUnion(): array
16791679
UnionType::class,
16801680
'array(\'a\' => int, \'b\' => int)|array(\'b\' => int, \'c\' => int)',
16811681
],
1682+
[
1683+
[
1684+
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
1685+
TypeCombinator::intersect(new StringType(), new HasOffsetType(new IntegerType())),
1686+
],
1687+
IntersectionType::class,
1688+
'string&hasOffset(int)',
1689+
],
1690+
[
1691+
[
1692+
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
1693+
TypeCombinator::intersect(new ConstantStringType('abc'), new HasOffsetType(new IntegerType())),
1694+
],
1695+
IntersectionType::class,
1696+
'\'abc\'&hasOffset(int)',
1697+
],
16821698
];
16831699
}
16841700

@@ -2734,6 +2750,14 @@ public function dataIntersect(): array
27342750
ConstantArrayType::class,
27352751
'array(\'a\' => int, \'b\' => int)',
27362752
],
2753+
[
2754+
[
2755+
new StringType(),
2756+
new HasOffsetType(new IntegerType()),
2757+
],
2758+
IntersectionType::class,
2759+
'string&hasOffset(int)',
2760+
],
27372761
];
27382762
}
27392763

0 commit comments

Comments
 (0)