mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 08:50:50 +00:00
[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:
parent
e2bd1841f4
commit
78a42c7b8a
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
};
|
|
@ -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'
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user