Skip to content

Commit d1626c1

Browse files
ArshidArshid
authored andcommitted
Replace with http_get_last_response_headers() call
1 parent f414fd2 commit d1626c1

7 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector\Fixture;
6+
7+
var_dump( $http_response_header );
8+
9+
?>
10+
-----
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
namespace Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector\Fixture;
16+
17+
$http_response_header = http_get_last_response_headers();
18+
var_dump( $http_response_header );
19+
20+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector\Fixture;
6+
7+
$http_response_header = 1;
8+
echo $http_response_header;
9+
10+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector\Fixture;
6+
7+
echo $http_response_header;
8+
9+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector\Fixture;
6+
7+
$http_response_header = null;
8+
echo $http_response_header;
9+
10+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ReplaceHttpResponseHeaderRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rule(ReplaceHttpResponseHeaderRector::class);
11+
12+
$rectorConfig->phpVersion(PhpVersion::PHP_85);
13+
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php85\Rector\Expression;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Assign;
9+
use PhpParser\Node\Expr\FuncCall;
10+
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Name;
12+
use PhpParser\Node\Stmt\Expression;
13+
use Rector\NodeTypeResolver\Node\AttributeKey;
14+
use Rector\PhpParser\Node\BetterNodeFinder;
15+
use Rector\Rector\AbstractRector;
16+
use Rector\ValueObject\PhpVersionFeature;
17+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
18+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
19+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
20+
21+
/**
22+
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_http_response_header_predefined_variable
23+
* @see \Rector\Tests\Php85\Rector\Expression\ReplaceHttpResponseHeaderRector\ReplaceHttpResponseHeaderRectorTest
24+
*/
25+
final class ReplaceHttpResponseHeaderRector extends AbstractRector implements MinPhpVersionInterface
26+
{
27+
public function __construct(
28+
private readonly BetterNodeFinder $betterNodeFinder,
29+
) {
30+
}
31+
32+
public function provideMinPhpVersion(): int
33+
{
34+
return PhpVersionFeature::SERVER_VAR;
35+
}
36+
37+
public function getRuleDefinition(): RuleDefinition
38+
{
39+
return new RuleDefinition('Assign http_get_last_response_headers() to $http_response_header if used without assignment', [
40+
new CodeSample(
41+
<<<'CODE_SAMPLE'
42+
43+
echo $http_response_header;
44+
45+
CODE_SAMPLE
46+
,
47+
<<<'CODE_SAMPLE'
48+
49+
$http_response_header = http_get_last_response_headers();
50+
51+
echo $http_response_header;
52+
53+
CODE_SAMPLE
54+
),
55+
]);
56+
}
57+
58+
/**
59+
* @return array<class-string<Node>>
60+
*/
61+
public function getNodeTypes(): array
62+
{
63+
return [Expression::class];
64+
}
65+
66+
/**
67+
* @param Variable $node
68+
*/
69+
public function refactor(Node $node): ?Array
70+
{
71+
$variables = $this->betterNodeFinder->findInstanceOf($node, Variable::class);
72+
73+
foreach ($variables as $var) {
74+
if( $var->getAttribute(AttributeKey::IS_BEING_ASSIGNED)){
75+
return null;
76+
}
77+
if ($this->getName($var) === 'http_response_header') {
78+
79+
$assign = new Expression(
80+
new Assign(
81+
new Variable('http_response_header'),
82+
new FuncCall(new Name('http_get_last_response_headers'))
83+
)
84+
);
85+
return [$assign, $node];
86+
}
87+
}
88+
89+
return null;
90+
}
91+
}

0 commit comments

Comments
 (0)