Repository navigation
Report deprecated $http_response_header on PHP 8.5 #217
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
Open
Gabrieljr42
wants to merge
4
commits into
phpstan:2.0.x
Choose a base branch
from
Gabrieljr42:fix/http-response-header-173
base: 2.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+673
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fbd68e8
Report deprecated $http_response_header on PHP 8.5
Gabrieljr42 818822d
Do not report writes or shadowed $http_response_header
Gabrieljr42 0f0a312
Use Scope PHP version, Variable::class, and expand loop coverage
Gabrieljr42 76b6569
Fix early exit coding standard violation
Gabrieljr42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Deprecations; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Array_; | ||
| use PhpParser\Node\Expr\Assign; | ||
| use PhpParser\Node\Expr\AssignOp\Coalesce as AssignCoalesce; | ||
| use PhpParser\Node\Expr\AssignRef; | ||
| use PhpParser\Node\Expr\List_; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\FunctionLike; | ||
| use PhpParser\Node\Stmt\Catch_; | ||
| use PhpParser\Node\Stmt\Foreach_; | ||
| use PhpParser\NodeVisitorAbstract; | ||
| use function array_pop; | ||
| use function count; | ||
| use function is_array; | ||
| use function is_string; | ||
|
|
||
| /** | ||
| * 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 = 'httpResponseHeaderShadowed'; | ||
|
|
||
| /** @var bool[] */ | ||
| private array $assignedStack = []; | ||
|
|
||
| /** @var Node[] */ | ||
| private array $nodeStack = []; | ||
|
|
||
| /** | ||
| * @param Node[] $nodes | ||
| */ | ||
| public function beforeTraverse(array $nodes) | ||
| { | ||
| $this->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->trackVariable($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 trackVariable(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 === 'foreachWrite' || $write === 'catchVar') { | ||
| $this->markAssigned(); | ||
| return; | ||
| } | ||
| if ($write === 'assign' || !$this->isAssigned()) { | ||
| return; | ||
| } | ||
|
|
||
| $node->setAttribute(self::ATTRIBUTE_NAME, true); | ||
| } | ||
|
|
||
| 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 || ($parent->byRef && $parent->valueVar === $child)) { | ||
| return 'foreachWrite'; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| if ($parent instanceof Catch_) { | ||
| if ($parent->var === $child) { | ||
| return 'catchVar'; | ||
| } | ||
|
|
||
| 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 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]; | ||
| } | ||
|
|
||
| } |
62 changes: 62 additions & 0 deletions
62
src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Deprecations; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\IntegerRangeType; | ||
| use function is_string; | ||
|
|
||
| /** | ||
| * @implements Rule<Variable> | ||
| */ | ||
| class ReadingDeprecatedHttpResponseHeaderVariableRule implements Rule | ||
| { | ||
|
|
||
| private const PHP_8_5 = 80500; | ||
|
|
||
| private DeprecatedScopeHelper $deprecatedScopeHelper; | ||
|
|
||
| public function __construct(DeprecatedScopeHelper $deprecatedScopeHelper) | ||
| { | ||
| $this->deprecatedScopeHelper = $deprecatedScopeHelper; | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return Variable::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!is_string($node->name) || $node->name !== 'http_response_header') { | ||
| return []; | ||
| } | ||
|
|
||
| if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!IntegerRangeType::fromInterval(self::PHP_8_5, null)->isSuperTypeOf($scope->getPhpVersion()->getType())->yes()) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($node->getAttribute(HttpResponseHeaderFetchVisitor::ATTRIBUTE_NAME) === true) { | ||
| 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(), | ||
| ]; | ||
| } | ||
|
|
||
| } | ||
45 changes: 45 additions & 0 deletions
45
tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhp84Test.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Deprecations; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<ReadingDeprecatedHttpResponseHeaderVariableRule> | ||
| */ | ||
| 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(), | ||
| ]; | ||
| } | ||
|
|
||
| } |
40 changes: 40 additions & 0 deletions
40
...Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRulePhpVersionRangeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Deprecations; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<ReadingDeprecatedHttpResponseHeaderVariableRule> | ||
| */ | ||
| 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(), | ||
| ]; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_headerwas magically implied by the engine, or whether it was assigned explicitly and we should skip over