Update RenameMethodCallBasedOnParameterRector based on feedback

This commit is contained in:
Mark Story 2019-09-26 20:47:35 -04:00
parent 31c1bfbe70
commit 018edbe75b
4 changed files with 53 additions and 49 deletions

View File

@ -8,7 +8,7 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
@ -36,7 +36,7 @@ final class RenameMethodCallBasedOnParameterRector extends AbstractRector
return new RectorDefinition(
'Changes method calls based on matching the first parameter value.',
[
new CodeSample(
new ConfiguredCodeSample(
<<<'PHP'
$object = new ServerRequest();
@ -48,8 +48,19 @@ PHP
$object = new ServerRequest();
$config = $object->getAttribute('paging');
$object = $object->withParam('paging', ['a value']);
$object = $object->withAttribute('paging', ['a value']);
PHP
,
[
'getParam' => [
'match_parameter' => 'paging',
'replace_with' => 'getAttribute'
],
'withParam' => [
'match_parameter' => 'paging',
'replace_with' => 'withAttribute'
]
]
),
]
);
@ -73,7 +84,7 @@ PHP
return null;
}
$node->name = new Identifier($config['replaceWith']);
$node->name = new Identifier($config['replace_with']);
return $node;
}
@ -99,7 +110,7 @@ PHP
continue;
}
$config = $methodMapping[$currentMethodName];
if (empty($config['matchParameter'])) {
if (empty($config['match_parameter'])) {
continue;
}
if (count($methodCall->args) < 1) {
@ -109,7 +120,7 @@ PHP
if (! ($arg->value instanceof String_)) {
continue;
}
if ($arg->value->value !== $config['matchParameter']) {
if ($arg->value->value !== $config['match_parameter']) {
continue;
}
@ -118,32 +129,4 @@ PHP
return null;
}
/**
* @param mixed[] $config
*/
private function resolveNewMethodNameByCondition(MethodCall $methodCall, array $config): string
{
if (count($methodCall->args) >= $config['minimal_argument_count']) {
return $config['set'];
}
if (! isset($methodCall->args[0])) {
return $config['get'];
}
// first argument type that is considered setter
if (! isset($config['first_argument_type_to_set'])) {
return $config['get'];
}
$argumentType = $config['first_argument_type_to_set'];
$argumentValue = $methodCall->args[0]->value;
if ($argumentType === 'array' && $argumentValue instanceof Array_) {
return $config['set'];
}
return $config['get'];
}
}

View File

@ -8,11 +8,6 @@ function renameMethodCallBasedOnParameter()
$config = $object->getParam('paging');
$object->withParam('paging', 'value');
$config = $object->getParam($value);
$config = $object->getParam('other');
$object->withParam('other', 'value');
}
?>
@ -27,11 +22,6 @@ function renameMethodCallBasedOnParameter()
$config = $object->getAttribute('paging');
$object->withAttribute('paging', 'value');
$config = $object->getParam($value);
$config = $object->getParam('other');
$object->withParam('other', 'value');
}
?>

View File

@ -0,0 +1,29 @@
<?php
namespace Rector\CakePHP\Tests\Rector\MethodCall\RenameMethodCallBasedOnParameterRector;
function renameMethodCallBasedOnParameterNoop()
{
$object = new Source\SomeModelType;
$config = $object->getParam($value);
$config = $object->getParam('other');
$object->withParam('other', 'value');
}
?>
-----
<?php
namespace Rector\CakePHP\Tests\Rector\MethodCall\RenameMethodCallBasedOnParameterRector;
function renameMethodCallBasedOnParameterNoop()
{
$object = new Source\SomeModelType;
$config = $object->getParam($value);
$config = $object->getParam('other');
$object->withParam('other', 'value');
}
?>

View File

@ -22,6 +22,7 @@ final class RenameMethodCallBasedOnParameterRectorTest extends AbstractRectorTes
public function provideDataForTest(): iterable
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
yield [__DIR__ . '/Fixture/fixture2.php.inc'];
}
/**
@ -33,14 +34,15 @@ final class RenameMethodCallBasedOnParameterRectorTest extends AbstractRectorTes
RenameMethodCallBasedOnParameterRector::class => [
'$methodNamesByTypes' => [
SomeModelType::class => [
'invalidNoOptions' => [],
'getParam' => [
'matchParameter' => 'paging',
'replaceWith' => 'getAttribute',
'match_parameter' => 'paging',
'replace_with' => 'getAttribute',
],
'withParam' => [
'matchParameter' => 'paging',
'replaceWith' => 'withAttribute',
]
'match_parameter' => 'paging',
'replace_with' => 'withAttribute',
],
],
],
],