[DowngradePhp73] Add DowngradePhp73JsonConstRector (#1782)

* [DowngradePhp73] Add DowngradePhp73JsonConstRector

* register

* clean up

* clean up

* [ci-review] Rector Rectify

* phpstan

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-02-09 05:12:41 +07:00 committed by GitHub
parent e2bd1841f4
commit 78a42c7b8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 200 additions and 1 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeIsCountableRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
@ -24,4 +25,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DowngradeArrayKeyFirstLastRector::class);
$services->set(SetCookieOptionsArrayToArgumentsRector::class);
$services->set(DowngradeIsCountableRector::class);
$services->set(DowngradePhp73JsonConstRector::class);
};

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradePhp73JsonConstRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradePhp73JsonConstRector\Fixture;
class Fixture
{
public function run()
{
$inDecoder = new Decoder($connection, true, 512, \JSON_THROW_ON_ERROR);
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradePhp73JsonConstRector\Fixture;
class Fixture
{
public function run()
{
$inDecoder = new Decoder($connection, true, 512, 0);
}
}
?>

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradePhp73JsonConstRector\Fixture;
class InBitwise
{
public function run($argument)
{
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradePhp73JsonConstRector\Fixture;
class InBitwise
{
public function run($argument)
{
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES);
}
}
?>

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradePhp73JsonConstRector\Fixture;
class SkipBitwiseDifferentConstant
{
public function run($argument)
{
$argument = json_encode($argument, JSON_HEX_TAG | JSON_PRETTY_PRINT);
}
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradePhp73JsonConstRector\Fixture;
class SkipDifferentConstant
{
public function run()
{
json_encode($a, JSON_HEX_TAG);
}
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
use Rector\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradePhp73JsonConstRector::class);
};

View File

@ -32,7 +32,7 @@ final class DowngradePhp72JsonConstRector extends AbstractRector
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change Json constant that available only in php 7.2 to 0',
'Remove Json constant that available only in php 7.2',
[
new CodeSample(
<<<'CODE_SAMPLE'

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp73\Rector\ConstFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
use PhpParser\Node\Expr\ConstFetch;
use Rector\Core\Rector\AbstractRector;
use Rector\DowngradePhp72\NodeManipulator\JsonConstCleaner;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://www.php.net/manual/en/function.json-encode.php#refsect1-function.json-encode-changelog
*
* @see \Rector\Tests\DowngradePhp73\Rector\ConstFetch\DowngradePhp73JsonConstRector\DowngradePhp73JsonConstRectorTest
*/
final class DowngradePhp73JsonConstRector extends AbstractRector
{
/**
* @var string[]
*/
private const CONSTANTS = ['JSON_THROW_ON_ERROR'];
public function __construct(
private readonly JsonConstCleaner $jsonConstCleaner
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove Json constant that available only in php 7.3',
[
new CodeSample(
<<<'CODE_SAMPLE'
json_encode($content, JSON_THROW_ON_ERROR);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
json_encode($content, 0);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ConstFetch::class, BitwiseOr::class];
}
/**
* @param ConstFetch|BitwiseOr $node
*/
public function refactor(Node $node): ?Node
{
return $this->jsonConstCleaner->clean($node, self::CONSTANTS);
}
}