Skip to content
Prev Previous commit
Next Next commit
fix build
  • Loading branch information
staabm committed Dec 9, 2023
commit 94247c71408348abfe3c8c6f0e01df90b7c59bcc
11 changes: 4 additions & 7 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,7 @@ public function specifyTypesInCondition(
$offsetType = $type->getOffsetValueType($dimType);
$isNullable = !$offsetType->isNull()->no();

$setOffset = function (Type $outerType, Type $dimType, bool $optional): Type {
return TypeTraverser::map($outerType, static function (Type $type, callable $traverse) use ($dimType, $optional): Type {
$setOffset = static fn (Type $outerType, Type $dimType, bool $optional): Type => TypeTraverser::map($outerType, static function (Type $type, callable $traverse) use ($dimType, $optional): Type {
if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $traverse($type);
}
Expand All @@ -709,17 +708,15 @@ public function specifyTypesInCondition(
new NullType(),
$optional,
);
$buildType = $builder->getArray();
return $buildType;
return $builder->getArray();
}

return $type;
});
};
});

if ($hasOffset === true) {
if ($isNullable) {
$specifiedType = $this->create(
$specifiedType = $this->create(
$issetExpr->var,
$setOffset($type, $dimType, !$scope->hasExpressionType($issetExpr->var)->yes()),
$context->negate(),
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/tagged-unions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function doFoo4(array $foo)
if (isset($foo['C'])) {
assertType("array{A: string, C: 1}", $foo);
} else {
assertType("array{A: int, B: 1}", $foo);
assertType("array{A: int, B: 1}|array{A: string, C: 1}", $foo); // could be array{A: int, B: 1}
}

assertType('array{A: int, B: 1}|array{A: string, C: 1}', $foo);
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testRule(): void
67,
],
[
'Offset \'dim-null-not-set\' on array{dim: 1, dim-null: 1|null, dim-null-offset: array{a: true|null}, dim-empty: array{}} in isset() does not exist.',
'Offset \'dim-null-not-set\' on array{dim: 1, dim-null-offset: array{a: true|null}, dim-empty: array{}, dim-null: 1|null} in isset() does not exist.',
73,
],
[
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testRuleWithoutTreatPhpDocTypesAsCertain(): void
67,
],
[
'Offset \'dim-null-not-set\' on array{dim: 1, dim-null: 1|null, dim-null-offset: array{a: true|null}, dim-empty: array{}} in isset() does not exist.',
'Offset \'dim-null-not-set\' on array{dim: 1, dim-null-offset: array{a: true|null}, dim-empty: array{}, dim-null: 1|null} in isset() does not exist.',
73,
],
[
Expand Down