Skip to content

Commit

Permalink
[DowngradePhp72] Add DowngradePhp72JsonConstRector (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 4, 2022
1 parent 1090aa6 commit 0a63fe4
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/set/downgrade-php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -21,4 +22,5 @@
$services->set(DowngradePregUnmatchedAsNullConstantRector::class);
$services->set(DowngradeStreamIsattyRector::class);
$services->set(DowngradeJsonDecodeNullAssociativeArgRector::class);
$services->set(DowngradePhp72JsonConstRector::class);
};
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

?>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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'));
}
}

0 comments on commit 0a63fe4

Please sign in to comment.