8000 add configure() method on ServiceConfiguator prototype · rectorphp/rector-src@9641bc1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9641bc1

Browse files
committed
add configure() method on ServiceConfiguator prototype
1 parent 0853c28 commit 9641bc1

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"symplify/skipper": "^10.0",
5050
"symplify/smart-file-system": "^10.0",
5151
"symplify/symfony-php-config": "^10.0",
52+
"symplify/vendor-patches": "^10.0",
5253
"tracy/tracy": "^2.8",
5354
"webmozart/assert": "^1.10"
5455
},
@@ -70,7 +71,6 @@
7071
"symplify/phpstan-extensions": "^10.0",
7172
"symplify/phpstan-rules": "^10.0",
7273
"symplify/rule-doc-generator": "^10.0",
73-
"symplify/vendor-patches": "^10.0",
7474
"timeweb/phpstan-enum": "dev-22-upgrade-phpstan-to-1.0"
7575
},
7676
"replace": {

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,11 @@ parameters:
514514
paths:
515515
- packages/StaticTypeMapper/PhpDocParser/IntersectionTypeMapper.php
516516
- packages/StaticTypeMapper/PhpParser/IntersectionTypeNodeMapper.php
517+
518+
-
519+
message: '#Use separate function calls with readable variable names#'
520+
path: src/DependencyInjection/Loader/Configurator/RectorServiceConfigurator.php
521+
522+
-
523+
message: '#\$service\-\>call\("configure", \[\[ \.\.\. \]\]\) must be followed by exact array#'
524+
path: src/DependencyInjection/Loader/Configurator/RectorServiceConfigurator.php

rector.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Rector\Set\ValueObject\LevelSetList;
1616
use Rector\Set\ValueObject\SetList;
1717
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
18-
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
1918

2019
return static function (ContainerConfigurator $containerConfigurator): void {
2120
// include the latest PHP version + all bellow in one config!
@@ -38,18 +37,18 @@
3837

3938
// phpunit
4039
$services->set(PreferThisOrSelfMethodCallRector::class)
41-
->call('configure', [[
40+
->configure([
4241
PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
43-
TestCase::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_THIS()),
42+
TestCase::class => PreferenceSelfThis::PREFER_THIS(),
4443
],
45-
]]);
44+
]);
4645

4746
$services->set(ReturnArrayClassMethodToYieldRector::class)
48-
->call('configure', [[
49-
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => ValueObjectInliner::inline([
47+
->configure([
48+
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => [
5049
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*'),
51-
]),
52-
]]);
50+
],
51+
]);
5352

5453
$parameters = $containerConfigurator->parameters();
5554

src/DependencyInjection/Loader/Configurator/RectorServiceConfigurator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,51 @@
44

55
namespace Rector\Core\DependencyInjection\Loader\Configurator;
66

7+
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
8+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
79
use Symfony\Component\DependencyInjection\Loader\Configurator\ServiceConfigurator;
10+
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
811

912
/**
13+
* @api
1014
* Same as Symfony service configurator, with extra "configure()" method for easier DX
1115
*/
1216
final class RectorServiceConfigurator extends ServiceConfigurator
1317
{
18+
/**
19+
* @param array<string, mixed> $configuration
20+
*/
21+
public function configure(array $configuration): self
22+
{
23+
$this->ensureClassIsConfigurable($this->id);
1424

25+
// decorate with value object inliner so Symfony understands, see https://getrector.org/blog/2020/09/07/how-to-inline-value-object-in-symfony-php-config
26+
array_walk_recursive($configuration, function (&$value) {
27+
if (is_object($value)) {
28+
$value = ValueObjectInliner::inline($value);
29+
}
30+
31+
return $value;
32+
});
33+
34+
$this->call('configure', [$configuration]);
35+
36+
return $this;
37+
}
38+
39+
private function ensureClassIsConfigurable(?string $class): void
40+
{
41+
if ($class === null) {
42+
throw new InvalidConfigurationException('The class is missing');
43+
}
44+
45+
if (! is_a($class, ConfigurableRectorInterface::class, true)) {
46+
$errorMessage = sprintf(
47+
'The service "%s" is not configurable. Make it implement "%s" or remove "configure()" call.',
48+
$class,
49+
ConfigurableRectorInterface::class,
50+
);
51+
throw new InvalidConfigurationException($errorMessage);
52+
}
53+
}
1554
}

0 commit comments

Comments
 (0)
0