-
-
Notifications
You must be signed in to change notification settings - Fork 431
[Php85] Remove calls to deprecated no-op functions #7128
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
Merged
samsonasik
merged 7 commits into
rectorphp:main
from
mttsch:feature/php85-remove-no-op-function-calls
Aug 20, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce801c7
[Php85] Remove calls to deprecated no-op functions
mttsch 7e9aa6f
Change namespace from DeadCode to Transform
mttsch 7ec3674
Check if wrapped func call is already wrapped
mttsch cb82c81
Add additional function_exists condition
mttsch 881b7a5
Move value object into correct namespace
mttsch 4f24659
Update code sample
mttsch 8b653cd
Fix PHPStan issue
mttsch 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
8 changes: 8 additions & 0 deletions
8
...tor/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector/Fixture/different_function.php.inc
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,8 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| different_function(); | ||
| different_function(1, 2); | ||
|
|
||
| ?> |
12 changes: 12 additions & 0 deletions
12
...l/WrapFuncCallWithPhpVersionIdCheckerRector/Fixture/skip_already_if_version_check.php.inc
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,12 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| if (PHP_VERSION_ID < 80500) { | ||
| no_op_function(); | ||
| } | ||
| if (PHP_VERSION_ID < 80500) { | ||
| no_op_function(1, 2); | ||
| } | ||
|
|
||
| ?> |
12 changes: 12 additions & 0 deletions
12
...VersionIdCheckerRector/Fixture/skip_already_if_version_check_with_function_exists.php.inc
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,12 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) { | ||
| no_op_function(); | ||
| } | ||
| if (PHP_VERSION_ID < 80500 && function_exists('no_op_function')) { | ||
| no_op_function(1, 2); | ||
| } | ||
|
|
||
| ?> |
7 changes: 7 additions & 0 deletions
7
...ll/WrapFuncCallWithPhpVersionIdCheckerRector/Fixture/skip_func_call_in_assignment.php.inc
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,7 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| $foo = no_op_function(); | ||
|
|
||
| ?> |
9 changes: 9 additions & 0 deletions
9
...all/WrapFuncCallWithPhpVersionIdCheckerRector/Fixture/skip_func_call_in_condition.php.inc
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,9 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| if (no_op_function()) { | ||
|
|
||
| } | ||
|
|
||
| ?> | ||
21 changes: 21 additions & 0 deletions
21
...ector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector/Fixture/wrapped_function.php.inc
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,21 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| no_op_function(); | ||
| no_op_function(1, 2); | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\Fixture; | ||
|
|
||
| if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) { | ||
| no_op_function(); | ||
| } | ||
| if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) { | ||
| no_op_function(1, 2); | ||
| } | ||
|
|
||
| ?> |
26 changes: 26 additions & 0 deletions
26
...apFuncCallWithPhpVersionIdCheckerRector/WrapFuncCallWithPhpVersionIdCheckerRectorTest.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,26 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class WrapFuncCallWithPhpVersionIdCheckerRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...form/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector/config/configured_rule.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,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector; | ||
| use Rector\Transform\ValueObject\WrapFuncCallWithPhpVersionIdChecker; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withConfiguredRule( | ||
| WrapFuncCallWithPhpVersionIdCheckerRector::class, | ||
| [new WrapFuncCallWithPhpVersionIdChecker('no_op_function', 80500)] | ||
| ); |
179 changes: 179 additions & 0 deletions
179
rules/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector.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,179 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Transform\Rector\FuncCall; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\BinaryOp\BooleanAnd; | ||
| use PhpParser\Node\Expr\BinaryOp\Smaller; | ||
| use PhpParser\Node\Expr\ConstFetch; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Scalar\Int_; | ||
| use PhpParser\Node\Scalar\String_; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use PhpParser\Node\Stmt\If_; | ||
| use PhpParser\NodeVisitor; | ||
| use Rector\Contract\PhpParser\Node\StmtsAwareInterface; | ||
| use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\Transform\ValueObject\WrapFuncCallWithPhpVersionIdChecker; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector\WrapFuncCallWithPhpVersionIdCheckerRectorTest | ||
| */ | ||
| final class WrapFuncCallWithPhpVersionIdCheckerRector extends AbstractRector implements ConfigurableRectorInterface | ||
| { | ||
| /** | ||
| * @var WrapFuncCallWithPhpVersionIdChecker[] | ||
| */ | ||
| private array $wrapFuncCallWithPhpVersionIdCheckers = []; | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Wraps function calls without assignment in a condition to check for a PHP version id', | ||
| [ | ||
| new ConfiguredCodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| no_op_function(); | ||
| CODE_SAMPLE | ||
|
|
||
| , | ||
| <<<'CODE_SAMPLE' | ||
| if (function_exists('no_op_function') && PHP_VERSION_ID < 80500) { | ||
| no_op_function(); | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| [new WrapFuncCallWithPhpVersionIdChecker('no_op_function', 80500)] | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [StmtsAwareInterface::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param StmtsAwareInterface $node | ||
| */ | ||
| public function refactor(Node $node): null|Node|int | ||
| { | ||
| if ($node->stmts === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($this->isWrappedFuncCall($node)) { | ||
| return NodeVisitor::DONT_TRAVERSE_CHILDREN; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
| foreach ($node->stmts as $key => $stmt) { | ||
| if (! $stmt instanceof Expression || ! $stmt->expr instanceof FuncCall) { | ||
| continue; | ||
| } | ||
|
|
||
| $funcCall = $stmt->expr; | ||
|
|
||
| foreach ($this->wrapFuncCallWithPhpVersionIdCheckers as $wrapFuncCallWithPhpVersionIdChecker) { | ||
| if ($this->getName($funcCall) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName()) { | ||
| continue; | ||
| } | ||
|
|
||
| $phpVersionIdConst = new ConstFetch(new Name('PHP_VERSION_ID')); | ||
| $if = new If_(new BooleanAnd( | ||
| new FuncCall(new Name('function_exists'), [new Arg(new String_( | ||
| $wrapFuncCallWithPhpVersionIdChecker->getFunctionName() | ||
| ))]), | ||
| new Smaller($phpVersionIdConst, new Int_($wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId())), | ||
| )); | ||
| $if->stmts = [$stmt]; | ||
|
|
||
| $node->stmts[$key] = $if; | ||
|
|
||
| $hasChanged = true; | ||
| } | ||
| } | ||
|
|
||
| if ($hasChanged) { | ||
| return $node; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public function configure(array $configuration): void | ||
| { | ||
| Assert::allIsInstanceOf($configuration, WrapFuncCallWithPhpVersionIdChecker::class); | ||
|
|
||
| $this->wrapFuncCallWithPhpVersionIdCheckers = $configuration; | ||
| } | ||
|
|
||
| private function isWrappedFuncCall(StmtsAwareInterface $node): bool | ||
| { | ||
| if (! $node instanceof If_) { | ||
| return false; | ||
| } | ||
|
|
||
| $phpVersionId = $this->getPhpVersionId($node->cond); | ||
| if ($phpVersionId === null) { | ||
| return false; | ||
| } | ||
|
|
||
| if (count($node->stmts) !== 1) { | ||
| return false; | ||
| } | ||
|
|
||
| $childStmt = $node->stmts[0]; | ||
|
|
||
| if (! $childStmt instanceof Expression || ! $childStmt->expr instanceof FuncCall) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach ($this->wrapFuncCallWithPhpVersionIdCheckers as $wrapFuncCallWithPhpVersionIdChecker) { | ||
| if ( | ||
| $this->getName($childStmt->expr) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName() | ||
| || $phpVersionId->value !== $wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId() | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private function getPhpVersionId(Expr $expr): ?Int_ | ||
| { | ||
| if ($expr instanceof BooleanAnd) { | ||
| return $this->getPhpVersionId($expr->left) ?? $this->getPhpVersionId($expr->right); | ||
| } | ||
|
|
||
| if (! $expr instanceof Smaller) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $expr->left instanceof ConstFetch || ! $this->isName($expr->left->name, 'PHP_VERSION_ID')) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $expr->right instanceof Int_) { | ||
| return null; | ||
| } | ||
|
|
||
| return $expr->right; | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
rules/Transform/ValueObject/WrapFuncCallWithPhpVersionIdChecker.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,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Transform\ValueObject; | ||
|
|
||
| final readonly class WrapFuncCallWithPhpVersionIdChecker | ||
| { | ||
| public function __construct( | ||
| private string $functionName, | ||
| private int $phpVersionId | ||
| ) { | ||
| } | ||
|
|
||
| public function getFunctionName(): string | ||
| { | ||
| return $this->functionName; | ||
| } | ||
|
|
||
| public function getPhpVersionId(): int | ||
| { | ||
| return $this->phpVersionId; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.