Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make $http_response_header a non-empty-list #9001

Merged
merged 5 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,12 @@ private static function getGlobalTypeInner(string $var_id, bool $files_full_path
}

if ($var_id === '$http_response_header') {
return Type::getList(Type::getNonEmptyString());
// $http_response_header exists only in the local scope after a successful network request
return new Union([
Type::getNonEmptyListAtomic(Type::getNonFalsyString())
], [
'possibly_undefined' => true,
]);
}

if ($var_id === '$GLOBALS') {
Expand Down
20 changes: 19 additions & 1 deletion tests/SuperGlobalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

namespace Psalm\Tests;

use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

class SuperGlobalsTest extends TestCase
{
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;

public function providerValidCodeParse(): iterable
{
yield 'http_response_headerIsList' => [
'code' => '<?php
/** @return list<string> */
/** @return non-empty-list<non-falsy-string> */
function returnsList(): array {
if (!isset($http_response_header)) {
throw new \RuntimeException();
}
return $http_response_header;
}
',
Expand All @@ -29,4 +34,17 @@ function f(): array {
'
];
}

public function providerInvalidCodeParse(): iterable
{
yield 'undefined http_response_header' => [
'code' => '<?php
/** @return non-empty-list<non-falsy-string> */
function returnsList(): array {
return $http_response_header;
}
',
'error_message' => 'InvalidReturnStatement',
];
}
}