Skip to content

Commit 6276ce1

Browse files
committed
array_shift et al. have side effects
Closes phpstan/phpstan#8084
1 parent 50f2a08 commit 6276ce1

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

bin/functionMetadata_original.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
'array_merge' => ['hasSideEffects' => false],
3232
'array_merge_recursive' => ['hasSideEffects' => false],
3333
'array_pad' => ['hasSideEffects' => false],
34+
'array_pop' => ['hasSideEffects' => true],
3435
'array_product' => ['hasSideEffects' => false],
36+
'array_push' => ['hasSideEffects' => true],
3537
'array_rand' => ['hasSideEffects' => false],
3638
'array_replace' => ['hasSideEffects' => false],
3739
'array_replace_recursive' => ['hasSideEffects' => false],
3840
'array_reverse' => ['hasSideEffects' => false],
41+
'array_shift' => ['hasSideEffects' => true],
3942
'array_slice' => ['hasSideEffects' => false],
4043
'array_sum' => ['hasSideEffects' => false],
4144
'array_udiff' => ['hasSideEffects' => false],
@@ -45,6 +48,7 @@
4548
'array_uintersect_assoc' => ['hasSideEffects' => false],
4649
'array_uintersect_uassoc' => ['hasSideEffects' => false],
4750
'array_unique' => ['hasSideEffects' => false],
51+
'array_unshift' => ['hasSideEffects' => true],
4852
'array_values' => ['hasSideEffects' => false],
4953
'asin' => ['hasSideEffects' => false],
5054
'asinh' => ['hasSideEffects' => false],

resources/functionMetadata.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,15 @@
696696
'array_merge' => ['hasSideEffects' => false],
697697
'array_merge_recursive' => ['hasSideEffects' => false],
698698
'array_pad' => ['hasSideEffects' => false],
699+
'array_pop' => ['hasSideEffects' => true],
699700
'array_product' => ['hasSideEffects' => false],
701+
'array_push' => ['hasSideEffects' => true],
700702
'array_rand' => ['hasSideEffects' => false],
701703
'array_replace' => ['hasSideEffects' => false],
702704
'array_replace_recursive' => ['hasSideEffects' => false],
703705
'array_reverse' => ['hasSideEffects' => false],
704706
'array_search' => ['hasSideEffects' => false],
707+
'array_shift' => ['hasSideEffects' => true],
705708
'array_slice' => ['hasSideEffects' => false],
706709
'array_sum' => ['hasSideEffects' => false],
707710
'array_udiff' => ['hasSideEffects' => false],
@@ -711,6 +714,7 @@
711714
'array_uintersect_assoc' => ['hasSideEffects' => false],
712715
'array_uintersect_uassoc' => ['hasSideEffects' => false],
713716
'array_unique' => ['hasSideEffects' => false],
717+
'array_unshift' => ['hasSideEffects' => true],
714718
'array_values' => ['hasSideEffects' => false],
715719
'asin' => ['hasSideEffects' => false],
716720
'asinh' => ['hasSideEffects' => false],

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ public function dataFileAsserts(): iterable
11711171
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8635.php');
11721172
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8625.php');
11731173
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8621.php');
1174+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8084.php');
11741175
}
11751176

11761177
/**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug8084b;
4+
5+
use Exception;
6+
use function array_shift;
7+
use function PHPStan\Testing\assertType;
8+
9+
class Bug8084
10+
{
11+
/**
12+
* @param array{a: 0, b?: 1} $arr
13+
* @throws Exception
14+
*/
15+
public function run(array $arr): void
16+
{
17+
assertType('0', array_shift($arr) ?? throw new Exception());
18+
assertType('1|null', array_shift($arr));
19+
}
20+
}

tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,12 @@ public function testBug7968(): void
360360
$this->analyse([__DIR__ . '/data/bug-7968.php'], []);
361361
}
362362

363+
public function testBug8084(): void
364+
{
365+
$this->treatPhpDocTypesAsCertain = true;
366+
$this->strictUnnecessaryNullsafePropertyFetch = true;
367+
368+
$this->analyse([__DIR__ . '/data/bug-8084.php'], []);
369+
}
370+
363371
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug8084a;
4+
5+
use Exception;
6+
use function array_shift;
7+
use function PHPStan\Testing\assertType;
8+
9+
class Bug8084
10+
{
11+
/**
12+
* @param string[] $params
13+
*/
14+
public function run(array $params): void
15+
{
16+
$a = array_shift($params) ?? throw new Exception();
17+
$b = array_shift($params) ?? "default_b";
18+
}
19+
}

0 commit comments

Comments
 (0)