Skip to content

Commit 3228f58

Browse files
committed
Merge remote-tracking branch 'origin/1.12.x' into 2.1.x
2 parents 781aefa + fe595cb commit 3228f58

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/Type/ArrayType.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,17 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
293293
if ($isKeyTypeInteger->no()) {
294294
$offsetType = new IntegerType();
295295
} elseif ($isKeyTypeInteger->yes()) {
296-
$offsetType = $this->keyType;
296+
/** @var list<ConstantIntegerType> $constantScalars */
297+
$constantScalars = $this->keyType->getConstantScalarTypes();
298+
if (count($constantScalars) > 0) {
299+
foreach ($constantScalars as $constantScalar) {
300+
$constantScalars[] = ConstantTypeHelper::getTypeFromValue($constantScalar->getValue() + 1);
301+
}
302+
303+
$offsetType = TypeCombinator::union(...$constantScalars);
304+
} else {
305+
$offsetType = $this->keyType;
306+
}
297307
} else {
298308
$integerTypes = [];
299309
TypeTraverser::map($this->keyType, static function (Type $type, callable $traverse) use (&$integerTypes): Type {

tests/PHPStan/Rules/Variables/IssetRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,11 @@ public function testVirtualProperty(): void
464464
]);
465465
}
466466

467+
public function testBug9328(): void
468+
{
469+
$this->treatPhpDocTypesAsCertain = true;
470+
471+
$this->analyse([__DIR__ . '/data/bug-9328.php'], []);
472+
}
473+
467474
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Bug9328;
4+
5+
function (): void {
6+
$contents = file('foo.ini', FILE_IGNORE_NEW_LINES);
7+
if (false === $contents) {
8+
throw new \Exception('Failed to read file');
9+
}
10+
// Add section so that the last one is flushed:
11+
$contents[] = '[--]';
12+
$currentSection = '';
13+
$sections = [];
14+
$lines = [];
15+
foreach ($contents as $line) {
16+
if (
17+
str_starts_with($line, '[')
18+
&& str_ends_with($line, ']')
19+
&& strlen($line) > 2
20+
) {
21+
// flush previously collected section:
22+
if ($lines) {
23+
$sections[] = [
24+
'name' => $currentSection,
25+
'lines' => $lines,
26+
];
27+
}
28+
$currentSection = substr($line, 1, -1);
29+
$lines = [];
30+
}
31+
$lines[] = $line;
32+
}
33+
34+
if (isset($sections[1])) {
35+
echo "We have multiple remaining sections!\n";
36+
}
37+
};

0 commit comments

Comments
 (0)