Skip to content
Open
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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,39 @@ composer require fsylum/rector-wordpress --dev

## Use Sets

To add a set to your config, use `Fsylum\RectorWordPress\Set\WordPressSetList` class and pick one of the constants. For example, to update the codebase to WordPress 6.8, use `WordPressSetList::WP_6_8`.
To add a set to your config, use `Fsylum\RectorWordPress\Set\WordPressSetList` class and pick one of the constants. For example, to update the codebase to WordPress 7.0, use `WordPressSetList::WP_7_0`.

```php
use Fsylum\RectorWordPress\Set\WordPressSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
WordPressSetList::WP_6_8,
WordPressSetList::WP_7_0,
]);
};
```

You can also use a level set list to include all the applicable rules from the lowest version, 0.71 up to the one you specified. For example, `WordPressLevelSetList::UP_TO_WP_6_8` will include all the rules from WordPress 0.71 up to 6.8. In most cases, this is the preferable way to transform your code as you only need to specify it once.
You can also use a level set list to include all the applicable rules from the lowest version, 0.71 up to the one you specified. For example, `WordPressLevelSetList::UP_TO_WP_7_0` will include all the rules from WordPress 0.71 up to 7.0. In most cases, this is the preferable way to transform your code as you only need to specify it once.

```php
use Fsylum\RectorWordPress\Set\WordPressLevelSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
WordPressLevelSetList::UP_TO_WP_6_8,
WordPressLevelSetList::UP_TO_WP_7_0,
]);
};
```

## Supported WordPress Versions

This package provides upgrade rules for WordPress versions from 0.71 to 6.8, covering:
This package provides upgrade rules for WordPress versions from 0.71 to 7.0, covering:
- WordPress 0.71, 1.0, 1.2, 1.5
- WordPress 2.x (2.0 through 2.9)
- WordPress 3.x (3.0 through 3.9)
- WordPress 4.x (4.0 through 4.9)
- WordPress 5.x (5.0 through 5.9)
- WordPress 6.x (6.0 through 6.8)
- WordPress 6.x (6.0 through 6.9)
- WordPress 7.0
6 changes: 6 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Fsylum\RectorWordPress\Rules\FuncCall\ParameterAdderRector;
use Fsylum\RectorWordPress\Rules\MethodCall\ParameterAdderRector as MethodParameterAdderRector;
use Fsylum\RectorWordPress\Rules\FuncCall\ParameterPrependerRector;
use Fsylum\RectorWordPress\Rules\FuncCall\RenameFunctionWithArgumentsRector;
use Fsylum\RectorWordPress\ValueObject\FunctionParameterAdder;
use Fsylum\RectorWordPress\ValueObject\FunctionParameterPrepender;
use Fsylum\RectorWordPress\ValueObject\FunctionRenameWithArguments;
use Fsylum\RectorWordPress\ValueObject\MethodParameterAdder;
use Rector\Config\RectorConfig;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
Expand Down Expand Up @@ -42,6 +44,10 @@
new FunctionParameterPrepender('_imaginary_function_that_should_not_exists', 'foo'),
]);

$rectorConfig->ruleWithConfiguration(RenameFunctionWithArgumentsRector::class, [
new FunctionRenameWithArguments('_imaginary_function_that_should_not_exists', '_new_imaginary_function_that_should_not_exists', []),
]);

$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
new MethodCallRename('_imaginary_class', '_old_imaginary_method_that_should_not_exists', '_new_imaginary_method_that_should_not_exists'),
]);
Expand Down
9 changes: 9 additions & 0 deletions config/sets/level/up-to-wp-6.9.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Fsylum\RectorWordPress\Set\WordPressLevelSetList;
use Fsylum\RectorWordPress\Set\WordPressSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([WordPressSetList::WP_6_9, WordPressLevelSetList::UP_TO_WP_6_8]);
};
9 changes: 9 additions & 0 deletions config/sets/level/up-to-wp-7.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Fsylum\RectorWordPress\Set\WordPressLevelSetList;
use Fsylum\RectorWordPress\Set\WordPressSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([WordPressSetList::WP_7_0, WordPressLevelSetList::UP_TO_WP_6_9]);
};
27 changes: 27 additions & 0 deletions config/sets/wp-6.9.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Fsylum\RectorWordPress\Rules\FuncCall\RenameFunctionWithArgumentsRector;
use Fsylum\RectorWordPress\ValueObject\FunctionRenameWithArguments;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');

$rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
'seems_utf8' => 'wp_is_valid_utf8',
'wp_print_auto_sizes_contain_css_fix' => 'wp_enqueue_img_auto_sizes_contain_css_fix',
]);

$rectorConfig->ruleWithConfiguration(RenameFunctionWithArgumentsRector::class, [
new FunctionRenameWithArguments('utf8_encode', 'mb_convert_encoding', ['UTF-8', 'ISO-8859-1']),
new FunctionRenameWithArguments('utf8_decode', 'mb_convert_encoding', ['ISO-8859-1', 'UTF-8']),
Comment on lines +17 to +18

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abhishek-kaushik I'd like to keep it focused on WP-specific function. Can we remove this one? If we remove this then we don't need FunctionRenameWithArguments I think?

]);

/*
* TODO: these are not handled currently
*
* ARGUMENTS
* - _wp_can_use_pcre_u (the $set argument is no longer used; private/internal function)
*/
};
32 changes: 32 additions & 0 deletions config/sets/wp-7.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Fsylum\RectorWordPress\Rules\FuncCall\RenameFunctionWithArgumentsRector;
use Fsylum\RectorWordPress\ValueObject\FunctionRenameWithArguments;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');

$rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
'addslashes_gpc' => 'wp_slash',
'block_core_navigation_submenu_render_submenu_icon' => 'block_core_shared_navigation_render_submenu_icon',
]);

$rectorConfig->ruleWithConfiguration(RenameFunctionWithArgumentsRector::class, [
new FunctionRenameWithArguments(
'block_core_navigation_block_contains_core_navigation',
'block_core_navigation_block_tree_has_block_type',
['core/navigation']
),
]);

/*
* TODO: these are not handled currently
*
* FUNCTIONS
* - wp_sanitize_script_attributes: no safe automatic replacement. It returns a string of HTML
* attributes, whereas its suggested replacements wp_get_script_tag() / wp_get_inline_script_tag()
* return a full <script> tag and take different arguments, so a rename would produce broken output.
*/
};
77 changes: 77 additions & 0 deletions src/Rules/FuncCall/RenameFunctionWithArgumentsRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Fsylum\RectorWordPress\Rules\FuncCall;

use Fsylum\RectorWordPress\ValueObject\FunctionRenameWithArguments;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

final class RenameFunctionWithArgumentsRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var array<FunctionRenameWithArguments>
*/
private array $configuration = [];

public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->configuration as $config) {
if (!$this->isName($node->name, $config->getOldFunction())) {
continue;
}

$node->name = new Name($config->getNewFunction());

foreach ($config->getArguments() as $value) {
/** @phpstan-ignore argument.type */
$node->args[] = new Arg(BuilderHelpers::normalizeValue($value));
}

return $node;
}

return null;
}

/**
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
// @phpstan-ignore argument.type
Assert::allIsAOf($configuration, FunctionRenameWithArguments::class);

// @var array<FunctionRenameWithArguments> $configuration
$this->configuration = $configuration;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Rename a function call and append additional arguments to it',
[
new ConfiguredCodeSample(
'utf8_encode($value);',
"mb_convert_encoding(\$value, 'UTF-8', 'ISO-8859-1');",
[new FunctionRenameWithArguments('utf8_encode', 'mb_convert_encoding', ['UTF-8', 'ISO-8859-1'])]
),
]
);
}
}
2 changes: 2 additions & 0 deletions src/Set/WordPressLevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ final class WordPressLevelSetList
public const UP_TO_WP_6_6 = __DIR__ . '/../../config/sets/level/up-to-wp-6.6.php';
public const UP_TO_WP_6_7 = __DIR__ . '/../../config/sets/level/up-to-wp-6.7.php';
public const UP_TO_WP_6_8 = __DIR__ . '/../../config/sets/level/up-to-wp-6.8.php';
public const UP_TO_WP_6_9 = __DIR__ . '/../../config/sets/level/up-to-wp-6.9.php';
public const UP_TO_WP_7_0 = __DIR__ . '/../../config/sets/level/up-to-wp-7.0.php';
}
2 changes: 2 additions & 0 deletions src/Set/WordPressSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ final class WordPressSetList
public const WP_6_6 = __DIR__ . '/../../config/sets/wp-6.6.php';
public const WP_6_7 = __DIR__ . '/../../config/sets/wp-6.7.php';
public const WP_6_8 = __DIR__ . '/../../config/sets/wp-6.8.php';
public const WP_6_9 = __DIR__ . '/../../config/sets/wp-6.9.php';
public const WP_7_0 = __DIR__ . '/../../config/sets/wp-7.0.php';
}
33 changes: 33 additions & 0 deletions src/ValueObject/FunctionRenameWithArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Fsylum\RectorWordPress\ValueObject;

final readonly class FunctionRenameWithArguments
{
/**
* @param array<mixed> $arguments
*/
public function __construct(
private string $oldFunction,
private string $newFunction,
private array $arguments
) {}

public function getOldFunction(): string
{
return $this->oldFunction;
}

public function getNewFunction(): string
{
return $this->newFunction;
}

/**
* @return array<mixed>
*/
public function getArguments(): array
{
return $this->arguments;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fsylum\RectorWordPress\Tests\Rector\Rules\FuncCall\RenameFunctionWithArgumentsRector;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @internal
*/
final class RenameFunctionWithArgumentsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideCases')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideCases(): iterable
{
return self::yieldFilesFromDirectory(__DIR__);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Fsylum\RectorWordPress\Rules\FuncCall\RenameFunctionWithArgumentsRector;
use Fsylum\RectorWordPress\ValueObject\FunctionRenameWithArguments;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(RenameFunctionWithArgumentsRector::class, [
new FunctionRenameWithArguments('utf8_encode', 'mb_convert_encoding', ['UTF-8', 'ISO-8859-1']),
new FunctionRenameWithArguments('utf8_decode', 'mb_convert_encoding', ['ISO-8859-1', 'UTF-8']),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

utf8_encode('foo');
utf8_decode('foo');

?>
-----
<?php

mb_convert_encoding('foo', 'UTF-8', 'ISO-8859-1');
mb_convert_encoding('foo', 'ISO-8859-1', 'UTF-8');

?>
28 changes: 28 additions & 0 deletions tests/Rector/Sets/Level/UpToWp70/UpToWp70Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fsylum\RectorWordPress\Tests\Rector\Sets\Level\UpToWp70;

use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @internal
*/
final class UpToWp70Test extends AbstractRectorTestCase
{
#[DataProvider('provideCases')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideCases(): iterable
{
return self::yieldFilesFromDirectory(__DIR__);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/../../../../../config/sets/level/up-to-wp-7.0.php';
}
}
Loading