-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DowngradePhp72] Add DowngradeJsonDecodeNullAssociativeArgRector (#1723)
* [DowngradePhp72] Add DowngradeJsonDecodeNullAssociativeArgRector * add fixture test * add fixture test * implemented for exactly null * skip already casted bool * rectify * check original node * resolved by created by rule
- Loading branch information
1 parent
e6d3388
commit 7cf2421
Showing
8 changed files
with
250 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
34 changes: 34 additions & 0 deletions
34
...adeJsonDecodeNullAssociativeArgRector/DowngradeJsonDecodeNullAssociativeArgRectorTest.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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeJsonDecodeNullAssociativeArgRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @requires PHP 7.2 | ||
* @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
...Php72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector/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\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
$value = json_decode('{}', null); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
$value = json_decode('{}', false); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector/Fixture/possibly_null.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\DowngradeJsonDecodeNullAssociativeArgRector\Fixture; | ||
|
||
class PossiblyNull | ||
{ | ||
function run(string $json, ?bool $associative) | ||
{ | ||
$value = json_decode($json, $associative); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture; | ||
|
||
class PossiblyNull | ||
{ | ||
function run(string $json, ?bool $associative) | ||
{ | ||
$value = json_decode($json, (bool) $associative); | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...Call/DowngradeJsonDecodeNullAssociativeArgRector/Fixture/skip_already_casted_bool.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\DowngradeJsonDecodeNullAssociativeArgRector\Fixture; | ||
|
||
class SkipAlreadyCastedBool | ||
{ | ||
function run(string $json, ?bool $associative) | ||
{ | ||
$value = json_decode($json, (bool) $associative); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector/Fixture/skip_bool_type.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\DowngradeJsonDecodeNullAssociativeArgRector\Fixture; | ||
|
||
class SkipBoolType | ||
{ | ||
public function run(bool $associative) | ||
{ | ||
$value = json_decode('{}', $associative); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector/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\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(DowngradeJsonDecodeNullAssociativeArgRector::class); | ||
}; |
115 changes: 115 additions & 0 deletions
115
rules/DowngradePhp72/Rector/FuncCall/DowngradeJsonDecodeNullAssociativeArgRector.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,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp72\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Cast\Bool_; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Type\BooleanType; | ||
use Rector\Core\NodeAnalyzer\ArgsAnalyzer; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\DowngradeJsonDecodeNullAssociativeArgRectorTest | ||
*/ | ||
final class DowngradeJsonDecodeNullAssociativeArgRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly ArgsAnalyzer $argsAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Downgrade json_decode() with null associative argument function', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
declare(strict_types=1); | ||
function exactlyNull(string $json) | ||
{ | ||
$value = json_decode($json, null); | ||
} | ||
function possiblyNull(string $json, ?bool $associative) | ||
{ | ||
$value = json_decode($json, $associative); | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
declare(strict_types=1); | ||
function exactlyNull(string $json) | ||
{ | ||
$value = json_decode($json, false); | ||
} | ||
function possiblyNull(string $json, ?bool $associative) | ||
{ | ||
$value = json_decode($json, (bool) $associative); | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node, 'json_decode')) { | ||
return null; | ||
} | ||
|
||
$createdByRule = $node->getAttribute(AttributeKey::CREATED_BY_RULE) ?? []; | ||
if (in_array(self::class, $createdByRule, true)) { | ||
return null; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
if ($this->argsAnalyzer->hasNamedArg($args)) { | ||
return null; | ||
} | ||
|
||
if (! isset($args[1])) { | ||
return null; | ||
} | ||
|
||
$associativeValue = $args[1]->value; | ||
|
||
if ($associativeValue instanceof Bool_) { | ||
return null; | ||
} | ||
|
||
$type = $this->nodeTypeResolver->getType($associativeValue); | ||
|
||
if ($type instanceof BooleanType) { | ||
return null; | ||
} | ||
|
||
if ($associativeValue instanceof ConstFetch && $this->valueResolver->isNull($associativeValue)) { | ||
$node->args[1]->value = $this->nodeFactory->createFalse(); | ||
return $node; | ||
} | ||
|
||
$node->args[1]->value = new Bool_($associativeValue); | ||
return $node; | ||
} | ||
} |