-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php82] Add Utf8DecodeEncodeToMbConvertEncodingRector (#3195)
* [Php82] Add Utf8DecodeToMbConvertEncodingRector * more @see * [ci-review] Rector Rectify * rename to Utf8DecodeEncodeToMbConvertEncodingRector * rename * update description * implemented * Final touch: space * Really Final touch: add @see in php version feature constant Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
f20fd6e
commit c21cafa
Showing
8 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
7 changes: 7 additions & 0 deletions
7
...ll/Utf8DecodeEncodeToMbConvertEncodingRector/Fixture/skip_different_function_name.php.inc
This file contains 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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Fixture; | ||
|
||
strlen('test'); |
21 changes: 21 additions & 0 deletions
21
...ector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector/Fixture/some_utf8_decode.php.inc
This file contains 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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Fixture; | ||
|
||
$utf8_string = "\x5A\x6F\xC3\xAB"; | ||
utf8_decode($utf8_string); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Fixture; | ||
|
||
$utf8_string = "\x5A\x6F\xC3\xAB"; | ||
mb_convert_encoding($utf8_string, 'ISO-8859-1'); | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
...ector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector/Fixture/some_utf8_encode.php.inc
This file contains 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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Fixture; | ||
|
||
$text = "\xE0"; | ||
utf8_encode($text); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Fixture; | ||
|
||
$text = "\xE0"; | ||
mb_convert_encoding($text, 'UTF-8', 'ISO-8859-1'); | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...f8DecodeEncodeToMbConvertEncodingRector/Utf8DecodeEncodeToMbConvertEncodingRectorTest.php
This file contains 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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class Utf8DecodeEncodeToMbConvertEncodingRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...hp82/Rector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector/config/configured_rule.php
This file contains 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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(Utf8DecodeEncodeToMbConvertEncodingRector::class); | ||
}; |
83 changes: 83 additions & 0 deletions
83
rules/Php82/Rector/FuncCall/Utf8DecodeEncodeToMbConvertEncodingRector.php
This file contains 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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php82\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://wiki.php.net/rfc/remove_utf8_decode_and_utf8_encode | ||
* | ||
* @see https://3v4l.org/Q14UR | ||
* @see \Rector\Tests\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector\Utf8DecodeEncodeToMbConvertEncodingRectorTest | ||
*/ | ||
final class Utf8DecodeEncodeToMbConvertEncodingRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change deprecated utf8_decode and utf8_encode to mb_convert_encoding', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
utf8_decode($value); | ||
utf8_encode($value); | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
mb_convert_encoding($value, 'ISO-8859-1'); | ||
mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1'); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if ($this->isName($node, 'utf8_decode')) { | ||
$node->name = new Name('mb_convert_encoding'); | ||
$node->args[1] = new Arg(new String_('ISO-8859-1')); | ||
|
||
return $node; | ||
} | ||
|
||
if ($this->isName($node, 'utf8_encode')) { | ||
$node->name = new Name('mb_convert_encoding'); | ||
$node->args[1] = new Arg(new String_('UTF-8')); | ||
$node->args[2] = new Arg(new String_('ISO-8859-1')); | ||
|
||
return $node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::DEPRECATE_UTF8_DECODE_ENCODE_FUNCTION; | ||
} | ||
} |
This file contains 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