Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use Symfony\Component\Yaml\Yaml;
class Fixture2 {
function argumentDefaultValue2()
{
Yaml::parse('...', true);
Yaml::parse('...', false);
Yaml::parse('...', false, true);
Yaml::parse('...', false, false, true);
}
}
Expand All @@ -25,9 +22,6 @@ use Symfony\Component\Yaml\Yaml;
class Fixture2 {
function argumentDefaultValue2()
{
Yaml::parse('...', \Symfony\Component\Yaml\Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
Yaml::parse('...', 0);
Yaml::parse('...', \Symfony\Component\Yaml\Yaml::PARSE_OBJECT);
Yaml::parse('...', \Symfony\Component\Yaml\Yaml::PARSE_OBJECT_FOR_MAP);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;

final class ReplaceInConstructor
{
public function __construct(string $someValue)
{
}
}


$result = new ReplaceInConstructor('some value');

?>
-----
<?php

namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;

final class ReplaceInConstructor
{
public function __construct(string $someValue)
{
}
}


$result = new ReplaceInConstructor(\SomeClass::SOME_CONSTANT);

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
use Rector\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\MethodName;
use Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture\ReplaceInConstructor;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(ReplaceArgumentDefaultValueRector::class, [
// special case for constructor
new ReplaceArgumentDefaultValue(
ReplaceInConstructor::class,
MethodName::CONSTRUCT,
0,
'some value',
'SomeClass::SOME_CONSTANT'
),

new ReplaceArgumentDefaultValue(
'Symfony\Component\DependencyInjection\Definition',
Expand All @@ -22,29 +32,6 @@
false,
true,
], 'Symfony\Component\Yaml\Yaml::PARSE_OBJECT_FOR_MAP'),
new ReplaceArgumentDefaultValue('Symfony\Component\Yaml\Yaml', 'parse', 1, [
false,
true,
], 'Symfony\Component\Yaml\Yaml::PARSE_OBJECT'),
new ReplaceArgumentDefaultValue('Symfony\Component\Yaml\Yaml', 'parse', 1, false, 0),
new ReplaceArgumentDefaultValue(
'Symfony\Component\Yaml\Yaml',
'parse',
1,
true,
'Symfony\Component\Yaml\Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE'
),
new ReplaceArgumentDefaultValue('Symfony\Component\Yaml\Yaml', 'dump', 3, [
false,
true,
], 'Symfony\Component\Yaml\Yaml::DUMP_OBJECT'),
new ReplaceArgumentDefaultValue(
'Symfony\Component\Yaml\Yaml',
'dump',
3,
true,
'Symfony\Component\Yaml\Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE'
),

new ReplaceArgumentDefaultValue(
'Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Source\SomeClassWithAnyDefaultValue',
Expand Down
10 changes: 6 additions & 4 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface;
Expand All @@ -27,7 +28,7 @@ public function __construct(
}

public function processReplaces(
MethodCall | StaticCall | ClassMethod | FuncCall $node,
MethodCall | StaticCall | ClassMethod | FuncCall | New_ $node,
ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue
): ?Node {
if ($node instanceof ClassMethod) {
Expand Down Expand Up @@ -82,15 +83,16 @@ private function processParams(
}

private function processArgs(
MethodCall | StaticCall | FuncCall $expr,
MethodCall | StaticCall | FuncCall | New_ $expr,
ReplaceArgumentDefaultValueInterface $replaceArgumentDefaultValue
): ?Expr {
$position = $replaceArgumentDefaultValue->getPosition();
if (! $expr->args[$position] instanceof Arg) {
$particularArg = $expr->getArgs()[$position] ?? null;
if (! $particularArg instanceof Arg) {
return null;
}

$argValue = $this->valueResolver->getValue($expr->getArgs()[$position]->value);
$argValue = $this->valueResolver->getValue($particularArg->value);

if (is_scalar(
$replaceArgumentDefaultValue->getValueBefore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Arguments\ArgumentDefaultValueReplacer;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
Expand All @@ -25,7 +27,7 @@ final class ReplaceArgumentDefaultValueRector extends AbstractRector implements
/**
* @var ReplaceArgumentDefaultValue[]
*/
private array $replacedArguments = [];
private array $replaceArgumentDefaultValues = [];

public function __construct(
private readonly ArgumentDefaultValueReplacer $argumentDefaultValueReplacer
Expand Down Expand Up @@ -67,29 +69,33 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
return [MethodCall::class, StaticCall::class, ClassMethod::class, New_::class];
}

/**
* @param MethodCall|StaticCall|ClassMethod $node
* @param MethodCall|StaticCall|ClassMethod|New_ $node
*/
public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | null
public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | New_ | null
{
$hasChanged = false;

foreach ($this->replacedArguments as $replacedArgument) {
if (! $this->isName($node->name, $replacedArgument->getMethod())) {
if ($node instanceof New_) {
return $this->refactorNew($node);
}

foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
if (! $this->isName($node->name, $replaceArgumentDefaultValue->getMethod())) {
continue;
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
$replacedArgument->getObjectType()
$replaceArgumentDefaultValue->getObjectType()
)) {
continue;
}

$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replacedArgument);
$replacedNode = $this->argumentDefaultValueReplacer->processReplaces($node, $replaceArgumentDefaultValue);
if ($replacedNode instanceof Node) {
$hasChanged = true;
}
Expand All @@ -109,6 +115,23 @@ public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, ReplaceArgumentDefaultValue::class);

$this->replacedArguments = $configuration;
$this->replaceArgumentDefaultValues = $configuration;
}

private function refactorNew(New_ $new): ?New_
{
foreach ($this->replaceArgumentDefaultValues as $replaceArgumentDefaultValue) {
if ($replaceArgumentDefaultValue->getMethod() !== MethodName::CONSTRUCT) {
continue;
}

if (! $this->isObjectType($new, $replaceArgumentDefaultValue->getObjectType())) {
continue;
}

return $this->argumentDefaultValueReplacer->processReplaces($new, $replaceArgumentDefaultValue);
}

return null;
}
}