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
11 changes: 10 additions & 1 deletion config/set/symfony/symfony42.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,57 @@ services:
2:
name: 'serverParameters'
default_value: []
scope: ['method_call']

# https://github.com/symfony/symfony/commit/f634afdb6f573e4af8d89aaa605e0c7d4058676d
Symfony\Component\DomCrawler\Crawler:
children:
0:
# $selector
default_value: null
scope: ['method_call']

Symfony\Component\Finder\Finder:
sortByName:
0:
# $useNaturalSort
default_vlaue: false
default_value: false
scope: ['method_call']

Symfony\Bridge\Monolog\Processor\DebugProcessor:
getLogs:
0:
# $request
default_value: null
scope: ['method_call']
countErrors:
0:
# $request
default_value: null
scope: ['method_call']

Symfony\Bridge\Monolog\Logger:
getLogs:
0:
# $request
default_value: null
scope: ['method_call']
countErrors:
0:
# $request
default_value: null
scope: ['method_call']

Symfony\Component\Serializer\Normalizer:
handleCircularReference:
1:
# $format
default_value: null
scope: ['method_call']
2:
# $context
default_value: []
scope: ['method_call']

Rector\Rector\MethodCall\RenameMethodRector:
Symfony\Component\Cache\CacheItem:
Expand Down
39 changes: 39 additions & 0 deletions src/Rector/Argument/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,15 @@ private function processPositionWithDefaultValues(Node $node, array $positionWit
continue;
}

// is correct scope?
if (! $this->isInCorrectScope($node, $parameterConfiguration)) {
continue;
}

if ($node instanceof ClassMethod) {
$this->addClassMethodParam($node, $name, $defaultValue, $type, $position);
} elseif ($node instanceof StaticCall && $this->isName($node->class, 'parent')) {
$node->args[$position] = new Arg(new Variable($name));
} else {
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
$node->args[$position] = $arg;
Expand Down Expand Up @@ -171,4 +178,36 @@ private function shouldSkipParameter(Node $node, int $position, string $name): b
// already added?
return isset($node->args[$position]) && $this->isName($node->args[$position], $name);
}

/**
* @param ClassMethod|MethodCall|StaticCall $node
* @param mixed[] $parameterConfiguration
*/
private function isInCorrectScope(Node $node, array $parameterConfiguration): bool
{
if (! isset($parameterConfiguration['scope'])) {
return true;
}

/** @var string[] $scope */
$scope = $parameterConfiguration['scope'];

if ($node instanceof ClassMethod) {
return in_array('class_method', $scope, true);
}

if ($node instanceof StaticCall) {
if ($this->isName($node->class, 'parent')) {
return in_array('parent_call', $scope, true);
}

return in_array('method_call', $scope, true);
}

if ($node instanceof MethodCall) {
return in_array('method_call', $scope, true);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Rector\Rector\Argument\ArgumentAdderRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeContainerBuilder;
use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeParentClient;

final class ArgumentAdderRectorTest extends AbstractRectorTestCase
{
Expand All @@ -14,6 +15,7 @@ public function test(): void
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/fixture2.php.inc',
__DIR__ . '/Fixture/fixture3.php.inc',
__DIR__ . '/Fixture/scoped.php.inc',
__DIR__ . '/Fixture/already_added.php.inc',
]);
}
Expand Down Expand Up @@ -41,6 +43,19 @@ protected function getRectorsWithConfiguration(): array
],
],
],

// scoped
SomeParentClient::class => [
'submit' => [
2 => [
'name' => 'serverParameters',
'default_value' => [],
'type' => 'array',
// scope!
'scope' => ['parent_call', 'class_method'],
],
],
],
],
],
];
Expand Down
43 changes: 43 additions & 0 deletions tests/Rector/Argument/ArgumentAdderRector/Fixture/scoped.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\Rector\Argument\ArgumentAdderRector\Fixture;

use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeParentClient;

class Scoped extends SomeParentClient
{
public function submit(\DomCrawlerForm $form, array $values = [])
{
return parent::submit($form, $values);
}

public function selfie($form)
{
$self = new self();
$self->submit($form);
}
}

?>
-----
<?php

namespace Rector\Tests\Rector\Argument\ArgumentAdderRector\Fixture;

use Rector\Tests\Rector\Argument\ArgumentAdderRector\Source\SomeParentClient;

class Scoped extends SomeParentClient
{
public function submit(\DomCrawlerForm $form, array $values = [], array $serverParameters = [])
{
return parent::submit($form, $values, $serverParameters);
}

public function selfie($form)
{
$self = new self();
$self->submit($form);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Rector\Tests\Rector\Argument\ArgumentAdderRector\Source;

class SomeParentClient
{
public function submit(\DomCrawlerForm $form, array $values = [])
{
}
}