10000 More cleaning · symfony/symfony@3bd794f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3bd794f

Browse files
committed
More cleaning
1 parent a9a1735 commit 3bd794f

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/Symfony/Component/Config/Builder/Generator.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
class Generator implements GeneratorInterface
3131
{
32-
private $classes = [];
32+
private $classes;
3333

3434
/**
3535
* @return \Closure that will return the root config class
@@ -39,8 +39,9 @@ public function build(ConfigurationInterface $configuration, string $outputDir):
3939
$this->classes = [];
4040

4141
$rootNode = $configuration->getConfigTreeBuilder()->buildTree();
42-
$rootClass = new ClassBuilder($namespace = 'Config\\'.ucfirst(preg_replace('#\W#', '', $rootNode->getPath())), $rootNode->getName());
43-
$this->classes[$rootNode->getPath()] = $rootClass;
42+
$namespace = 'Config\\'.ucfirst(preg_replace('#\W#', '', $rootNode->getPath()));
43+
$rootClass = new ClassBuilder($namespace, $rootNode->getName());
44+
$this->classes[] = $rootClass;
4445

4546
$this->buildNode($rootNode, $rootClass, $namespace);
4647

@@ -71,7 +72,7 @@ private function writeClasses(string $outputDir)
7172
private function buildNode(NodeInterface $node, ClassBuilder $class, string $namespace)
7273
{
7374
if (!$node instanceof ArrayNode) {
74-
throw new \LogicException('The node was expected to be an array node.');
75+
throw new \LogicException('The node was expected to be an ArrayNode. This Configuration includes an edge case not supported yet.');
7576
}
7677

7778
foreach ($node->getChildren() as $child) {
@@ -98,15 +99,15 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
9899
{
99100
$childClass = new ClassBuilder($namespace, $node->getName());
100101
$class->addRequire($childClass);
101-
$this->classes[$node->getPath()] = $childClass;
102+
$this->classes[] = $childClass;
102103

103104
$property = $class->addProperty($node->getName(), $childClass->getName());
104105
$body = '
105-
public function NAME(array $value = []): OUT
106+
public function NAME(array $value = []): CLASS
106107
{
107108
return $this->PROPERTY = new CLASS($value);
108109
}';
109-
$class->addMethod('add_'.$node->getName(), $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn(), 'OUT' => $childClass->getFqcn()]);
110+
$class->addMethod('add_'.$node->getName(), $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
110111

111112
$this->buildNode($node, $childClass, $namespace.'\\'.$childClass->getName());
112113
}
@@ -130,20 +131,7 @@ public function NAME($value): self
130131

131132
private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuilder $class, string $namespace)
132133
{
133-
/*
134-
* Pick a good singular name
135-
*/
136-
$name = $node->getName();
137-
if ('s' === substr($name, -1)) {
138-
$mappings = $node->getParent() instanceof ArrayNode ? $node->getParent()->getXmlRemappings() : [];
139-
foreach ($mappings as $map) {
140-
if ($map[1] === $name) {
141-
$name = $map[0];
142-
break;
143-
}
144-
}
145-
}
146-
134+
$name = $this->getSingularName($node);
147135
$prototype = $node->getPrototype();
148136
$methodName = 'add_'.$name;
149137

@@ -175,7 +163,7 @@ public function NAME(string $key, TYPE $value): self
175163

176164
$childClass = new ClassBuilder($namespace, $name);
177165
$class->addRequire($childClass);
178-
$this->classes[$node->getPath()] = $childClass;
166+
$this->classes[] = $childClass;
179167
$property = $class->addProperty($node->getName(), $childClass->getName().'[]');
180168

181169
if (null === $node->getKeyAttribute()) {
@@ -269,4 +257,26 @@ private function getComment(VariableNode $node): string
269257

270258
return $comment;
271259
}
260+
261+
/**
262+
* Pick a good singular name
263+
*/
264+
private function getSingularName(PrototypedArrayNode $node): string
265+
{
266+
$name = $node->getName();
267+
if ('s' !== substr($name, -1)) {
268+
return $name;
269+
}
270+
271+
$parent = $node->getParent();
272+
$mappings = $parent instanceof ArrayNode ? $parent->getXmlRemappings() : [];
273+
foreach ($mappings as $map) {
274+
if ($map[1] === $name) {
275+
$name = $map[0];
276+
break;
277+
}
278+
}
279+
280+
return $name;
281+
}
272282
}

0 commit comments

Comments
 (0)
0