Skip to content

Commit 76702eb

Browse files
Remove some ErrorType check
1 parent ef9fa11 commit 76702eb

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5628,10 +5628,9 @@ private function processAssignVar(
56285628
$varNativeType = $scope->getNativeType($var);
56295629

56305630
// 4. compose types
5631-
if ($varType instanceof ErrorType) {
5631+
$isImplicitArrayCreation = $this->isImplicitArrayCreation($dimFetchStack, $scope);
5632+
if ($isImplicitArrayCreation->yes()) {
56325633
$varType = new ConstantArrayType([], []);
5633-
}
5634-
if ($varNativeType instanceof ErrorType) {
56355634
$varNativeType = new ConstantArrayType([], []);
56365635
}
56375636
$offsetValueType = $varType;
@@ -6018,6 +6017,27 @@ static function (): void {
60186017
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
60196018
}
60206019

6020+
/**
6021+
* @param list<ArrayDimFetch> $dimFetchStack
6022+
*/
6023+
private function isImplicitArrayCreation(array $dimFetchStack, Scope $scope): TrinaryLogic
6024+
{
6025+
if (count($dimFetchStack) === 0) {
6026+
return TrinaryLogic::createNo();
6027+
}
6028+
6029+
$varNode = $dimFetchStack[0]->var;
6030+
if (!$varNode instanceof Variable) {
6031+
return TrinaryLogic::createNo();
6032+
}
6033+
6034+
if (!is_string($varNode->name)) {
6035+
return TrinaryLogic::createNo();
6036+
}
6037+
6038+
return $scope->hasVariableType($varNode->name)->negate();
6039+
}
6040+
60216041
/**
60226042
* @param list<ArrayDimFetch> $dimFetchStack
60236043
* @param list<Type|null> $offsetTypes
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12447Bis;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface Foo
8+
{
9+
10+
}
11+
12+
class HelloWorld
13+
{
14+
15+
public function __invoke(Foo $foo): void
16+
{
17+
$a = $foo->doFoo();
18+
assertType('*ERROR*', $a);
19+
$a[] = 5;
20+
assertType('mixed', $a);
21+
}
22+
}

0 commit comments

Comments
 (0)