Skip to content

Commit 9a1437f

Browse files
committed
more precise array shape
1 parent eef61d3 commit 9a1437f

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,21 @@ public function specifyTypes(
121121
$this->phpVersion->throwsValueErrorForInternalFunctions()
122122
&& !$arrayType->isArray()->yes()
123123
) {
124+
$specifiedTypes = $this->typeSpecifier->create(
125+
$array,
126+
new HasOffsetType($keyType),
127+
$context,
128+
$scope,
129+
);
130+
124131
$type = new ArrayType(new MixedType(), new MixedType());
125132
$type = $type->unsetOffset($keyType);
126-
$context = $context->negate();
133+
return $specifiedTypes->unionWith($this->typeSpecifier->create(
134+
$array,
135+
$type,
136+
$context->negate(),
137+
$scope,
138+
));
127139
} else {
128140
$type = new HasOffsetType($keyType);
129141
}

tests/PHPStan/Analyser/nsrt/bug-2001-php8.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ public function parseUrl(string $url): string
1616
throw new \RuntimeException('Absolute URLs are prohibited for the redirectTo parameter.');
1717
}
1818

19-
assertType('array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?: string}', $parsedUrl);
19+
assertType('array{scheme?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?: string}', $parsedUrl);
2020

2121
$redirectUrl = $parsedUrl['path'];
2222

2323
if (array_key_exists('query', $parsedUrl)) {
24-
assertType('array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query: string, fragment?: string}', $parsedUrl);
24+
assertType('array{scheme?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query: string, fragment?: string}', $parsedUrl);
2525
$redirectUrl .= '?' . $parsedUrl['query'];
2626
}
2727

2828
if (array_key_exists('fragment', $parsedUrl)) {
29-
assertType('array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment: string}', $parsedUrl);
29+
assertType('array{scheme?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment: string}', $parsedUrl);
3030
$redirectUrl .= '#' . $parsedUrl['query'];
3131
}
3232

33-
assertType('array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?: string}', $parsedUrl);
33+
assertType('array{scheme?: string, port?: int<0, 65535>, user?: string, pass?: string, path?: string, query?: string, fragment?: string}', $parsedUrl);
3434

3535
return $redirectUrl;
3636
}

0 commit comments

Comments
 (0)