Skip to content

Commit 3126861

Browse files
committed
skip existing defaults public call
1 parent 7ef1d4b commit 3126861

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\ServiceTagsToDefaultsAutoconfigureRector\Fixture;
4+
5+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6+
7+
return static function (ContainerConfigurator $containerConfigurator): void {
8+
$services = $containerConfigurator->services();
9+
10+
$services->defaults()->public();
11+
12+
$services->set(NextCommand::class)
13+
->public();
14+
};
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Symfony\Tests\Configs\Rector\Closure\ServiceTagsToDefaultsAutoconfigureRector\Fixture;
21+
22+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
23+
24+
return static function (ContainerConfigurator $containerConfigurator): void {
25+
$services = $containerConfigurator->services();
26+
27+
$services->defaults()->public();
28+
29+
$services->set(NextCommand::class);
30+
};
31+
32+
?>

rules/Configs/Rector/Closure/FromServicePublicToDefaultsPublicRector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public function refactor(Node $node): ?Node
9898
return null;
9999
}
100100

101+
// not the ->defaults()->public()
102+
if ($this->isDefaultCall($node)) {
103+
return null;
104+
}
105+
101106
$hasChanged = true;
102107

103108
return $node->var;
@@ -113,4 +118,17 @@ public function refactor(Node $node): ?Node
113118

114119
return $node;
115120
}
121+
122+
public function isDefaultCall(MethodCall $methodCall): bool
123+
{
124+
$currentMethodCall = $methodCall;
125+
while ($currentMethodCall instanceof MethodCall) {
126+
if ($this->isName($currentMethodCall->name, 'defaults')) {
127+
return true;
128+
}
129+
$currentMethodCall = $currentMethodCall->var;
130+
}
131+
132+
return false;
133+
}
116134
}

0 commit comments

Comments
 (0)