Skip to content

Commit bed1737

Browse files
committed
NodeScopeResolver: do not reassign objects as arrays on ArrayDimFetch
this fixes phpstan/phpstan#3782. I'm not sure if this may have any side effects, but I can't see any circumstances where it makes sense to reassign an object as an array on array dim fetch.
1 parent 04de69a commit bed1737

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
use PHPStan\Type\MixedType;
9898
use PHPStan\Type\NullType;
9999
use PHPStan\Type\ObjectType;
100+
use PHPStan\Type\ObjectWithoutClassType;
100101
use PHPStan\Type\StaticType;
101102
use PHPStan\Type\StringType;
102103
use PHPStan\Type\Type;
@@ -2379,7 +2380,7 @@ private function processAssignVar(
23792380
$scope = $result->getScope();
23802381

23812382
$varType = $scope->getType($var);
2382-
if (!(new ObjectType(\ArrayAccess::class))->isSuperTypeOf($varType)->yes()) {
2383+
if (!(new ObjectWithoutClassType())->isSuperTypeOf($varType)->yes()) {
23832384
// 4. compose types
23842385
if ($varType instanceof ErrorType) {
23852386
$varType = new ConstantArrayType([], []);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10048,6 +10048,11 @@ public function dataBug2899(): array
1004810048
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2899.php');
1004910049
}
1005010050

10051+
public function dataBug3782(): array
10052+
{
10053+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3782.php');
10054+
}
10055+
1005110056
/**
1005210057
* @dataProvider dataBug2574
1005310058
* @dataProvider dataBug2577
@@ -10116,6 +10121,7 @@ public function dataBug2899(): array
1011610121
* @dataProvider dataBug3133
1011710122
* @dataProvider dataBug2550
1011810123
* @dataProvider dataBug2899
10124+
* @dataProvider dataBug3782
1011910125
* @param string $assertType
1012010126
* @param string $file
1012110127
* @param mixed ...$args
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug3782;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class HelloWorld
8+
{
9+
/** @param mixed[] $data */
10+
public function sayHello(array $data): void
11+
{
12+
foreach($data as $key => $value){
13+
$this[$key] = $value;
14+
assertType('$this(Bug3782\\HelloWorld)', $this);
15+
}
16+
}
17+
18+
public static function sayHello2(array $data): void
19+
{
20+
$var = new HelloWorld();
21+
foreach ($data as $key => $value){
22+
$var[$key] = $value;
23+
assertType('Bug3782\\HelloWorld', $var);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)