Repository navigation
Report deprecated $http_response_header on PHP 8.5 - #217
Gabrieljr42 wants to merge 4 commits into
Conversation
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; |
There was a problem hiding this comment.
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.
}?
There was a problem hiding this comment.
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.
| if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) { | ||
| return []; | ||
| } |
There was a problem hiding this comment.
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
|
@VincentLanglet or @SanderMuller do you have a ideas on how we could approach this thing differently? |
|
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 |
|
I think a change in the Scope cannot match what PHP reports. PHP 8.5 emits this deprecation at compile time, in I checked this with 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; } // deprecatedA marker for the origin would miss 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 Two smaller points:
|
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. that way we would only decide in phpstan-src when the variable will be implied and react to that fact in this PR here.
|
|
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 |
|
Agreed, the marker covers the case that matters. 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; } // sameThe marker gets a false positive only when the function also writes the variable before the read in source order, as in |
|
Makes sense, doing this in Before I open a PR on Is |
Why
PHP 8.5 deprecates the predefined
$http_response_headervariable and tells callers to usehttp_get_last_response_headers()instead. WithphpVersionset 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_headerwhen 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 stringhttp_response_header.Scope
Reads of that variable. The rule is registered for every project that includes this extension, and it follows the
phpVersionsetting 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_headerthat PHP itself would deprecate.Verification
php vendor/bin/phpunit tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php— OK (2 tests, 2 assertions). The test constructsPhpVersionfor 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=rawon the rule, the fetch visitor, and the test — no errors.php vendor/bin/parallel-linton the changed PHP files — no syntax errors.The full CI matrix and the coding-standard job were not run.
Fixes #173