mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 03:40:50 +00:00
[DowngradePhp72] Add DowngradePhp72JsonConstRector (#1765)
This commit is contained in:
parent
1090aa6e8a
commit
0a63fe4d52
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
|
||||
use Rector\DowngradePhp72\Rector\ConstFetch\DowngradePhp72JsonConstRector;
|
||||
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector;
|
||||
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
|
||||
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
|
||||
|
@ -21,4 +22,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(DowngradePregUnmatchedAsNullConstantRector::class);
|
||||
$services->set(DowngradeStreamIsattyRector::class);
|
||||
$services->set(DowngradeJsonDecodeNullAssociativeArgRector::class);
|
||||
$services->set(DowngradePhp72JsonConstRector::class);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\ConstFetch\DowngradePhp72JsonConstRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class DowngradePhp72JsonConstRectorTest 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\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$inDecoder = new Decoder($connection, true, 512, \JSON_INVALID_UTF8_IGNORE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$inDecoder = new Decoder($connection, true, 512, 0);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class SkipDifferentConstant
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
json_encode($a, JSON_HEX_TAG);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\DowngradePhp72\Rector\ConstFetch\DowngradePhp72JsonConstRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(DowngradePhp72JsonConstRector::class);
|
||||
};
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp72\Rector\ConstFetch;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Name;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
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\DowngradePhp72\Rector\ConstFetch\DowngradePhp72JsonConstRector\DowngradePhp72JsonConstRectorTest
|
||||
*/
|
||||
final class DowngradePhp72JsonConstRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var array<string>
|
||||
*/
|
||||
private const CONSTANTS = ['JSON_INVALID_UTF8_IGNORE', 'JSON_INVALID_UTF8_SUBSTITUTE'];
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Change Json constant that available only in php 7.2 to 0',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$inDecoder = new Decoder($connection, true, 512, \JSON_INVALID_UTF8_IGNORE);
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$inDecoder = new Decoder($connection, true, 512, 0);
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ConstFetch::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConstFetch $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->nodeNameResolver->isNames($node, self::CONSTANTS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ConstFetch(new Name('0'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user