Skip to content

Commit 75ac060

Browse files
authored
Merge branch refs/heads/1.12.x into 2.1.x
2 parents c27879c + 8734057 commit 75ac060

File tree

7 files changed

+55
-4
lines changed

7 files changed

+55
-4
lines changed

src/Analyser/MutatingScope.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5138,7 +5138,10 @@ private static function generalizeType(Type $a, Type $b, int $depth): Type
51385138
} else {
51395139
$constantArraysA = TypeCombinator::union(...$constantArrays['a']);
51405140
$constantArraysB = TypeCombinator::union(...$constantArrays['b']);
5141-
if ($constantArraysA->getIterableKeyType()->equals($constantArraysB->getIterableKeyType())) {
5141+
if (
5142+
$constantArraysA->getIterableKeyType()->equals($constantArraysB->getIterableKeyType())
5143+
&& $constantArraysA->getArraySize()->equals($constantArraysB->getArraySize())
5144+
) {
51425145
$resultArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
51435146
foreach (TypeUtils::flattenTypes($constantArraysA->getIterableKeyType()) as $keyType) {
51445147
$resultArrayBuilder->setOffsetValueType(
@@ -5158,7 +5161,11 @@ private static function generalizeType(Type $a, Type $b, int $depth): Type
51585161
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableKeyType(), $constantArraysB->getIterableKeyType(), $depth + 1)),
51595162
TypeCombinator::union(self::generalizeType($constantArraysA->getIterableValueType(), $constantArraysB->getIterableValueType(), $depth + 1)),
51605163
);
5161-
if ($constantArraysA->isIterableAtLeastOnce()->yes() && $constantArraysB->isIterableAtLeastOnce()->yes()) {
5164+
if (
5165+
$constantArraysA->isIterableAtLeastOnce()->yes()
5166+
&& $constantArraysB->isIterableAtLeastOnce()->yes()
5167+
&& $constantArraysA->getArraySize()->getGreaterOrEqualType()->isSuperTypeOf($constantArraysB->getArraySize())->yes()
5168+
) {
51625169
$resultType = TypeCombinator::intersect($resultType, new NonEmptyArrayType());
51635170
}
51645171
if ($constantArraysA->isList()->yes() && $constantArraysB->isList()->yes()) {

tests/PHPStan/Analyser/nsrt/bug-1021.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function foobar() {
1313
}
1414
}
1515

16-
assertType('array{0?: int<1, max>, 1?: 2|3, 2?: 3}', $x);
16+
assertType('array<1|2|3>&list', $x);
1717

1818
if ($x) {
1919
}

tests/PHPStan/Analyser/nsrt/bug7856.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function doFoo() {
1010
$endDate = new DateTimeImmutable('+1year');
1111

1212
do {
13-
assertType("array{'+1week', '+1months', '+6months', '+17months'}|array{0: literal-string&lowercase-string&non-falsy-string, 1?: literal-string&lowercase-string&non-falsy-string, 2?: '+17months'}", $intervals);
13+
assertType("list<literal-string&lowercase-string&non-falsy-string>", $intervals);
1414
$periodEnd = $periodEnd->modify(array_shift($intervals));
1515
} while (count($intervals) > 0 && $periodEnd->format('U') < $endDate);
1616
}

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ public function testLastMatchArm(bool $reportAlwaysTrueInLastCondition, array $e
736736
$this->analyse([__DIR__ . '/data/strict-comparison-last-match-arm.php'], $expectedErrors);
737737
}
738738

739+
public function testBug8030(): void
740+
{
741+
$this->checkAlwaysTrueStrictComparison = true;
742+
$this->analyse([__DIR__ . '/data/bug-8030.php'], []);
743+
}
744+
739745
public function testBug8776Part1(): void
740746
{
741747
$this->analyse([__DIR__ . '/data/bug-8776-1.php'], []);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug8030;
4+
5+
function (): ?string
6+
{
7+
$refs = range('a', 'e');
8+
9+
while (null !== $ref = array_shift($refs)) {
10+
if (random_int(0, 1) === 1) {
11+
return $ref;
12+
}
13+
}
14+
15+
return null;
16+
};

tests/PHPStan/Rules/Variables/EmptyRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,12 @@ public function testBug9403(bool $treatPhpDocTypesAsCertain): void
199199
$this->analyse([__DIR__ . '/data/bug-9403.php'], []);
200200
}
201201

202+
public function testBug12658(): void
203+
{
204+
$this->treatPhpDocTypesAsCertain = true;
205+
$this->strictUnnecessaryNullsafePropertyFetch = false;
206+
207+
$this->analyse([__DIR__ . '/data/bug-12658.php'], []);
208+
}
209+
202210
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug12658;
4+
5+
function (): void {
6+
$ads = ['inline_1', 'inline_2', 'inline_3', 'inline_4'];
7+
$paragraphs = ['a', 'b', 'c', 'd', 'f'];
8+
9+
foreach ($paragraphs as $key => $paragraph) {
10+
if (!empty($ads)) {
11+
$ad = array_shift($ads);
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)