Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ services:
tags:
- phpstan.restrictedClassNameUsageExtension

-
class: PHPStan\Rules\Deprecations\HttpResponseHeaderFetchVisitor
tags:
- phpstan.parser.richParserNodeVisitor

rules:
- PHPStan\Rules\Deprecations\FetchingDeprecatedConstRule
- PHPStan\Rules\Deprecations\ReadingDeprecatedHttpResponseHeaderVariableRule

conditionalTags:
PHPStan\Rules\Deprecations\CallWithDeprecatedIniOptionRule:
Expand Down
220 changes: 220 additions & 0 deletions src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php
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];
}

}
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 [];
}
Comment on lines +39 to +41

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


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(),
];
}

}
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(),
];
}

}
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(),
];
}

}
Loading