Skip to content

Commit

Permalink
[DowngradePhp72] Add DowngradeJsonDecodeNullAssociativeArgRector (#1723)
Browse files Browse the repository at this point in the history
* [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
samsonasik authored Jan 25, 2022
1 parent e6d3388 commit 7cf2421
Show file tree
Hide file tree
Showing 8 changed files with 250 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\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
Expand All @@ -19,4 +20,5 @@
$services->set(DowngradeParameterTypeWideningRector::class);
$services->set(DowngradePregUnmatchedAsNullConstantRector::class);
$services->set(DowngradeStreamIsattyRector::class);
$services->set(DowngradeJsonDecodeNullAssociativeArgRector::class);
};
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';
}
}
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);
}
}

?>
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);
}
}

?>
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);
}
}
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);
}
}
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);
};
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;
}
}

0 comments on commit 7cf2421

Please sign in to comment.