mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-25 09:55:28 +02:00
Add Yaml::parse(, Yaml::PARSE_KEYS_AS_STRINGS) case
This commit is contained in:
parent
8122ae6f17
commit
f2cea259d2
@ -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'
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
@ -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);
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user