Skip to content

Commit 7d43259

Browse files
authored
Merge branch refs/heads/1.11.x into 1.12.x
2 parents 9613c66 + 07d6405 commit 7d43259

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed

phpstan-baseline.neon

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ parameters:
15541554

15551555
-
15561556
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
1557-
count: 4
1557+
count: 5
15581558
path: src/Type/TypeCombinator.php
15591559

15601560
-
@@ -1597,6 +1597,16 @@ parameters:
15971597
count: 1
15981598
path: src/Type/TypeCombinator.php
15991599

1600+
-
1601+
message: "#^Instanceof between PHPStan\\\\Type\\\\Constant\\\\ConstantIntegerType and PHPStan\\\\Type\\\\Constant\\\\ConstantIntegerType will always evaluate to true\\.$#"
1602+
count: 1
1603+
path: src/Type/TypeCombinator.php
1604+
1605+
-
1606+
message: "#^Result of \\|\\| is always true\\.$#"
1607+
count: 1
1608+
path: src/Type/TypeCombinator.php
1609+
16001610
-
16011611
message: "#^Doing instanceof PHPStan\\\\Type\\\\ArrayType is error\\-prone and deprecated\\. Use Type\\:\\:isArray\\(\\) or Type\\:\\:getArrays\\(\\) instead\\.$#"
16021612
count: 3

src/Type/TypeCombinator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,12 @@ private static function processArrayAccessoryTypes(array $arrayTypes): array
594594
if (!($innerType instanceof AccessoryType) && !($innerType instanceof CallableType)) {
595595
continue;
596596
}
597+
if ($innerType instanceof HasOffsetType) {
598+
$offset = $innerType->getOffsetType();
599+
if ($offset instanceof ConstantStringType || $offset instanceof ConstantIntegerType) {
600+
$innerType = new HasOffsetValueType($offset, $arrayType->getIterableValueType());
601+
}
602+
}
597603
if ($innerType instanceof HasOffsetValueType) {
598604
$accessoryTypes[sprintf('hasOffsetValue(%s)', $innerType->getOffsetType()->describe(VerbosityLevel::cache()))][$i] = $innerType;
599605
continue;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug11518Types;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param mixed[] $a
9+
* @return array{thing: mixed}
10+
* */
11+
function blah(array $a): array
12+
{
13+
if (!array_key_exists('thing', $a)) {
14+
$a['thing'] = 'bla';
15+
assertType('hasOffsetValue(\'thing\', \'bla\')&non-empty-array', $a);
16+
} else {
17+
assertType('array&hasOffset(\'thing\')', $a);
18+
}
19+
20+
assertType('array&hasOffsetValue(\'thing\', mixed)', $a);
21+
22+
return $a;
23+
}

tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,11 @@ public function testBug10732(): void
283283
$this->analyse([__DIR__ . '/data/bug-10732.php'], []);
284284
}
285285

286+
public function testBug11518(): void
287+
{
288+
$this->checkExplicitMixed = true;
289+
$this->checkNullables = true;
290+
$this->analyse([__DIR__ . '/data/bug-11518.php'], []);
291+
}
292+
286293
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug11518;
4+
5+
/**
6+
* @param mixed[] $a
7+
* @return array{thing: mixed}
8+
* */
9+
function blah(array $a): array
10+
{
11+
if (!array_key_exists('thing', $a)) {
12+
$a['thing'] = 'bla';
13+
}
14+
15+
return $a;
16+
}

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ public function dataUnion(): iterable
978978
]),
979979
],
980980
IntersectionType::class,
981-
'array&hasOffset(\'foo\')',
981+
'array&hasOffsetValue(\'foo\', mixed)',
982982
],
983983
[
984984
[
@@ -2568,6 +2568,21 @@ public function dataUnion(): iterable
25682568
ClosureType::class,
25692569
'Closure(): mixed',
25702570
];
2571+
yield [
2572+
[
2573+
new IntersectionType([
2574+
new ArrayType(new MixedType(), new MixedType()),
2575+
new NonEmptyArrayType(),
2576+
new HasOffsetValueType(new ConstantStringType('thing'), new ConstantStringType('bla')),
2577+
]),
2578+
new IntersectionType([
2579+
new ArrayType(new MixedType(), new MixedType()),
2580+
new HasOffsetType(new ConstantStringType('thing')),
2581+
]),
2582+
],
2583+
IntersectionType::class,
2584+
'array&hasOffsetValue(\'thing\', mixed)',
2585+
];
25712586
}
25722587

25732588
/**

0 commit comments

Comments
 (0)