Add Yaml::parse(, Yaml::PARSE_KEYS_AS_STRINGS) case

This commit is contained in:
Tomas Votruba 2018-07-17 22:48:31 +02:00
parent 8122ae6f17
commit f2cea259d2
8 changed files with 85 additions and 4 deletions

View File

@ -7,3 +7,11 @@ services:
' !php/object:': ' !php/object '
' !php/const:': ' !php/const '
' !str': ' !!str'
Rector\Rector\Dynamic\ArgumentRemoverRector:
$argumentChangesByMethodAndType:
-
class: 'Symfony\Component\Yaml\Yaml'
method: 'parse'
position: 2
value: 'Symfony\Component\Yaml\Yaml::PARSE_KEYS_AS_STRINGS'

View File

@ -4,4 +4,19 @@ namespace Rector\Configuration\Rector;
final class ArgumentRemoverRecipe extends AbstractArgumentRecipe
{
/**
* @var null|string
*/
private $value;
public function __construct(string $class, string $method, int $position, ?string $value)
{
parent::__construct($class, $method, $position);
$this->value = $value;
}
public function getValue(): ?string
{
return $this->value;
}
}

View File

@ -11,6 +11,6 @@ final class ArgumentRemoverRecipeFactory extends AbstractArgumentRecipeFactory
{
$this->validateArrayData($data);
return new ArgumentRemoverRecipe($data['class'], $data['method'], $data['position']);
return new ArgumentRemoverRecipe($data['class'], $data['method'], $data['position'], $data['value'] ?? null);
}
}

View File

@ -3,11 +3,14 @@
namespace Rector\Rector\Dynamic;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Configuration\Rector\ArgumentRemoverRecipe;
use Rector\Configuration\Rector\ArgumentRemoverRecipeFactory;
use Rector\Node\Attribute;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -106,16 +109,38 @@ CODE_SAMPLE
}
/**
* @param mixed[] $argumentNodes
* @param Arg[] $argumentNodes
* @return mixed[]
*/
private function processArgumentNodes(array $argumentNodes): array
{
foreach ($this->activeArgumentRemoverRecipes as $activeArgumentRemoverRecipe) {
$position = $activeArgumentRemoverRecipe->getPosition();
unset($argumentNodes[$position]);
if ($activeArgumentRemoverRecipe->getValue() === null) {
unset($argumentNodes[$position]);
} elseif ($this->isArgumentValueMatch($argumentNodes[$position], $activeArgumentRemoverRecipe)) {
unset($argumentNodes[$position]);
}
}
return $argumentNodes;
}
private function isArgumentValueMatch(Arg $argNode, ArgumentRemoverRecipe $argumentRemoverRecipe): bool
{
$valueNode = $argNode->value;
if ($valueNode instanceof ClassConstFetch) {
$valueNodeAsString = $valueNode->class->getAttribute(Attribute::RESOLVED_NAME)->toString()
. '::'
. $valueNode->name->toString();
if ($valueNodeAsString === $argumentRemoverRecipe->getValue()) {
return true;
}
}
return false;
}
}

View File

@ -17,7 +17,8 @@ final class ArgumentRemoverRectorTest extends AbstractRectorTestCase
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
// yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
}
protected function provideConfig(): string

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
use Symfony\Component\Yaml\Yaml;
$yaml = [];
Symfony\Component\Yaml\Yaml::parse($yaml);
Symfony\Component\Yaml\Yaml::parse($yaml);
Symfony\Component\Yaml\Yaml::parse($yaml, Symfony\Component\Yaml\Yaml::PARSE_CONSTANT);
Yaml::parse($yaml);

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
use Symfony\Component\Yaml\Yaml;
$yaml = [];
Symfony\Component\Yaml\Yaml::parse($yaml, Symfony\Component\Yaml\Yaml::PARSE_KEYS_AS_STRINGS);
Symfony\Component\Yaml\Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);
Symfony\Component\Yaml\Yaml::parse($yaml, Symfony\Component\Yaml\Yaml::PARSE_CONSTANT);
Yaml::parse($yaml, Yaml::PARSE_KEYS_AS_STRINGS);

View File

@ -5,3 +5,9 @@ services:
class: 'Doctrine\ORM\Persisters\Entity\AbstractEntityInheritancePersister'
method: 'getSelectJoinColumnSQL'
position: 4
-
class: 'Symfony\Component\Yaml\Yaml'
method: 'parse'
position: 1
value: 'Symfony\Component\Yaml\Yaml::PARSE_KEYS_AS_STRINGS'