mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-15 23:52:23 +00:00
[DowngradePhp72] Handle in BitwiseOr on DowngradePhp72JsonConstRector (#1781)
* [DowngradePhp72] Handle in bitwise on DowngradePhp72JsonConstRector
* add fixture for all used
* fixture update
* Fixed 🎉
* more example
* cs fix
* fixture bitwise different constant
* clean up
* more fixture
* final touch: clean up
* final touch: move logic to new service: JsonConstCleaner for re-use for next new services: DowngradePhp73JsonConstRector, DowngradePhp74JsonConstRector
* final touch: clean up
* final touch: clean up
This commit is contained in:
parent
c7e4ba97fd
commit
20351c5d12
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwise
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwise
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwiseUseAll
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwiseUseAll
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, 0);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwiseUseAllInBetween
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_INVALID_UTF8_IGNORE | JSON_HEX_TAG | JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwiseUseAllInBetween
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_HEX_TAG);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwiseUseAllInBetweenVariable
|
||||
{
|
||||
public function run($argument, $variable)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_INVALID_UTF8_IGNORE | $variable | JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class InBitwiseUseAllInBetweenVariable
|
||||
{
|
||||
public function run($argument, $variable)
|
||||
{
|
||||
$argument = json_encode($argument, $variable);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture;
|
||||
|
||||
class SkipBitwiseDifferentConstant
|
||||
{
|
||||
public function run($argument)
|
||||
{
|
||||
$argument = json_encode($argument, JSON_HEX_TAG | JSON_PRETTY_PRINT);
|
||||
}
|
||||
}
|
80
rules/DowngradePhp72/NodeManipulator/JsonConstCleaner.php
Normal file
80
rules/DowngradePhp72/NodeManipulator/JsonConstCleaner.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\DowngradePhp72\NodeManipulator;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Name;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class JsonConstCleaner
|
||||
{
|
||||
public function __construct(
|
||||
private readonly NodeNameResolver $nodeNameResolver
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $constants
|
||||
*/
|
||||
public function clean(ConstFetch|BitwiseOr $node, array $constants): ConstFetch|Expr|null
|
||||
{
|
||||
if ($node instanceof ConstFetch) {
|
||||
return $this->cleanByConstFetch($node, $constants);
|
||||
}
|
||||
|
||||
return $this->cleanByBitwiseOr($node, $constants);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $constants
|
||||
*/
|
||||
private function cleanByConstFetch(ConstFetch $constFetch, array $constants): ?ConstFetch
|
||||
{
|
||||
if (! $this->nodeNameResolver->isNames($constFetch, $constants)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parent = $constFetch->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (! $parent instanceof BitwiseOr) {
|
||||
return new ConstFetch(new Name('0'));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $constants
|
||||
*/
|
||||
private function cleanByBitwiseOr(BitwiseOr $bitwiseOr, array $constants): ?Expr
|
||||
{
|
||||
$isLeftTransformed = $this->isTransformed($bitwiseOr->left, $constants);
|
||||
$isRightTransformed = $this->isTransformed($bitwiseOr->right, $constants);
|
||||
|
||||
if (! $isLeftTransformed && ! $isRightTransformed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $isLeftTransformed) {
|
||||
return $bitwiseOr->left;
|
||||
}
|
||||
|
||||
if (! $isRightTransformed) {
|
||||
return $bitwiseOr->right;
|
||||
}
|
||||
|
||||
return new ConstFetch(new Name('0'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $constants
|
||||
*/
|
||||
private function isTransformed(Expr $expr, array $constants): bool
|
||||
{
|
||||
return $expr instanceof ConstFetch && $this->nodeNameResolver->isNames($expr, $constants);
|
||||
}
|
||||
}
|
|
@ -5,9 +5,10 @@ declare(strict_types=1);
|
|||
namespace Rector\DowngradePhp72\Rector\ConstFetch;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Name;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DowngradePhp72\NodeManipulator\JsonConstCleaner;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
|
@ -23,6 +24,11 @@ final class DowngradePhp72JsonConstRector extends AbstractRector
|
|||
*/
|
||||
private const CONSTANTS = ['JSON_INVALID_UTF8_IGNORE', 'JSON_INVALID_UTF8_SUBSTITUTE'];
|
||||
|
||||
public function __construct(
|
||||
private readonly JsonConstCleaner $jsonConstCleaner
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
|
@ -31,10 +37,12 @@ final class DowngradePhp72JsonConstRector extends AbstractRector
|
|||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$inDecoder = new Decoder($connection, true, 512, \JSON_INVALID_UTF8_IGNORE);
|
||||
$inDecoder = new Decoder($connection, true, 512, \JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$inDecoder = new Decoder($connection, true, 512, 0);
|
||||
$inDecoder = new Decoder($connection, true, 512, 0);
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
|
@ -46,18 +54,14 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ConstFetch::class];
|
||||
return [ConstFetch::class, BitwiseOr::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConstFetch $node
|
||||
* @param ConstFetch|BitwiseOr $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->nodeNameResolver->isNames($node, self::CONSTANTS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ConstFetch(new Name('0'));
|
||||
return $this->jsonConstCleaner->clean($node, self::CONSTANTS);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user