Skip to content

Commit 886ab5c

Browse files
committed
Fix incorrect analyzing of array_shift with non-empty-list property
1 parent 305e592 commit 886ab5c

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,9 @@ static function (): void {
26692669
$isArrayPop ? $arrayArgType->popArray() : $arrayArgType->shiftArray(),
26702670
$isArrayPop ? $arrayArgNativeType->popArray() : $arrayArgNativeType->shiftArray(),
26712671
);
2672+
if ($arrayArg instanceof PropertyFetch || $arrayArg instanceof StaticPropertyFetch) {
2673+
$nodeCallback(new PropertyAssignNode($arrayArg, new TypeExpr($scope->getType($arrayArg)), false), $scope);
2674+
}
26722675
}
26732676

26742677
if (

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,45 @@ public function testBug7824(): void
819819
$this->analyse([__DIR__ . '/data/bug-7824.php'], []);
820820
}
821821

822+
public function testBug13438(): void
823+
{
824+
$this->checkExplicitMixed = true;
825+
$this->analyse([__DIR__ . '/data/bug-13438.php'], [
826+
[
827+
'Property Bug13438\Test::$queue (non-empty-list<int>) does not accept list<int>.',
828+
20,
829+
'list<int> might be empty.',
830+
],
831+
[
832+
'Property Bug13438\Test::$queue (non-empty-list<int>) does not accept list<int>.',
833+
26,
834+
'list<int> might be empty.',
835+
],
836+
]);
837+
}
838+
839+
public function testBug13438b(): void
840+
{
841+
$this->checkExplicitMixed = true;
842+
$this->analyse([__DIR__ . '/data/bug-13438b.php'], [
843+
[
844+
'Property Bug13438b\Test::$queue (non-empty-list<int>) does not accept list<int>.',
845+
20,
846+
'list<int> might be empty.',
847+
],
848+
[
849+
'Property Bug13438b\Test::$queue (non-empty-list<int>) does not accept list<int>.',
850+
26,
851+
'list<int> might be empty.',
852+
],
853+
854+
]);
855+
}
856+
857+
public function testBug13438c(): void
858+
{
859+
$this->checkExplicitMixed = true;
860+
$this->analyse([__DIR__ . '/data/bug-13438c.php'], []);
861+
}
862+
822863
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug13438;
4+
5+
use LogicException;
6+
7+
class Test
8+
{
9+
/**
10+
* @param non-empty-list<int> $queue
11+
*/
12+
public function __construct(
13+
private array $queue,
14+
)
15+
{
16+
}
17+
18+
public function test1(): int
19+
{
20+
return array_shift($this->queue)
21+
?? throw new LogicException('queue is empty');
22+
}
23+
24+
public function test2(): int
25+
{
26+
return array_shift($this->queue);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug13438b;
4+
5+
use LogicException;
6+
7+
class Test
8+
{
9+
/**
10+
* @param non-empty-list<int> $queue
11+
*/
12+
public function __construct(
13+
private array $queue,
14+
)
15+
{
16+
}
17+
18+
public function test1(): int
19+
{
20+
return array_pop($this->queue)
21+
?? throw new LogicException('queue is empty');
22+
}
23+
24+
public function test2(): int
25+
{
26+
return array_pop($this->queue);
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug13438c;
4+
5+
use LogicException;
6+
7+
class Test
8+
{
9+
/**
10+
* @var list<int>
11+
*/
12+
private $queue;
13+
14+
/**
15+
* @param non-empty-list<int> $queue
16+
*/
17+
public function __construct(
18+
array $queue,
19+
)
20+
{
21+
$this->queue = $queue;
22+
}
23+
24+
public function test1(): int
25+
{
26+
return array_shift($this->queue)
27+
?? throw new LogicException('queue is empty');
28+
}
29+
30+
public function test2(): int
31+
{
32+
return array_shift($this->queue);
33+
}
34+
}

0 commit comments

Comments
 (0)