-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add WordPress 6.9 and 7.0 upgrade rule sets #14
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
Open
abhishek-kaushik
wants to merge
1
commit into
fsylum:main
Choose a base branch
from
abhishek-kaushik:feature-wp-6.9-7.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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 | ||
|
|
||
| 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]); | ||
| }; |
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 | ||
|
|
||
| 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]); | ||
| }; |
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,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']), | ||
| ]); | ||
|
|
||
| /* | ||
| * TODO: these are not handled currently | ||
| * | ||
| * ARGUMENTS | ||
| * - _wp_can_use_pcre_u (the $set argument is no longer used; private/internal function) | ||
| */ | ||
| }; | ||
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,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. | ||
| */ | ||
| }; |
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,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'])] | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
| } |
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
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
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,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; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...ules/FuncCall/RenameFunctionWithArgumentsRector/RenameFunctionWithArgumentsRectorTest.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,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'; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
tests/Rector/Rules/FuncCall/RenameFunctionWithArgumentsRector/config.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,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']), | ||
| ]); | ||
| }; |
13 changes: 13 additions & 0 deletions
13
tests/Rector/Rules/FuncCall/RenameFunctionWithArgumentsRector/test_fixture.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,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'); | ||
|
|
||
| ?> |
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,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'; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
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
FunctionRenameWithArgumentsI think?