From fbd68e89a6e614e787f38eb42eddbd0e5b0919d3 Mon Sep 17 00:00:00 2001 From: Gabrieljr42 Date: Thu, 26 Nov 2026 16:42:54 -0300 Subject: [PATCH 1/4] Report deprecated $http_response_header on PHP 8.5 Fixes #173 --- rules.neon | 1 + ...precatedHttpResponseHeaderVariableRule.php | 60 +++++++++++++++++++ ...atedHttpResponseHeaderVariableRuleTest.php | 50 ++++++++++++++++ tests/Rules/Deprecations/data/bug-173.php | 34 +++++++++++ 4 files changed, 145 insertions(+) create mode 100644 src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php create mode 100644 tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php create mode 100644 tests/Rules/Deprecations/data/bug-173.php diff --git a/rules.neon b/rules.neon index aa4a8a0..d4b328d 100644 --- a/rules.neon +++ b/rules.neon @@ -45,6 +45,7 @@ services: rules: - PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule + - PHPStan\Rules\Deprecations\ReadingDeprecatedHttpResponseHeaderVariableRule conditionalTags: PHPStan\Rules\Deprecations\CallWithDeprecatedIniOptionRule: diff --git a/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php new file mode 100644 index 0000000..3b2a0d8 --- /dev/null +++ b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php @@ -0,0 +1,60 @@ + + */ +class ReadingDeprecatedHttpResponseHeaderVariableRule implements Rule +{ + + private const PHP_8_5 = 80500; + + private DeprecatedScopeHelper $deprecatedScopeHelper; + + private PhpVersion $phpVersion; + + public function __construct( + DeprecatedScopeHelper $deprecatedScopeHelper, + PhpVersion $phpVersion + ) + { + $this->deprecatedScopeHelper = $deprecatedScopeHelper; + $this->phpVersion = $phpVersion; + } + + public function getNodeType(): string + { + return Variable::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { + return []; + } + + if ($this->phpVersion->getVersionId() < self::PHP_8_5) { + return []; + } + + if (!is_string($node->name) || $node->name !== 'http_response_header') { + return []; + } + + return [ + RuleErrorBuilder::message( + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + )->identifier('variable.deprecated')->build(), + ]; + } + +} diff --git a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php new file mode 100644 index 0000000..ebe9a5f --- /dev/null +++ b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php @@ -0,0 +1,50 @@ + + */ +class ReadingDeprecatedHttpResponseHeaderVariableRuleTest extends RuleTestCase +{ + + private int $phpVersionId = 80500; + + protected function getRule(): Rule + { + return new ReadingDeprecatedHttpResponseHeaderVariableRule( + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]), + new PhpVersion($this->phpVersionId, PhpVersion::SOURCE_CONFIG), + ); + } + + public function testPhp85(): void + { + $this->phpVersionId = 80500; + $this->analyse([__DIR__ . '/data/bug-173.php'], [ + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 7, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 12, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 17, + ], + ]); + } + + public function testPhp84(): void + { + $this->phpVersionId = 80400; + $this->analyse([__DIR__ . '/data/bug-173.php'], []); + } + +} diff --git a/tests/Rules/Deprecations/data/bug-173.php b/tests/Rules/Deprecations/data/bug-173.php new file mode 100644 index 0000000..ebe005e --- /dev/null +++ b/tests/Rules/Deprecations/data/bug-173.php @@ -0,0 +1,34 @@ + Date: Thu, 26 Nov 2026 17:16:08 -0300 Subject: [PATCH 2/4] Do not report writes or shadowed $http_response_header 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. --- rules.neon | 5 + .../HttpResponseHeaderFetchVisitor.php | 283 ++++++++++++++++++ ...precatedHttpResponseHeaderVariableRule.php | 19 +- ...atedHttpResponseHeaderVariableRuleTest.php | 38 ++- tests/Rules/Deprecations/data/bug-173.php | 74 ++++- 5 files changed, 415 insertions(+), 4 deletions(-) create mode 100644 src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php diff --git a/rules.neon b/rules.neon index d4b328d..f9e770b 100644 --- a/rules.neon +++ b/rules.neon @@ -43,6 +43,11 @@ services: tags: - phpstan.restrictedClassNameUsageExtension + - + class: PHPStan\Rules\Deprecations\HttpResponseHeaderFetchVisitor + tags: + - phpstan.parser.richParserNodeVisitor + rules: - PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule - PHPStan\Rules\Deprecations\ReadingDeprecatedHttpResponseHeaderVariableRule diff --git a/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php new file mode 100644 index 0000000..1dbf913 --- /dev/null +++ b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php @@ -0,0 +1,283 @@ +assignedStack = [false]; + $this->nodeStack = []; + + return null; + } + + /** + * @param Node[] $nodes + */ + public function afterTraverse(array $nodes) + { + $this->assignedStack = []; + $this->nodeStack = []; + + return null; + } + + public function enterNode(Node $node) + { + if ($node instanceof FunctionLike) { + $this->assignedStack[] = $this->parametersShadowTheVariable($node); + } + + if ($node instanceof Variable && is_string($node->name) && $node->name === 'http_response_header') { + $this->classifyRead($node); + } + + $this->nodeStack[] = $node; + + return null; + } + + public function leaveNode(Node $node) + { + if ( + ($node instanceof Assign || $node instanceof AssignRef) + && ($node->var instanceof Array_ || $node->var instanceof List_) + && $this->subtreeContainsName($node->var) + ) { + // list() compiles the right-hand side before the writes. + $this->markAssigned(); + } + + if ($node instanceof AssignCoalesce && $this->subtreeContainsName($node->var)) { + // ??= reads with BP_VAR_IS, evaluates the default, then writes. + $this->markAssigned(); + } + + if ($node instanceof FunctionLike) { + array_pop($this->assignedStack); + } + + array_pop($this->nodeStack); + + return null; + } + + private function classifyRead(Variable $node): void + { + $write = $this->enclosingWrite($node); + if ($write === 'assign' && !$this->listAssignment()) { + // A simple or dim write is compiled before the right-hand side. + $this->markAssigned(); + return; + } + if ($write === 'assignRefRhs' || $write === 'foreachKey') { + $this->markAssigned(); + return; + } + if ($write === 'assign' || $this->isSilentFetch($node) || $this->isInPlaceTarget($node) || $this->isAssigned()) { + return; + } + + $node->setAttribute(self::ATTRIBUTE_NAME, self::FETCH_READ); + } + + private function parametersShadowTheVariable(FunctionLike $function): bool + { + foreach ($function->getParams() as $param) { + if ($param->var instanceof Variable && $param->var->name === 'http_response_header') { + return true; + } + } + + return false; + } + + private function enclosingWrite(Variable $node): ?string + { + $child = $node; + for ($i = count($this->nodeStack) - 1; $i >= 0; $i--) { + $parent = $this->nodeStack[$i]; + if ($parent instanceof FunctionLike) { + return null; + } + if ($parent instanceof Assign || $parent instanceof AssignRef) { + if ($parent->var === $child) { + return 'assign'; + } + if ($parent instanceof AssignRef && $parent->expr === $child) { + return 'assignRefRhs'; + } + + return null; + } + if ($parent instanceof Foreach_) { + if ($parent->keyVar === $child) { + return 'foreachKey'; + } + + return null; + } + + $child = $parent; + } + + return null; + } + + private function listAssignment(): bool + { + for ($i = count($this->nodeStack) - 1; $i >= 0; $i--) { + $parent = $this->nodeStack[$i]; + if ($parent instanceof Assign || $parent instanceof AssignRef) { + return $parent->var instanceof Array_ || $parent->var instanceof List_; + } + } + + return false; + } + + private function isSilentFetch(Variable $node): bool + { + $child = $node; + for ($i = count($this->nodeStack) - 1; $i >= 0; $i--) { + $parent = $this->nodeStack[$i]; + if ($parent instanceof FunctionLike) { + return false; + } + if ($parent instanceof Isset_ || $parent instanceof Empty_ || $parent instanceof Unset_) { + return true; + } + if ($parent instanceof Coalesce && ($parent->left === $child || $this->isDimBase($parent->left, $child))) { + return true; + } + + $child = $parent; + } + + return false; + } + + private function isInPlaceTarget(Variable $node): bool + { + $child = $node; + for ($i = count($this->nodeStack) - 1; $i >= 0; $i--) { + $parent = $this->nodeStack[$i]; + if ($parent instanceof FunctionLike) { + return false; + } + if ( + ($parent instanceof PreInc || $parent instanceof PostInc || $parent instanceof PreDec || $parent instanceof PostDec || $parent instanceof AssignOp) + && ($parent->var === $child || $this->isDimBase($parent->var, $child)) + ) { + return true; + } + + $child = $parent; + } + + return false; + } + + private function isDimBase(Node $root, Node $child): bool + { + $current = $root; + while ($current instanceof ArrayDimFetch) { + if ($current === $child) { + return true; + } + $current = $current->var; + } + + return $current === $child; + } + + private function subtreeContainsName(Node $node): bool + { + if ($node instanceof Variable && $node->name === 'http_response_header') { + return true; + } + + foreach ($node->getSubNodeNames() as $subName) { + $subNode = $node->{$subName}; + if ($subNode instanceof Node && $this->subtreeContainsName($subNode)) { + return true; + } + if (!is_array($subNode)) { + continue; + } + foreach ($subNode as $item) { + if ($item instanceof Node && $this->subtreeContainsName($item)) { + return true; + } + } + } + + return false; + } + + private function markAssigned(): void + { + $last = count($this->assignedStack) - 1; + if ($last < 0) { + return; + } + + $this->assignedStack[$last] = true; + } + + private function isAssigned(): bool + { + $last = count($this->assignedStack) - 1; + + return $last >= 0 && $this->assignedStack[$last]; + } + +} diff --git a/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php index 3b2a0d8..b93b57b 100644 --- a/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php +++ b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php @@ -4,6 +4,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt\Foreach_; use PHPStan\Analyser\Scope; use PHPStan\Php\PhpVersion; use PHPStan\Rules\Rule; @@ -11,7 +12,7 @@ use function is_string; /** - * @implements Rule + * @implements Rule */ class ReadingDeprecatedHttpResponseHeaderVariableRule implements Rule { @@ -33,11 +34,21 @@ public function __construct( public function getNodeType(): string { - return Variable::class; + // 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; } public function processNode(Node $node, Scope $scope): array { + if ($node instanceof Foreach_) { + $node = $node->valueVar; + } + + if (!$node instanceof Variable) { + return []; + } + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } @@ -50,6 +61,10 @@ public function processNode(Node $node, Scope $scope): array return []; } + if ($node->getAttribute(HttpResponseHeaderFetchVisitor::ATTRIBUTE_NAME) !== HttpResponseHeaderFetchVisitor::FETCH_READ) { + return []; + } + return [ RuleErrorBuilder::message( 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', diff --git a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php index ebe9a5f..1012f0a 100644 --- a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php +++ b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php @@ -36,7 +36,35 @@ public function testPhp85(): void ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 17, + 51, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 57, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 70, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 76, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 77, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 87, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 93, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 105, ], ]); } @@ -47,4 +75,12 @@ public function testPhp84(): void $this->analyse([__DIR__ . '/data/bug-173.php'], []); } + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../../rules.neon', + ...parent::getAdditionalConfigFiles(), + ]; + } + } diff --git a/tests/Rules/Deprecations/data/bug-173.php b/tests/Rules/Deprecations/data/bug-173.php index ebe005e..47dd00b 100644 --- a/tests/Rules/Deprecations/data/bug-173.php +++ b/tests/Rules/Deprecations/data/bug-173.php @@ -7,9 +7,22 @@ function readHeader(): void echo $http_response_header[0]; } -function assignHeader(): void +function readThenAssign(): void +{ + echo $http_response_header[0]; + $http_response_header = []; +} + +function assignThenRead(): void { $http_response_header = []; + echo $http_response_header[0]; +} + +function appendThenRead(): void +{ + $http_response_header[] = 'x'; + echo $http_response_header[0]; } function takesHeader(array $http_response_header): void @@ -27,8 +40,67 @@ function variableVariable(string $name): void echo $$name; } +function issetHeader(): void +{ + var_dump(isset($http_response_header)); +} + +function issetThenRead(): void +{ + isset($http_response_header); + echo $http_response_header[0]; +} + +function coalesceThenRead(): void +{ + $_ = $http_response_header ?? null; + echo $http_response_header[0]; +} + +function assignCoalesceThenRead(): void +{ + $http_response_header ??= []; + echo $http_response_header[0]; +} + +function outerAssignDoesNotSilenceClosure(): void +{ + $http_response_header = []; + $inner = function (): void { + echo $http_response_header[0]; + }; +} + +function foreachValue(array $list): void +{ + foreach ($list as $http_response_header) { + echo $http_response_header[0]; + } +} + /** @deprecated */ function inDeprecatedScope(): void { echo $http_response_header[0]; } + +echo $http_response_header[0]; +$http_response_header = []; +echo $http_response_header[0]; + +function afterFileAssign(): void +{ + echo $http_response_header[0]; +} + +function foreachKeySilencesBody(array $list): void +{ + foreach ($list as $http_response_header => $value) { + echo $http_response_header[0]; + } +} + +function listAssignReadsRightHandSide(): void +{ + [$http_response_header] = $http_response_header; +} From 0f0a3123d9645cd4e4cac5687a711ef2d41f4815 Mon Sep 17 00:00:00 2001 From: Gabrieljr42 Date: Tue, 1 Dec 2026 10:40:46 -0300 Subject: [PATCH 3/4] Use Scope PHP version, Variable::class, and expand loop coverage --- .../HttpResponseHeaderFetchVisitor.php | 101 ++++-------------- ...precatedHttpResponseHeaderVariableRule.php | 31 ++---- ...ttpResponseHeaderVariableRulePhp84Test.php | 45 ++++++++ ...eHeaderVariableRulePhpVersionRangeTest.php | 40 +++++++ ...atedHttpResponseHeaderVariableRuleTest.php | 54 +++++++--- .../Deprecations/data/bug-173-php-version.php | 16 +++ tests/Rules/Deprecations/data/bug-173.php | 66 +++++++++++- .../data/http-response-header-php84-85.neon | 4 + .../data/http-response-header-php84.neon | 2 + .../data/http-response-header-php85.neon | 2 + 10 files changed, 240 insertions(+), 121 deletions(-) create mode 100644 tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhp84Test.php create mode 100644 tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhpVersionRangeTest.php create mode 100644 tests/Rules/Deprecations/data/bug-173-php-version.php create mode 100644 tests/Rules/Deprecations/data/http-response-header-php84-85.neon create mode 100644 tests/Rules/Deprecations/data/http-response-header-php84.neon create mode 100644 tests/Rules/Deprecations/data/http-response-header-php85.neon diff --git a/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php index 1dbf913..98ab01f 100644 --- a/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php +++ b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php @@ -4,23 +4,14 @@ use PhpParser\Node; use PhpParser\Node\Expr\Array_; -use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\AssignOp; use PhpParser\Node\Expr\AssignOp\Coalesce as AssignCoalesce; use PhpParser\Node\Expr\AssignRef; -use PhpParser\Node\Expr\BinaryOp\Coalesce; -use PhpParser\Node\Expr\Empty_; -use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Expr\List_; -use PhpParser\Node\Expr\PostDec; -use PhpParser\Node\Expr\PostInc; -use PhpParser\Node\Expr\PreDec; -use PhpParser\Node\Expr\PreInc; use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; +use PhpParser\Node\Stmt\Catch_; use PhpParser\Node\Stmt\Foreach_; -use PhpParser\Node\Stmt\Unset_; use PhpParser\NodeVisitorAbstract; use function array_pop; use function count; @@ -28,19 +19,13 @@ use function is_string; /** - * Marks plain reads of $http_response_header. - * - * PHP 8.5 warns only for BP_VAR_R, and only while the current op array has - * not already accepted the name as a write (BP_VAR_W) or as a parameter. - * isset/empty/unset and the left side of ?? do not warn and do not count - * as a write. A foreach value is compiled as BP_VAR_R, so it does warn. + * Tracks whether $http_response_header has already been shadowed in the current + * op array by a parameter or prior write (BP_VAR_W). */ class HttpResponseHeaderFetchVisitor extends NodeVisitorAbstract { - public const ATTRIBUTE_NAME = 'httpResponseHeaderFetch'; - - public const FETCH_READ = 'read'; + public const ATTRIBUTE_NAME = 'httpResponseHeaderShadowed'; /** @var bool[] */ private array $assignedStack = []; @@ -77,7 +62,7 @@ public function enterNode(Node $node) } if ($node instanceof Variable && is_string($node->name) && $node->name === 'http_response_header') { - $this->classifyRead($node); + $this->trackVariable($node); } $this->nodeStack[] = $node; @@ -110,7 +95,7 @@ public function leaveNode(Node $node) return null; } - private function classifyRead(Variable $node): void + private function trackVariable(Variable $node): void { $write = $this->enclosingWrite($node); if ($write === 'assign' && !$this->listAssignment()) { @@ -118,15 +103,17 @@ private function classifyRead(Variable $node): void $this->markAssigned(); return; } - if ($write === 'assignRefRhs' || $write === 'foreachKey') { + if ($write === 'assignRefRhs' || $write === 'foreachWrite' || $write === 'catchVar') { $this->markAssigned(); return; } - if ($write === 'assign' || $this->isSilentFetch($node) || $this->isInPlaceTarget($node) || $this->isAssigned()) { + if ($write === 'assign') { return; } - $node->setAttribute(self::ATTRIBUTE_NAME, self::FETCH_READ); + if ($this->isAssigned()) { + $node->setAttribute(self::ATTRIBUTE_NAME, true); + } } private function parametersShadowTheVariable(FunctionLike $function): bool @@ -159,8 +146,15 @@ private function enclosingWrite(Variable $node): ?string return null; } if ($parent instanceof Foreach_) { - if ($parent->keyVar === $child) { - return 'foreachKey'; + if ($parent->keyVar === $child || ($parent->byRef && $parent->valueVar === $child)) { + return 'foreachWrite'; + } + + return null; + } + if ($parent instanceof Catch_) { + if ($parent->var === $child) { + return 'catchVar'; } return null; @@ -184,61 +178,6 @@ private function listAssignment(): bool return false; } - private function isSilentFetch(Variable $node): bool - { - $child = $node; - for ($i = count($this->nodeStack) - 1; $i >= 0; $i--) { - $parent = $this->nodeStack[$i]; - if ($parent instanceof FunctionLike) { - return false; - } - if ($parent instanceof Isset_ || $parent instanceof Empty_ || $parent instanceof Unset_) { - return true; - } - if ($parent instanceof Coalesce && ($parent->left === $child || $this->isDimBase($parent->left, $child))) { - return true; - } - - $child = $parent; - } - - return false; - } - - private function isInPlaceTarget(Variable $node): bool - { - $child = $node; - for ($i = count($this->nodeStack) - 1; $i >= 0; $i--) { - $parent = $this->nodeStack[$i]; - if ($parent instanceof FunctionLike) { - return false; - } - if ( - ($parent instanceof PreInc || $parent instanceof PostInc || $parent instanceof PreDec || $parent instanceof PostDec || $parent instanceof AssignOp) - && ($parent->var === $child || $this->isDimBase($parent->var, $child)) - ) { - return true; - } - - $child = $parent; - } - - return false; - } - - private function isDimBase(Node $root, Node $child): bool - { - $current = $root; - while ($current instanceof ArrayDimFetch) { - if ($current === $child) { - return true; - } - $current = $current->var; - } - - return $current === $child; - } - private function subtreeContainsName(Node $node): bool { if ($node instanceof Variable && $node->name === 'http_response_header') { diff --git a/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php index b93b57b..65f5fd2 100644 --- a/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php +++ b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php @@ -4,15 +4,14 @@ use PhpParser\Node; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Stmt\Foreach_; use PHPStan\Analyser\Scope; -use PHPStan\Php\PhpVersion; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; +use PHPStan\Type\IntegerRangeType; use function is_string; /** - * @implements Rule + * @implements Rule */ class ReadingDeprecatedHttpResponseHeaderVariableRule implements Rule { @@ -21,47 +20,35 @@ class ReadingDeprecatedHttpResponseHeaderVariableRule implements Rule private DeprecatedScopeHelper $deprecatedScopeHelper; - private PhpVersion $phpVersion; - - public function __construct( - DeprecatedScopeHelper $deprecatedScopeHelper, - PhpVersion $phpVersion - ) + public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) { $this->deprecatedScopeHelper = $deprecatedScopeHelper; - $this->phpVersion = $phpVersion; } public function getNodeType(): string { - // 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; + return Variable::class; } public function processNode(Node $node, Scope $scope): array { - if ($node instanceof Foreach_) { - $node = $node->valueVar; - } - - if (!$node instanceof Variable) { + if (!is_string($node->name) || $node->name !== 'http_response_header') { return []; } - if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { + if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) { return []; } - if ($this->phpVersion->getVersionId() < self::PHP_8_5) { + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { return []; } - if (!is_string($node->name) || $node->name !== 'http_response_header') { + if (!IntegerRangeType::fromInterval(self::PHP_8_5, null)->isSuperTypeOf($scope->getPhpVersion()->getType())->yes()) { return []; } - if ($node->getAttribute(HttpResponseHeaderFetchVisitor::ATTRIBUTE_NAME) !== HttpResponseHeaderFetchVisitor::FETCH_READ) { + if ($node->getAttribute(HttpResponseHeaderFetchVisitor::ATTRIBUTE_NAME) === true) { return []; } diff --git a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhp84Test.php b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhp84Test.php new file mode 100644 index 0000000..252a3fb --- /dev/null +++ b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhp84Test.php @@ -0,0 +1,45 @@ + + */ +class ReadingDeprecatedHttpResponseHeaderVariableRulePhp84Test extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new ReadingDeprecatedHttpResponseHeaderVariableRule( + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]), + ); + } + + public function testPhp84(): void + { + $this->analyse([__DIR__ . '/data/bug-173.php'], []); + } + + public function testPhp84VersionGuards(): void + { + $this->analyse([__DIR__ . '/data/bug-173-php-version.php'], [ + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 8, + ], + ]); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../../rules.neon', + __DIR__ . '/data/http-response-header-php84.neon', + ...parent::getAdditionalConfigFiles(), + ]; + } + +} diff --git a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhpVersionRangeTest.php b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhpVersionRangeTest.php new file mode 100644 index 0000000..7edf21f --- /dev/null +++ b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhpVersionRangeTest.php @@ -0,0 +1,40 @@ + + */ +class ReadingDeprecatedHttpResponseHeaderVariableRulePhpVersionRangeTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new ReadingDeprecatedHttpResponseHeaderVariableRule( + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]), + ); + } + + public function testPhpVersionRange(): void + { + $this->analyse([__DIR__ . '/data/bug-173-php-version.php'], [ + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 8, + ], + ]); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../../rules.neon', + __DIR__ . '/data/http-response-header-php84-85.neon', + ...parent::getAdditionalConfigFiles(), + ]; + } + +} diff --git a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php index 1012f0a..8dc79dc 100644 --- a/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php +++ b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php @@ -2,7 +2,6 @@ namespace PHPStan\Rules\Deprecations; -use PHPStan\Php\PhpVersion; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; @@ -12,19 +11,15 @@ class ReadingDeprecatedHttpResponseHeaderVariableRuleTest extends RuleTestCase { - private int $phpVersionId = 80500; - protected function getRule(): Rule { return new ReadingDeprecatedHttpResponseHeaderVariableRule( new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]), - new PhpVersion($this->phpVersionId, PhpVersion::SOURCE_CONFIG), ); } public function testPhp85(): void { - $this->phpVersionId = 80500; $this->analyse([__DIR__ . '/data/bug-173.php'], [ [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', @@ -32,53 +27,78 @@ public function testPhp85(): void ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 12, + 13, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 18, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 59, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 65, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 51, + 78, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 57, + 85, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 70, + 98, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 76, + 109, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 77, + 115, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 87, + 135, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 93, + 149, ], [ 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', - 105, + 163, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 169, ], ]); } - public function testPhp84(): void + public function testPhp85VersionGuards(): void { - $this->phpVersionId = 80400; - $this->analyse([__DIR__ . '/data/bug-173.php'], []); + $this->analyse([__DIR__ . '/data/bug-173-php-version.php'], [ + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 8, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 15, + ], + ]); } public static function getAdditionalConfigFiles(): array { return [ __DIR__ . '/../../../rules.neon', + __DIR__ . '/data/http-response-header-php85.neon', ...parent::getAdditionalConfigFiles(), ]; } diff --git a/tests/Rules/Deprecations/data/bug-173-php-version.php b/tests/Rules/Deprecations/data/bug-173-php-version.php new file mode 100644 index 0000000..3636290 --- /dev/null +++ b/tests/Rules/Deprecations/data/bug-173-php-version.php @@ -0,0 +1,16 @@ += 80500) { + echo $http_response_header[0]; + } + + if (PHP_VERSION_ID < 80500) { + echo $http_response_header[0]; + } + + echo $http_response_header[0]; +} diff --git a/tests/Rules/Deprecations/data/bug-173.php b/tests/Rules/Deprecations/data/bug-173.php index 47dd00b..d7237e0 100644 --- a/tests/Rules/Deprecations/data/bug-173.php +++ b/tests/Rules/Deprecations/data/bug-173.php @@ -7,6 +7,12 @@ function readHeader(): void echo $http_response_header[0]; } +function readAfterFileGetContents(): void +{ + file_get_contents('https://example.com'); + echo $http_response_header[0]; +} + function readThenAssign(): void { echo $http_response_header[0]; @@ -40,9 +46,11 @@ function variableVariable(string $name): void echo $$name; } -function issetHeader(): void +function issetEmptyUnsetHeader(): void { var_dump(isset($http_response_header)); + var_dump(empty($http_response_header)); + unset($http_response_header); } function issetThenRead(): void @@ -78,6 +86,20 @@ function foreachValue(array $list): void } } +function foreachValueByRef(array $list): void +{ + foreach ($list as &$http_response_header) { + echo $http_response_header[0]; + } +} + +function foreachIteratingHeaders(): void +{ + foreach ($http_response_header as $header) { + echo $header; + } +} + /** @deprecated */ function inDeprecatedScope(): void { @@ -100,6 +122,48 @@ function foreachKeySilencesBody(array $list): void } } +function forLoopAssignedInInit(): void +{ + for ($http_response_header = 0; $http_response_header < 3; $http_response_header++) { + echo $http_response_header; + } +} + +function forLoopReadUnassigned(): void +{ + for ($i = 0; $i < 3; $i++) { + echo $http_response_header[0]; + } +} + +function whileLoopAssignedInCondition(callable $next): void +{ + while (($http_response_header = $next()) !== null) { + echo $http_response_header[0]; + } +} + +function whileLoopReadUnassigned(bool $cond): void +{ + while ($cond) { + echo $http_response_header[0]; + } +} + +function doWhileLoopAssignedInBody(): void +{ + do { + $http_response_header = []; + } while ($http_response_header !== []); +} + +function doWhileLoopReadUnassigned(bool $cond): void +{ + do { + echo $http_response_header[0]; + } while ($cond); +} + function listAssignReadsRightHandSide(): void { [$http_response_header] = $http_response_header; diff --git a/tests/Rules/Deprecations/data/http-response-header-php84-85.neon b/tests/Rules/Deprecations/data/http-response-header-php84-85.neon new file mode 100644 index 0000000..7e23179 --- /dev/null +++ b/tests/Rules/Deprecations/data/http-response-header-php84-85.neon @@ -0,0 +1,4 @@ +parameters: + phpVersion: + min: 80400 + max: 80500 diff --git a/tests/Rules/Deprecations/data/http-response-header-php84.neon b/tests/Rules/Deprecations/data/http-response-header-php84.neon new file mode 100644 index 0000000..39467bf --- /dev/null +++ b/tests/Rules/Deprecations/data/http-response-header-php84.neon @@ -0,0 +1,2 @@ +parameters: + phpVersion: 80400 diff --git a/tests/Rules/Deprecations/data/http-response-header-php85.neon b/tests/Rules/Deprecations/data/http-response-header-php85.neon new file mode 100644 index 0000000..72ae924 --- /dev/null +++ b/tests/Rules/Deprecations/data/http-response-header-php85.neon @@ -0,0 +1,2 @@ +parameters: + phpVersion: 80500 From 76b65691347b78366013cea79fae06633903aa43 Mon Sep 17 00:00:00 2001 From: Gabrieljr42 Date: Tue, 1 Dec 2026 11:18:44 -0300 Subject: [PATCH 4/4] Fix early exit coding standard violation --- src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php index 98ab01f..98d738d 100644 --- a/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php +++ b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php @@ -107,13 +107,11 @@ private function trackVariable(Variable $node): void $this->markAssigned(); return; } - if ($write === 'assign') { + if ($write === 'assign' || !$this->isAssigned()) { return; } - if ($this->isAssigned()) { - $node->setAttribute(self::ATTRIBUTE_NAME, true); - } + $node->setAttribute(self::ATTRIBUTE_NAME, true); } private function parametersShadowTheVariable(FunctionLike $function): bool