Skip to content
Merged
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
16 changes: 15 additions & 1 deletion config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Cast\String_;
use Rector\Config\RectorConfig;
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Removing\ValueObject\RemoveFuncCallArg;
Expand All @@ -20,6 +20,8 @@
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameCast;
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
use Rector\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector;
use Rector\Transform\ValueObject\WrapFuncCallWithPhpVersionIdChecker;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules(
Expand Down Expand Up @@ -180,4 +182,16 @@
new RenameCast(String_::class, String_::KIND_BINARY, String_::KIND_STRING),
]
);

// https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_no-op_functions_from_the_resource_to_object_conversion
$rectorConfig->ruleWithConfiguration(
WrapFuncCallWithPhpVersionIdCheckerRector::class,
[
new WrapFuncCallWithPhpVersionIdChecker('curl_close', 80500),
new WrapFuncCallWithPhpVersionIdChecker('curl_share_close', 80500),
new WrapFuncCallWithPhpVersionIdChecker('finfo_close', 80500),
new WrapFuncCallWithPhpVersionIdChecker('imagedestroy', 80500),
new WrapFuncCallWithPhpVersionIdChecker('xml_parser_free', 80500),
]
);
};
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);

?>
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);
}

?>
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);
}

?>
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();

?>
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()) {

}

?>
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);
}

?>
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';
}
}
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)]
);
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;
}
}
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;
}
}
Loading