diff --git a/rules.neon b/rules.neon index aa4a8a0..f9e770b 100644 --- a/rules.neon +++ b/rules.neon @@ -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: diff --git a/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php new file mode 100644 index 0000000..98d738d --- /dev/null +++ b/src/Rules/Deprecations/HttpResponseHeaderFetchVisitor.php @@ -0,0 +1,220 @@ +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]; + } + +} diff --git a/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php new file mode 100644 index 0000000..65f5fd2 --- /dev/null +++ b/src/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRule.php @@ -0,0 +1,62 @@ + + */ +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(), + ]; + } + +} 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 new file mode 100644 index 0000000..8dc79dc --- /dev/null +++ b/tests/Rules/Deprecations/ReadingDeprecatedHttpResponseHeaderVariableRuleTest.php @@ -0,0 +1,106 @@ + + */ +class ReadingDeprecatedHttpResponseHeaderVariableRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new ReadingDeprecatedHttpResponseHeaderVariableRule( + new DeprecatedScopeHelper([new DefaultDeprecatedScopeResolver()]), + ); + } + + public function testPhp85(): void + { + $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.', + 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.', + 78, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 85, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 98, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 109, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 115, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 135, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 149, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 163, + ], + [ + 'The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead.', + 169, + ], + ]); + } + + public function testPhp85VersionGuards(): 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, + ], + [ + '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 new file mode 100644 index 0000000..d7237e0 --- /dev/null +++ b/tests/Rules/Deprecations/data/bug-173.php @@ -0,0 +1,170 @@ + $value) { + echo $http_response_header[0]; + } +} + +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