Skip to content

Report deprecated $http_response_header on PHP 8.5 - #217

Open
Gabrieljr42 wants to merge 4 commits into
phpstan:2.0.xfrom
Gabrieljr42:fix/http-response-header-173
Open

Gabrieljr42 wants to merge 4 commits into
phpstan:2.0.xfrom
Gabrieljr42:fix/http-response-header-173

Conversation

@Gabrieljr42

@Gabrieljr42 Gabrieljr42 commented Nov 26, 2026 •

Copy link
Copy Markdown

Why

PHP 8.5 deprecates the predefined $http_response_header variable and tells callers to use http_get_last_response_headers() instead. With phpVersion set to 8.5, this extension still stays silent on that variable, so the usage is easy to miss until runtime.

What changed

A rule reports a read of $http_response_header when the configured PHP version is 8.5 or newer. The message matches PHP's own deprecation text. A write in the same scope, including a parameter of that name, marks the name as assigned, so a later read in that scope is not reported. isset, empty, unset, and the left side of ?? are not reported and do not count as a write. Uses inside an already deprecated scope stay unreported. A variable variable is ignored, because its name is not the string http_response_header.

Scope

Reads of that variable. The rule is registered for every project that includes this extension, and it follows the phpVersion setting rather than the PHP binary running PHPStan.

A by-reference function argument is not modeled, because whether PHP warns depends on the callee signature. A dynamic name such as ${'http_response_header'} is not reported.

Blast Radius

Projects on PHP 8.4 and older see no new errors. Projects on PHP 8.5 and newer gain one deprecation identifier, variable.deprecated, at each read of $http_response_header that PHP itself would deprecate.

Verification

php vendor/bin/phpunit tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php — OK (2 tests, 2 assertions). The test constructs PhpVersion for 8.5 and 8.4, so the result does not depend on the PHP binary version.

php vendor/bin/phpstan analyse --no-progress --error-format=raw on the rule, the fetch visitor, and the test — no errors.

php vendor/bin/parallel-lint on the changed PHP files — no syntax errors.

The full CI matrix and the coding-standard job were not run.

Fixes #173

PHP 8.5 warns only when that name is read in a scope that has not already assigned it. A parameter of that name counts as an assignment.
{
// A foreach value is compiled as a read, but PHPStan does not visit
// that variable on its own. The foreach statement is visited.
return Node::class;

@staabm staabm Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of hooking onto all Node-types, can we take inspiration from
https://github.com/phpstan/phpstan-src/blob/2.3.x/src/Rules/Variables/DefinedVariableRule.php#L90
which listens on Variable::class and skips the write and undefined-allowed cases:

  if ($scope->isInExpressionAssign($node) ||
  $scope->isUndefinedExpressionAllowed($node)) {
      return []; // write position, or isset/??/etc.
  }

?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the rule to listen on Variable::class and skip writes / isset / ?? with $scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node). That let me drop the silent-fetch and in-place checks from the visitor.

I kept the smaller visitor for shadowing because PHPStan assigns $http_response_header in scope after file_get_contents() / fopen(), so $scope->hasVariableType() alone cannot distinguish the predefined variable from a user assignment. Because PHPStan doesn't visit simple foreach value variables on their own, foreach ($list as $http_response_header) now reports on reads in the loop body instead of the foreach line itself.

Comment on lines +39 to +41
if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) {
return [];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still try to think about ways how we can prevent using the *Visitor class.

not sure its a good idea, but maybe we can utilize this if case here to track which variables have been assigned, so we know whether http_response_header was magically implied by the engine, or whether it was assigned explicitly and we should skip over

@staabm

staabm commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@VincentLanglet or @SanderMuller do you have a ideas on how we could approach this thing differently?

@VincentLanglet

Copy link
Copy Markdown

I'm affraid a too complex solution would give false positive/negative.

Looking at https://phpstan.org/r/e2dad966-7a02-4f59-a8f4-64fb1be91ebc

I feel like a PR on PHPStan could help
Could we just not hydrate the scope with $http_response_header ?
Or mark the origin of $http_response_header in order to report it here ?

@SanderMuller

Copy link
Copy Markdown
Contributor

I think a change in the Scope cannot match what PHP reports. PHP 8.5 emits this deprecation at compile time, in zend_try_compile_cv(). Each function has a flag. A plain read of $http_response_header reports the deprecation while the flag is off. The first write or a parameter with that name sets it. The check follows compile order, not control flow, and fopen() has no part in it.

I checked this with php -l on 8.5:

function a() { return $http_response_header; } // deprecated
function b() { file_get_contents('x'); if (false) { $http_response_header = []; } return $http_response_header; } // not deprecated
function c() { while (true) { echo $http_response_header; $http_response_header = []; } } // deprecated
function d() { if (PHP_VERSION_ID < 80500) { return $http_response_header; } return 1; } // deprecated

A marker for the origin would miss a() and c(), because no fopen() comes before the read. In b() the marker survives the dead branch, so the rule would report a read that PHP does not report.

So I would keep a walk in compile order, like the visitor does now, but run it from the rule. The rule can listen on the function-like nodes and on FileNode for the top level. It walks each body once and skips nested closures and functions, because those have their own flag. It can return early when the PHP version cannot be 8.5. Projects on older versions then skip the walk, and the rich parser does not run one more visitor on every file.

Two smaller points:

  • PHP_VERSION_ID guards do not prevent the deprecation. On 8.5, bug-173-php-version.php reports lines 8, 12 and 15. The 8.5 test expects 8 and 15, and the 8.4–8.5 test expects only 8.
  • http_get_last_response_headers() exists since PHP 8.4. So the fix also works for a project on 8.4–8.5, and I would report when the range includes 8.5.

@staabm

staabm commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Or mark the origin of $http_response_header in order to report it here ?

this sounds to me like the way forward.

I think we could define a new VirtualNode-Type implementing NodeType in PHPStan-src and let it run thru the node-callback (look for e.g. NoopExpressionNode for inspiration).

that way we would only decide in phpstan-src when the variable will be implied and react to that fact in this PR here.

I think a change in the Scope cannot match what PHP reports. PHP 8.5 emits this deprecation at compile time, in zend_try_compile_cv(). Each function has a flag. A plain read of $http_response_header reports the deprecation while the flag is off. The first write or a parameter with that name sets it. The check follows compile order, not control flow, and fopen() has no part in it.

$http_response_header is a rarely used mechanism and I feel we shouldn't overthink the solution.
I think its good enough to emit the error in the cases in which phpstan-src implies the variable to exist. we don't need 1:1 mimic the cases in which php-src emits the deprecation.

@VincentLanglet

Copy link
Copy Markdown

Looking at https://phpstan.org/r/6689aba7-38d6-48be-8a90-577ac89124a6

We just need to report the b situation, cause others are already reported as "not be defined" anyway

@SanderMuller

Copy link
Copy Markdown
Contributor

Agreed, the marker covers the case that matters. a(), c() and d() already get variable.undefined, so the gap is a read that PHPStan defines through file_get_contents():

function e(): array { file_get_contents('x'); return $http_response_header; } // deprecated on 8.5, PHPStan reports nothing
function g(): array { file_get_contents('x'); $h = $http_response_header; $http_response_header = []; return $h; } // same

The marker gets a false positive only when the function also writes the variable before the read in source order, as in b(). PHP does not report that case. I think that is rare enough to accept.

@Gabrieljr42

Copy link
Copy Markdown
Author

Makes sense, doing this in phpstan-src is much simpler and lets me drop HttpResponseHeaderFetchVisitor here.

Before I open a PR on phpstan-src (2.3.x), I was thinking of tracking in MutatingScope when $http_response_header was implicitly assigned by FuncCallScopeEffectsHelper (fopen / file_get_contents), clearing that marker on an explicit write, and emitting a new VirtualNode when a non-write read hits it (skipping isset / ??, and updating turbo-ext too if needed). Then this PR would just listen on that node and report when the configured PHP version range includes 8.5, since http_get_last_response_headers() is available from 8.4 as @SanderMuller noted.

Is 2.3.x the right target branch for phpstan-src, and do you have a name in mind for the VirtualNode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$http_response_header is deprecated in PHP 8.5

4 participants