Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 9, 2022
1 parent 7642518 commit 2be2313
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 60 deletions.
2 changes: 0 additions & 2 deletions config/phpstan/parser.neon
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ services:
arguments:
phpstanPathRoutingParser: @pathRoutingParser
currentPhpVersionRichParser: @cachedRectorParser
# currentPhpVersionSimpleParser: @rectorSimpleParser
# php8Parser: @php8Parser
autowired: no

rectorParser:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

final class ArrayAssoc
{
public function run($data, array $items)
{
$data = \json_decode($data, $items[0]);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeJsonDecodeNullAssociativeArgRector\Fixture;

final class ArrayAssoc
{
public function run($data, array $items)
{
$data = \json_decode($data, $items[0] === null);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class ClueJsonDecode
}
}
}

?>
-----
<?php

Expand All @@ -37,9 +39,11 @@ final class ClueJsonDecode
public function run($data)
{
if ($this->options === 0) {
$data = \json_decode($data, $this->assoc === null ? true : false, $this->depth);
$data = \json_decode($data, $this->assoc === null, $this->depth);
} else {
$data = \json_decode($data, $this->assoc === null ? true : false, $this->depth, $this->options);
$data = \json_decode($data, $this->assoc === null, $this->depth, $this->options);
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PossiblyNull
{
function run(string $json, ?bool $associative)
{
$value = json_decode($json, $associative === null ? true : false);
$value = json_decode($json, $associative === null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\BooleanType;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -96,12 +92,7 @@ public function refactor(Node $node): ?Node

$associativeValue = $args[1]->value;

if ($associativeValue instanceof Bool_) {
return null;
}

$associativeValueType = $this->nodeTypeResolver->getType($associativeValue);

if ($associativeValueType instanceof BooleanType) {
return null;
}
Expand All @@ -111,19 +102,9 @@ public function refactor(Node $node): ?Node
return $node;
}

if (! in_array(
$associativeValue::class,
[Variable::class, PropertyFetch::class, StaticPropertyFetch::class],
true
)) {
return null;
}

$condition = new Identical($associativeValue, $this->nodeFactory->createNull());

// add conditional ternary
$ternary = new Ternary($condition, $this->nodeFactory->createTrue(), $this->nodeFactory->createFalse());
$args[1]->value = $ternary;
$nullIdentical = new Identical($associativeValue, $this->nodeFactory->createNull());
$args[1]->value = $nullIdentical;

return $node;
}
Expand Down
35 changes: 1 addition & 34 deletions src/PhpParser/Parser/RectorPathRoutingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Core\PhpParser\Parser;

use PHPStan\File\FileHelper;
use PHPStan\Parser\Parser;

/**
Expand All @@ -16,56 +15,24 @@
*
* Fixes https://github.com/rectorphp/rector/issues/6970
*/
final class RectorPathRoutingParser implements \PHPStan\Parser\Parser
final class RectorPathRoutingParser implements Parser
{
// /**
// * @var array<string, bool> FilePath (string) => bool (true)
// */
// private $analysedFiles = [];

public function __construct(
private Parser $phpstanPathRoutingParser,
// private FileHelper $fileHelper,
private Parser $currentPhpVersionRichParser,
// private Parser $currentPhpVersionSimpleParser,
// private Parser $php8Parser
) {
}
//
// /**
// * @param string[] $file
// */
// public function setAnalysedFiles(array $files): void
// {
// $this->analysedFiles = \array_fill_keys($files, \true);
// }

public function parseFile(string $file): array
{
// if (\strpos($this->fileHelper->normalizePath($file, '/'), 'vendor/jetbrains/phpstorm-stubs') !== \false) {
// return $this->php8Parser->parseFile($file);
// }

// for tests, always parse nodes with directly rich parser to be aware of types
if (defined('PHPUNIT_COMPOSER_INSTALL')) {
return $this->currentPhpVersionRichParser->parseFile($file);
}

return $this->phpstanPathRoutingParser->parseFile($file);
//
// $file = $this->fileHelper->normalizePath($file);
// if (! isset($this->analysedFiles[$file])) {
// return $this->currentPhpVersionSimpleParser->parseFile($file);
// }
//
// return $this->currentPhpVersionRichParser->parseFile($file);
}

//
// public function parseString(string $sourceCode): array
// {
// return $this->currentPhpVersionSimpleParser->parseString($sourceCode);
// }
public function parseString(string $sourceCode): array
{
return $this->phpstanPathRoutingParser->parseString($sourceCode);
Expand Down

0 comments on commit 2be2313

Please sign in to comment.