Skip to content

Commit 667be58

Browse files
committed
[Php85] Remove calls to deprecated no-op functions
1 parent 65e3739 commit 667be58

9 files changed

Lines changed: 218 additions & 0 deletions

File tree

config/set/php85.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector;
7+
use Rector\DeadCode\ValueObject\WrapFuncCallWithPhpVersionIdChecker;
68
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
79

810
return static function (RectorConfig $rectorConfig): void {
911
$rectorConfig->rules([ArrayFirstLastRector::class]);
12+
13+
// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_no-op_functions_from_the_resource_to_object_conversion
14+
$rectorConfig->ruleWithConfiguration(
15+
WrapFuncCallWithPhpVersionIdCheckerRector::class,
16+
[
17+
new WrapFuncCallWithPhpVersionIdChecker('curl_close', 80500),
18+
new WrapFuncCallWithPhpVersionIdChecker('curl_share_close', 80500),
19+
new WrapFuncCallWithPhpVersionIdChecker('finfo_close', 80500),
20+
new WrapFuncCallWithPhpVersionIdChecker('imagedestroy', 80500),
21+
new WrapFuncCallWithPhpVersionIdChecker('xml_parser_free', 80500),
22+
]
23+
);
1024
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
4+
5+
different_function();
6+
different_function(1, 2);
7+
8+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
4+
5+
$foo = no_op_function();
6+
7+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
4+
5+
if (no_op_function()) {
6+
7+
}
8+
9+
?>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
4+
5+
no_op_function();
6+
no_op_function(1, 2);
7+
8+
?>
9+
-----
10+
<?php
11+
12+
namespace Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture;
13+
14+
if (PHP_VERSION_ID < 80500) {
15+
no_op_function();
16+
}
17+
if (PHP_VERSION_ID < 80500) {
18+
no_op_function(1, 2);
19+
}
20+
21+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector;
4+
5+
use Iterator;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
8+
9+
final class WrapFuncCallWithPhpVersionIdCheckerRectorTest extends AbstractRectorTestCase
10+
{
11+
#[DataProvider('provideData')]
12+
public function test(string $filePath): void
13+
{
14+
$this->doTestFile($filePath);
15+
}
16+
17+
public static function provideData(): Iterator
18+
{
19+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
20+
}
21+
22+
public function provideConfigFilePath(): string
23+
{
24+
return __DIR__ . '/config/configured_rule.php';
25+
}
26+
}
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\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector;
7+
use Rector\DeadCode\ValueObject\WrapFuncCallWithPhpVersionIdChecker;
8+
9+
return RectorConfig::configure()
10+
->withConfiguredRule(
11+
WrapFuncCallWithPhpVersionIdCheckerRector::class,
12+
[new WrapFuncCallWithPhpVersionIdChecker('no_op_function', 80500)]
13+
);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\Rector\FuncCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp\Smaller;
9+
use PhpParser\Node\Expr\ConstFetch;
10+
use PhpParser\Node\Expr\FuncCall;
11+
use PhpParser\Node\Name;
12+
use PhpParser\Node\Param;
13+
use PhpParser\Node\Scalar\Int_;
14+
use PhpParser\Node\Stmt\Expression;
15+
use PhpParser\Node\Stmt\If_;
16+
use Rector\Contract\Rector\ConfigurableRectorInterface;
17+
use Rector\DeadCode\ValueObject\WrapFuncCallWithPhpVersionIdChecker;
18+
use Rector\Rector\AbstractRector;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
use Webmozart\Assert\Assert;
22+
23+
/**
24+
* @see \Rector\Tests\DeadCode\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\WrapFuncCallWithPhpVersionIdCheckerRectorTest
25+
*/
26+
final class WrapFuncCallWithPhpVersionIdCheckerRector extends AbstractRector implements ConfigurableRectorInterface
27+
{
28+
/**
29+
* @var WrapFuncCallWithPhpVersionIdChecker[]
30+
*/
31+
private array $wrapFuncCallWithPhpVersionIdCheckers = [];
32+
33+
public function getRuleDefinition(): RuleDefinition
34+
{
35+
return new RuleDefinition(
36+
'Wraps function calls without assignment in a condition to check for a PHP version id',
37+
[
38+
new ConfiguredCodeSample(
39+
<<<'CODE_SAMPLE'
40+
no_op_function();
41+
CODE_SAMPLE
42+
43+
,
44+
<<<'CODE_SAMPLE'
45+
if (PHP_VERSION_ID < 80500) {
46+
no_op_function();
47+
}
48+
CODE_SAMPLE
49+
,
50+
[new WrapFuncCallWithPhpVersionIdChecker('no_op_function', 80500)]
51+
),
52+
]
53+
);
54+
}
55+
56+
/**
57+
* @return array<class-string<Node>>
58+
*/
59+
public function getNodeTypes(): array
60+
{
61+
return [Expression::class];
62+
}
63+
64+
/**
65+
* @param Expression $node
66+
*/
67+
public function refactor(Node $node): ?Node
68+
{
69+
if (! $node->expr instanceof FuncCall) {
70+
return null;
71+
}
72+
73+
$funcCall = $node->expr;
74+
75+
foreach ($this->wrapFuncCallWithPhpVersionIdCheckers as $wrapFuncCallWithPhpVersionIdChecker) {
76+
if ($this->getName($funcCall) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName()) {
77+
continue;
78+
}
79+
80+
$phpVersionIdConst = new ConstFetch(new Name('PHP_VERSION_ID'));
81+
$if = new If_(new Smaller($phpVersionIdConst, new Int_(
82+
$wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId()
83+
)));
84+
$if->stmts = [$node];
85+
86+
return $if;
87+
}
88+
89+
return null;
90+
}
91+
92+
public function configure(array $configuration): void
93+
{
94+
Assert::allIsInstanceOf($configuration, WrapFuncCallWithPhpVersionIdChecker::class);
95+
96+
$this->wrapFuncCallWithPhpVersionIdCheckers = $configuration;
97+
}
98+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Rector\DeadCode\ValueObject;
4+
5+
final readonly class WrapFuncCallWithPhpVersionIdChecker
6+
{
7+
public function __construct(
8+
private string $functionName,
9+
private int $phpVersionId
10+
) {
11+
}
12+
13+
public function getFunctionName(): string
14+
{
15+
return $this->functionName;
16+
}
17+
18+
public function getPhpVersionId(): int
19+
{
20+
return $this->phpVersionId;
21+
}
22+
}

0 commit comments

Comments
 (0)