|
25 | 25 | use Rector\Symfony\ValueObject\ExtensionKeyAndConfiguration;
|
26 | 26 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
27 | 27 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
| 28 | +use RectorPrefix202401\Webmozart\Assert\Assert; |
28 | 29 | /**
|
29 | 30 | * @changelog https://symfony.com/blog/new-in-symfony-5-3-config-builder-classes
|
30 | 31 | *
|
@@ -70,7 +71,7 @@ final class StringExtensionToConfigBuilderRector extends AbstractRector
|
70 | 71 | /**
|
71 | 72 | * @var array<string, string>
|
72 | 73 | */
|
73 |
| - private const EXTENSION_KEY_TO_CLASS_MAP = ['security' => 'Symfony\\Config\\SecurityConfig', 'framework' => 'Symfony\\Config\\FrameworkConfig', 'monolog' => 'Symfony\\Config\\MonologConfig', 'twig' => 'Symfony\\Config\\TwigConfig', 'doctrine' => 'Symfony\\Config\\DoctrineConfig', 'doctrine_migrations' => 'Symfony\\Config\\DoctrineMigrationsConfig', 'sentry' => 'Symfony\\Config\\DoctrineMigrationsConfig']; |
| 74 | + private const EXTENSION_KEY_TO_CLASS_MAP = ['security' => 'Symfony\\Config\\SecurityConfig', 'framework' => 'Symfony\\Config\\FrameworkConfig', 'monolog' => 'Symfony\\Config\\MonologConfig', 'twig' => 'Symfony\\Config\\TwigConfig', 'doctrine' => 'Symfony\\Config\\DoctrineConfig', 'doctrine_migrations' => 'Symfony\\Config\\DoctrineMigrationsConfig', 'sentry' => 'Symfony\\Config\\SentryConfig']; |
74 | 75 | public function __construct(SymfonyPhpClosureDetector $symfonyPhpClosureDetector, SymfonyClosureExtensionMatcher $symfonyClosureExtensionMatcher, PropertyNaming $propertyNaming, ValueResolver $valueResolver, NestedConfigCallsFactory $nestedConfigCallsFactory, SecurityAccessDecisionManagerConfigArrayHandler $securityAccessDecisionManagerConfigArrayHandler, SymfonyClosureFactory $symfonyClosureFactory)
|
75 | 76 | {
|
76 | 77 | $this->symfonyPhpClosureDetector = $symfonyPhpClosureDetector;
|
@@ -123,13 +124,16 @@ public function refactor(Node $node) : ?Node
|
123 | 124 | if (!$this->symfonyPhpClosureDetector->detect($node)) {
|
124 | 125 | return null;
|
125 | 126 | }
|
| 127 | + // make sure to avoid duplicates |
| 128 | + Assert::uniqueValues(self::EXTENSION_KEY_TO_CLASS_MAP); |
| 129 | + Assert::uniqueValues(\array_keys(self::EXTENSION_KEY_TO_CLASS_MAP)); |
126 | 130 | $extensionKeyAndConfiguration = $this->symfonyClosureExtensionMatcher->match($node);
|
127 | 131 | if (!$extensionKeyAndConfiguration instanceof ExtensionKeyAndConfiguration) {
|
128 | 132 | return null;
|
129 | 133 | }
|
130 | 134 | $configClass = self::EXTENSION_KEY_TO_CLASS_MAP[$extensionKeyAndConfiguration->getKey()] ?? null;
|
131 | 135 | if ($configClass === null) {
|
132 |
| - throw new NotImplementedYetException($extensionKeyAndConfiguration->getKey()); |
| 136 | + throw new NotImplementedYetException(\sprintf('The extensions "%s" is not supported yet. Check the rule and add keyword.', $extensionKeyAndConfiguration->getKey())); |
133 | 137 | }
|
134 | 138 | $configVariable = $this->createConfigVariable($configClass);
|
135 | 139 | $stmts = $this->createMethodCallStmts($extensionKeyAndConfiguration->getArray(), $configVariable);
|
@@ -197,9 +201,14 @@ private function createMethodCallStmts(Array_ $configurationArray, Variable $con
|
197 | 201 | continue;
|
198 | 202 | }
|
199 | 203 | $simpleMethodName = StringUtils::underscoreToCamelCase($key);
|
200 |
| - $args = $this->nodeFactory->createArgs([$value]); |
201 |
| - $methodCall = new MethodCall($configVariable, $simpleMethodName, $args); |
202 |
| - $methodCallStmts[] = new Expression($methodCall); |
| 204 | + if (\is_array($value)) { |
| 205 | + $simpleMethodCallStmts = $this->nestedConfigCallsFactory->create([$value], $configVariable, $simpleMethodName); |
| 206 | + $methodCallStmts = \array_merge($methodCallStmts, $simpleMethodCallStmts); |
| 207 | + } else { |
| 208 | + $args = $this->nodeFactory->createArgs([$value]); |
| 209 | + $methodCall = new MethodCall($configVariable, $simpleMethodName, $args); |
| 210 | + $methodCallStmts[] = new Expression($methodCall); |
| 211 | + } |
203 | 212 | }
|
204 | 213 | }
|
205 | 214 | return $methodCallStmts;
|
|
0 commit comments