-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DowngradePhp71] Add DowngradePhp71JsonConstRector (#1784)
- Loading branch information
1 parent
78a42c7
commit 6aa48e1
Showing
8 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...p71/Rector/ConstFetch/DowngradePhp71JsonConstRector/DowngradePhp71JsonConstRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\ConstFetch\DowngradePhp71JsonConstRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradePhp71JsonConstRectorTest 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'; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ts/DowngradePhp71/Rector/ConstFetch/DowngradePhp71JsonConstRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\FuncCall\DowngradePhp71JsonConstRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
$inDecoder = new Decoder($connection, true, 512, \JSON_UNESCAPED_LINE_TERMINATORS); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\FuncCall\DowngradePhp71JsonConstRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
$inDecoder = new Decoder($connection, true, 512, 0); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...DowngradePhp71/Rector/ConstFetch/DowngradePhp71JsonConstRector/Fixture/in_bitwise.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\FuncCall\DowngradePhp71JsonConstRector\Fixture; | ||
|
||
class InBitwise | ||
{ | ||
public function run($argument) | ||
{ | ||
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\FuncCall\DowngradePhp71JsonConstRector\Fixture; | ||
|
||
class InBitwise | ||
{ | ||
public function run($argument) | ||
{ | ||
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...ConstFetch/DowngradePhp71JsonConstRector/Fixture/skip_bitwise_different_constants.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\FuncCall\DowngradePhp71JsonConstRector\Fixture; | ||
|
||
class SkipBitwiseDifferentConstant | ||
{ | ||
public function run($argument) | ||
{ | ||
$argument = json_encode($argument, JSON_HEX_TAG | JSON_PRETTY_PRINT); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...1/Rector/ConstFetch/DowngradePhp71JsonConstRector/Fixture/skip_different_constant.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp71\Rector\FuncCall\DowngradePhp71JsonConstRector\Fixture; | ||
|
||
class SkipDifferentConstant | ||
{ | ||
public function run() | ||
{ | ||
json_encode($a, JSON_HEX_TAG); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...DowngradePhp71/Rector/ConstFetch/DowngradePhp71JsonConstRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\DowngradePhp71\Rector\ConstFetch\DowngradePhp71JsonConstRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradePhp71JsonConstRector::class); | ||
}; |
65 changes: 65 additions & 0 deletions
65
rules/DowngradePhp71/Rector/ConstFetch/DowngradePhp71JsonConstRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp71\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\DowngradePhp71\Rector\ConstFetch\DowngradePhp71JsonConstRector\DowngradePhp71JsonConstRectorTest | ||
*/ | ||
final class DowngradePhp71JsonConstRector extends AbstractRector | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private const CONSTANTS = ['JSON_UNESCAPED_LINE_TERMINATORS']; | ||
|
||
public function __construct( | ||
private readonly JsonConstCleaner $jsonConstCleaner | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Remove Json constant that available only in php 7.1', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
json_encode($content, JSON_UNESCAPED_LINE_TERMINATORS); | ||
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); | ||
} | ||
} |