Skip to content

Commit 2c7d442

Browse files
committed
[DependencyInjection] YAML: reduce the complexity of the _defaults parser
1 parent af15a88 commit 2c7d442

File tree

1 file changed

+60
-44
lines changed

1 file changed

+60
-44
lines changed

Loader/YamlFileLoader.php

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -157,57 +157,73 @@ private function parseDefinitions($content, $file)
157157
if (!is_array($content['services'])) {
158158
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
159159
}
160-
if (isset($content['services']['_defaults'])) {
161-
if (!is_array($defaults = $content['services']['_defaults'])) {
162-
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
160+
161+
$defaults = $this->parseDefaults($content, $file);
162+
foreach ($content['services'] as $id => $service) {
163+
$this->parseDefinition($id, $service, $file, $defaults);
164+
}
165+
}
166+
167+
/**
168+
* @param array $content
169+
* @param string $file
170+
*
171+
* @return array
172+
*
173+
* @throws InvalidArgumentException
174+
*/
175+
private function parseDefaults(array &$content, $file)
176+
{
177+
if (!isset($content['services']['_defaults'])) {
178+
return $content;
179+
}
180+
if (!is_array($defaults = $content['services']['_defaults'])) {
181+
throw new InvalidArgumentException(sprintf('Service defaults must be an array, "%s" given in "%s".', gettype($defaults), $file));
182+
}
183+
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
184+
@trigger_error('Giving a service the "_defaults" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', E_USER_DEPRECATED);
185+
186+
return $content;
187+
}
188+
189+
$defaultKeys = array('public', 'tags', 'inherit_tags', 'autowire');
190+
unset($content['services']['_defaults']);
191+
192+
foreach ($defaults as $key => $default) {
193+
if (!in_array($key, $defaultKeys)) {
194+
throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', $defaultKeys)));
163195
}
164-
if (isset($defaults['alias']) || isset($defaults['class']) || isset($defaults['factory'])) {
165-
@trigger_error('Giving a service the "_defaults" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', E_USER_DEPRECATED);
166-
$defaults = array();
167-
} else {
168-
$defaultKeys = array('public', 'tags', 'inherit_tags', 'autowire');
169-
unset($content['services']['_defaults']);
196+
}
197+
if (!isset($defaults['tags'])) {
198+
return $defaults;
199+
}
200+
if (!is_array($tags = $defaults['tags'])) {
201+
throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
202+
}
170203

171-
foreach ($defaults as $key => $default) {
172-
if (!in_array($key, $defaultKeys)) {
173-
throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', $defaultKeys)));
174-
}
175-
}
176-
if (isset($defaults['tags'])) {
177-
if (!is_array($tags = $defaults['tags'])) {
178-
throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
179-
}
204+
foreach ($tags as $tag) {
205+
if (!is_array($tag)) {
206+
$tag = array('name' => $tag);
207+
}
180208

181-
foreach ($tags as $tag) {
182-
if (!is_array($tag)) {
183-
$tag = array('name' => $tag);
184-
}
185-
186-
if (!isset($tag['name'])) {
187-
throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in %s.', $file));
188-
}
189-
$name = $tag['name'];
190-
unset($tag['name']);
191-
192-
if (!is_string($name) || '' === $name) {
193-
throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in %s.', $file));
194-
}
195-
196-
foreach ($tag as $attribute => $value) {
197-
if (!is_scalar($value) && null !== $value) {
198-
throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s. Check your YAML syntax.', $name, $attribute, $file));
199-
}
200-
}
201-
}
209+
if (!isset($tag['name'])) {
210+
throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in %s.', $file));
211+
}
212+
$name = $tag['name'];
213+
unset($tag['name']);
214+
215+
if (!is_string($name) || '' === $name) {
216+
throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in %s.', $file));
217+
}
218+
219+
foreach ($tag as $attribute => $value) {
220+
if (!is_scalar($value) && null !== $value) {
221+
throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in %s. Check your YAML syntax.', $name, $attribute, $file));
202222
}
203223
}
204-
} else {
205-
$defaults = array();
206224
}
207225

208-
foreach ($content['services'] as $id => $service) {
209-
$this->parseDefinition($id, $service, $file, $defaults);
210-
}
226+
return $defaults;
211227
}
212228

213229
/**

0 commit comments

Comments
 (0)