8000 minor #21348 [DependencyInjection] Yaml: check if $tags is an array b… · symfony/symfony@29b6e32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29b6e32

Browse files
minor #21348 [DependencyInjection] Yaml: check if $tags is an array before using it (dunglas)
This PR was squashed before being merged into the 3.3-dev branch (closes #21348). Discussion ---------- [DependencyInjection] Yaml: check if $tags is an array before using it | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 1c9b5c9 [DependencyInjection] Yaml: check if is an array before using it
2 parents 7b6e327 + 1c9b5c9 commit 29b6e32

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ private function parseDefinition($id, $service, $file, array $defaults)
352352
}
353353

354354
$tags = isset($service['tags']) ? $service['tags'] : array();
355+
if (!is_array($tags)) {
356+
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
357+
}
355358

356359
if (!isset($defaults['tags'])) {
357360
// no-op
@@ -363,34 +366,28 @@ private function parseDefinition($id, $service, $file, array $defaults)
363366
$tags = array_merge($tags, $defaults['tags']);
364367
}
365368

366-
if (null !== $tags) {
367-
if (!is_array($tags)) {
368-
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
369+
foreach ($tags as $tag) {
370+
if (!is_array($tag)) {
371+
$tag = array('name' => $tag);
369372
}
370373

371-
foreach ($tags as $tag) {
372-
if (!is_array($tag)) {
373-
$tag = array('name' => $tag);
374-
}
375-
376-
if (!isset($tag['name'])) {
377-
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
378-
}
379-
$name = $tag['name'];
380-
unset($tag['name']);
374+
if (!isset($tag['name'])) {
375+
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
376+
}
377+
$name = $tag['name'];
378+
unset($tag['name']);
381379

382-
if (!is_string($name) || '' === $name) {
383-
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
384-
}
380+
if (!is_string($name) || '' === $name) {
381+
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
382+
}
385383

386-
foreach ($tag as $attribute => $value) {
387-
if (!is_scalar($value) && null !== $value) {
388-
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
389-
}
384+
foreach ($tag as $attribute => $value) {
385+
if (!is_scalar($value) && null !== $value) {
386+
throw new InvalidArgumentException(sprintf('A "tags" attribute 10000 must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
390387
}
391-
392-
$definition->addTag($name, $tag);
393388
}
389+
390+
$definition->addTag($name, $tag);
394391
}
395392

396393
if (isset($service['decorates'])) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
tags: ['foo']
4+
5+
Foo\Bar:
6+
tags: invalid
7+
inherit_tags: true

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_configurator_short_syntax.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
services:
2-
32
foo_bar:
43
class: FooBarClass
54
configurator: foo_bar_configurator:configure

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,14 @@ public function testDecoratedServicesWithWrongSyntaxThrowsException()
414414
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
415415
$loader->load('bad_decorates.yml');
416416
}
417+
418+
/**
419+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
420+
* @expectedExceptionMessage Parameter "tags" must be an array for service "Foo\Bar" in services31_invalid_tags.yml. Check your YAML syntax.
421+
*/
422+
public function testInvalidTagsWithDefaults()
423+
{
424+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
425+
$loader->load('services31_invalid_tags.yml');
426+
}
417427
}

0 commit comments

Comments
 (0)
0