-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
1 parent
c7e4ba9
commit 20351c5
Showing
7 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...DowngradePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector/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\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); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...ePhp72/Rector/ConstFetch/DowngradePhp72JsonConstRector/Fixture/in_bitwise_use_all.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\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); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...or/ConstFetch/DowngradePhp72JsonConstRector/Fixture/in_bitwise_use_all_in_between.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\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); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...etch/DowngradePhp72JsonConstRector/Fixture/in_bitwise_use_all_in_between_variable.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\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); | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...ConstFetch/DowngradePhp72JsonConstRector/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\DowngradePhp72\Rector\FuncCall\DowngradePhp72JsonConstRector\Fixture; | ||
|
||
class SkipBitwiseDifferentConstant | ||
{ | ||
public function run($argument) | ||
{ | ||
$argument = json_encode($argument, JSON_HEX_TAG | JSON_PRETTY_PRINT); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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