Skip to content

Commit 5ba5a86

Browse files
herndlmondrejmirtes
authored andcommitted
Handle empty array case via ConstantArrayType::spliceArray()
1 parent 2247f0c commit 5ba5a86

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,9 @@ public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $pre
10291029
public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
10301030
{
10311031
$keyTypesCount = count($this->keyTypes);
1032+
if ($keyTypesCount === 0) {
1033+
return $this;
1034+
}
10321035

10331036
$offset = $offsetType instanceof ConstantIntegerType ? $offsetType->getValue() : null;
10341037

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,13 @@ public function testBug12095(): void
14931493
$this->assertNoErrors($errors);
14941494
}
14951495

1496+
public function testBug13279(): void
1497+
{
1498+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-13279.php');
1499+
$this->assertCount(1, $errors);
1500+
$this->assertSame('Parameter #2 $offset of function array_splice expects int, string given.', $errors[0]->getMessage());
1501+
}
1502+
14961503
/**
14971504
* @param string[]|null $allAnalysedFiles
14981505
* @return Error[]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13279;
4+
5+
class ReproduceMe
6+
{
7+
public function crashIt(string $addMe = ''): string
8+
{
9+
$arg = [];
10+
if ($addMe !== '') {
11+
$arg[] = $addMe;
12+
}
13+
14+
array_splice($arg, 'extra', 1);
15+
16+
return implode(' ', $arg);
17+
}
18+
}

0 commit comments

Comments
 (0)