mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
change ChoiceDefaultValueRector to generic PropertyAssignToMethodCallRector
This commit is contained in:
parent
84535e43fa
commit
f0ddc564e4
@ -1,2 +0,0 @@
|
||||
services:
|
||||
Rector\Nette\Rector\Forms\ChoiceDefaultValueRector: ~
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Rector\Forms;
|
||||
namespace Rector\Rector\Assign;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
@ -12,7 +12,7 @@ use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class ChoiceDefaultValueRector extends AbstractRector
|
||||
final class PropertyAssignToMethodCallRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var PropertyFetchAnalyzer
|
||||
@ -24,18 +24,42 @@ final class ChoiceDefaultValueRector extends AbstractRector
|
||||
*/
|
||||
private $methodCallNodeFactory;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $types;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $oldPropertyName;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $newMethodName;
|
||||
|
||||
/**
|
||||
* @param string[]
|
||||
*/
|
||||
public function __construct(
|
||||
PropertyFetchAnalyzer $propertyFetchAnalyzer,
|
||||
MethodCallNodeFactory $methodCallNodeFactory
|
||||
MethodCallNodeFactory $methodCallNodeFactory,
|
||||
array $types = ['Nette\Forms\Controls\MultiChoiceControl', 'Nette\Forms\Controls\ChoiceControl'],
|
||||
string $oldPropertyName = 'checkAllowedValues',
|
||||
string $newMethodName = 'checkDefaultValue'
|
||||
) {
|
||||
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
||||
$this->methodCallNodeFactory = $methodCallNodeFactory;
|
||||
$this->types = $types;
|
||||
$this->oldPropertyName = $oldPropertyName;
|
||||
$this->newMethodName = $newMethodName;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Turns checkAllowedValues to method in Nette\Forms Control element', [
|
||||
new CodeSample('$control->checkAllowedValues = false;', '$control->checkDefaultValue(false);'),
|
||||
return new RectorDefinition('Turns property assign of specific type and property name to method call', [
|
||||
new CodeSample('$control->oldProperty = false;', '$control->newMethodCall(false);'),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -47,8 +71,8 @@ final class ChoiceDefaultValueRector extends AbstractRector
|
||||
|
||||
return $this->propertyFetchAnalyzer->isTypesAndProperty(
|
||||
$node->var,
|
||||
['Nette\Forms\Controls\MultiChoiceControl', 'Nette\Forms\Controls\ChoiceControl'],
|
||||
'checkAllowedValues'
|
||||
$this->types,
|
||||
$this->oldPropertyName
|
||||
);
|
||||
}
|
||||
|
||||
@ -65,7 +89,7 @@ final class ChoiceDefaultValueRector extends AbstractRector
|
||||
|
||||
return $this->methodCallNodeFactory->createWithVariableMethodNameAndArguments(
|
||||
$propertyNode,
|
||||
'checkDefaultValue',
|
||||
$this->newMethodName,
|
||||
[$assignNode->expr]
|
||||
);
|
||||
}
|
@ -1,14 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\Forms\ChoiceDefaultValueRector;
|
||||
namespace Rector\Tests\Rector\Assign\PropertyAssignToMethodCallRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @covers \Rector\Nette\Rector\Forms\ChoiceDefaultValueRector
|
||||
* @covers \Rector\Rector\Assign\PropertyAssignToMethodCallRector
|
||||
*/
|
||||
final class ChoiceDefaultValueRectorTest extends AbstractRectorTestCase
|
||||
final class PropertyAssignToMethodCallRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideWrongToFixedFiles()
|
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\Rector\Assign\PropertyAssignToMethodCallRector: ~
|
Loading…
x
Reference in New Issue
Block a user