Skip to content
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

[Php82] Add Utf8DecodeEncodeToMbConvertEncodingRector #3195

Merged
merged 9 commits into from
Dec 14, 2022
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
3 changes: 2 additions & 1 deletion config/set/php82.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Rector\Config\RectorConfig;
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
use Rector\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReadOnlyClassRector::class);
$rectorConfig->rules([ReadOnlyClassRector::class, Utf8DecodeEncodeToMbConvertEncodingRector::class]);
};
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');
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');

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

?>
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';
}
}
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);
};
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;
}
}
6 changes: 6 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,10 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_NULL_ARG_IN_STRING_FUNCTION = PhpVersion::PHP_81;

/**
* @see https://wiki.php.net/rfc/remove_utf8_decode_and_utf8_encode
* @var int
*/
public const DEPRECATE_UTF8_DECODE_ENCODE_FUNCTION = PhpVersion::PHP_82;
}