Skip to content

Commit

Permalink
fix unknown type infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 9, 2022
1 parent 781122e commit 3964aff
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 47 deletions.
9 changes: 5 additions & 4 deletions config/phpstan/parser.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this file overrides definitions from the config above
services:
defaultAnalysisParser:
factory: @pathRoutingParser
factory: @rectorPathRoutingParser
arguments!: []

cachedRectorParser:
Expand All @@ -18,12 +18,13 @@ services:
wrappedParser: @currentPhpVersionSimpleDirectParser
autowired: no

pathRoutingParser:
rectorPathRoutingParser:
class: Rector\Core\PhpParser\Parser\RectorPathRoutingParser
arguments:
phpstanPathRoutingParser: @pathRoutingParser
currentPhpVersionRichParser: @cachedRectorParser
currentPhpVersionSimpleParser: @rectorSimpleParser
php8Parser: @php8Parser
# currentPhpVersionSimpleParser: @rectorSimpleParser
# php8Parser: @php8Parser
autowired: no

rectorParser:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,26 @@ final class ClueJsonDecode
}
}
}
-----
<?php

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

/**
* @see \Clue\React\NDJson\Decoder
*/
final class ClueJsonDecode
{
private $assoc;
private $depth;
private $options;

public function run($data)
{
if ($this->options === 0) {
$data = \json_decode($data, $this->assoc === null ? true : false, $this->depth);
} else {
$data = \json_decode($data, $this->assoc === null ? true : false, $this->depth, $this->options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class PossiblyNull
{
function run(string $json, ?bool $associative)
{
if ($associative === null) {
$associative = true;
}
$value = json_decode($json, $associative);
$value = json_decode($json, $associative === null ? true : false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
namespace Rector\DowngradePhp72\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
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 PhpParser\Node\Stmt\Expression;
use PHPStan\Type\BooleanType;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -29,7 +26,6 @@ final class DowngradeJsonDecodeNullAssociativeArgRector extends AbstractRector
{
public function __construct(
private readonly ArgsAnalyzer $argsAnalyzer,
private readonly IfManipulator $ifManipulator
) {
}

Expand Down Expand Up @@ -104,14 +100,14 @@ public function refactor(Node $node): ?Node
return null;
}

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

if ($type instanceof BooleanType) {
if ($associativeValueType instanceof BooleanType) {
return null;
}

if ($associativeValue instanceof ConstFetch && $this->valueResolver->isNull($associativeValue)) {
$node->args[1]->value = $this->nodeFactory->createTrue();
$args[1]->value = $this->nodeFactory->createTrue();
return $node;
}

Expand All @@ -123,12 +119,11 @@ public function refactor(Node $node): ?Node
return null;
}

$currentExpression = $node->getAttribute(AttributeKey::CURRENT_STATEMENT);
$if = $this->ifManipulator->createIfExpr(
new Identical($associativeValue, $this->nodeFactory->createNull()),
new Expression(new Assign($associativeValue, $this->nodeFactory->createTrue()))
);
$this->nodesToAddCollector->addNodeBeforeNode($if, $currentExpression);
$condition = new Identical($associativeValue, $this->nodeFactory->createNull());

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

return $node;
}
Expand Down
58 changes: 33 additions & 25 deletions src/PhpParser/Parser/RectorPathRoutingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,56 @@
*/
final class RectorPathRoutingParser implements \PHPStan\Parser\Parser
{
/**
* @var array<string, bool> FilePath (string) => bool (true)
*/
private $analysedFiles = [];
// /**
// * @var array<string, bool> FilePath (string) => bool (true)
// */
// private $analysedFiles = [];

public function __construct(
private FileHelper $fileHelper,
private Parser $phpstanPathRoutingParser,
// private FileHelper $fileHelper,
private Parser $currentPhpVersionRichParser,
private Parser $currentPhpVersionSimpleParser,
private Parser $php8Parser
// private Parser $currentPhpVersionSimpleParser,
// private Parser $php8Parser
) {
}

/**
* @param string[] $files
*/
public function setAnalysedFiles(array $files): void
{
$this->analysedFiles = \array_fill_keys($files, \true);
}
//
// /**
// * @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);
}
// 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);
}

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

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->currentPhpVersionSimpleParser->parseString($sourceCode);
return $this->phpstanPathRoutingParser->parseString($sourceCode);
}
}

0 comments on commit 3964aff

Please sign in to comment.