add argument adder Laravel rules

This commit is contained in:
Tomas Votruba 2019-03-08 20:56:34 +00:00
parent 06350ced1b
commit e1e19db873
8 changed files with 73 additions and 22 deletions

View File

@ -5,3 +5,16 @@ services:
addRoute: 'public'
Illuminate\Contracts\Auth\Access\Gate:
raw: 'public'
Rector\Rector\Argument\ArgumentAdderRector:
Illuminate\Auth\Middleware\Authenticate:
authenticate:
name: 'request'
Illuminate\Foundation\Auth\ResetsPasswords:
sendResetResponse:
name: 'request'
type: 'Illuminate\Http\Illuminate\Http'
Illuminate\Foundation\Auth\SendsPasswordResetEmails:
sendResetLinkResponse:
name: 'request'
type: 'Illuminate\Http\Illuminate\Http'

View File

@ -26,11 +26,13 @@ services:
Sylius\Component\Mailer\Sender\SenderInterface:
send:
0:
code: '[]'
name: 'code'
default_value: '[]'
Sylius\Component\Core\Repository\ProductRepositoryInterface:
findOneBySlug:
2:
__unknown__: false
name: '__unknown__'
default_value: false
Rector\Rector\ClassMethod\AddReturnTypeDeclarationRector:
Sylius\Component\Order\Model\OrderInterface:

View File

@ -4,4 +4,5 @@ services:
Symfony\Component\DependencyInjection\ContainerBuilder:
addCompilerPass:
2:
priority: 0
name: 'priority'
default_value: 0

View File

@ -5,14 +5,17 @@ services:
Symfony\Component\DependencyInjection\ContainerBuilder:
compile:
2:
__unknown__: 0
name: '__unknown__'
default_value: 0
addCompilerPass:
2:
priority: 0
name: 'priority'
default_value: 0
Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraph:
connect:
6:
weak: false
name: 'weak'
default_value: false
Rector\Symfony\Rector\Console\ConsoleExceptionToErrorEventConstantRector: ~

View File

@ -20,7 +20,7 @@ services:
Symfony\Component\BrowserKit\Client:
submit:
2:
# $serverParameters
name: 'serverParameters'
default_value: []
# https://github.com/symfony/symfony/commit/f634afdb6f573e4af8d89aaa605e0c7d4058676d

View File

@ -8,6 +8,8 @@ use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Rector\AbstractRector;
@ -35,7 +37,9 @@ final class ArgumentAdderRector extends AbstractRector
'SomeExampleClass' => [
'someMethod' => [
0 => [
'name' => 'someArgument',
'default_value' => 'true',
'type' => 'SomeType',
],
],
],
@ -118,20 +122,35 @@ CODE_SAMPLE
*/
private function processPositionWithDefaultValues(Node $node, array $positionWithDefaultValues): void
{
foreach ($positionWithDefaultValues as $position => $nameToValue) {
reset($nameToValue);
$name = key($nameToValue);
if ($name === null || is_int($name)) {
continue;
}
$value = $nameToValue[$name];
foreach ($positionWithDefaultValues as $position => $parameterConfiguration) {
$name = $parameterConfiguration['name'];
$defaultValue = $parameterConfiguration['default_value'] ?? null;
$type = $parameterConfiguration['type'] ?? null;
if ($node instanceof ClassMethod) {
$node->params[$position] = new Param(new Variable($name), BuilderHelpers::normalizeValue($value));
$this->addClassMethodParam($node, $name, $defaultValue, $type, $position);
} else {
$node->args[$position] = new Arg(BuilderHelpers::normalizeValue($value));
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
$node->args[$position] = $arg;
}
}
}
/**
* @param mixed $defaultValue
*/
private function addClassMethodParam(
ClassMethod $classMethod,
string $name,
$defaultValue,
?string $type,
int $position
): void {
$param = new Param(new Variable($name), BuilderHelpers::normalizeValue($defaultValue));
if ($type) {
$param->type = ctype_upper($type[0]) ? new FullyQualified($type) : new Identifier($type);
}
$classMethod->params[$position] = $param;
}
}

View File

@ -24,9 +24,22 @@ final class ArgumentAdderRectorTest extends AbstractRectorTestCase
*/
protected function getRectorConfiguration(): array
{
return [ContainerBuilder::class => [
'compile' => [['isCompiled' => false]],
'addCompilerPass' => [2 => ['priority' => 0]],
]];
return [
ContainerBuilder::class => [
'compile' => [
[
'name' => 'isCompiled',
'default_value' => false,
],
],
'addCompilerPass' => [
2 => [
'name' => 'priority',
'default_value' => 0,
'type' => 'SomeType',
],
],
],
];
}
}

View File

@ -21,7 +21,7 @@ use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\ContainerBuilder;
class CustomContainerBuilder extends ContainerBuilder
{
public function addCompilerPass($pass, $type, $priority = 0)
public function addCompilerPass($pass, $type, \SomeType $priority = 0)
{
}
}