Skip to content

Commit

Permalink
Merge pull request #9001 from fluffycondor/http_response_header-non-e…
Browse files Browse the repository at this point in the history
…mpty-list

Make `$http_response_header` a non-empty-list
  • Loading branch information
orklah committed Dec 27, 2022
2 parents 9892ef2 + bfc0005 commit 41ae518
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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
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',
];
}
}

0 comments on commit 41ae518

Please sign in to comment.