8000 [DependencyInjection] Yaml: check if $tags is an array before using it by dunglas · Pull Request #21348 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Yaml: check if $tags is an array before using it #21348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ private function parseDefinition($id, $service, $file, array $defaults)
}

$tags = isset($service['tags']) ? $service['tags'] : array();
< 8000 svg aria-label="Show options" role="img" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-kebab-horizontal"> Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line was fine, just add if (!is_array($tags)) { on the next line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this PR be applied on a lower branch btw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not but it adds 1 useless check when $service['tags'] is not set.

Copy link
Member Author
@dunglas dunglas Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No the branch is correct (this "bug" was introduced by _defaults in #21071).

Copy link
Member
@nicolas-grekas nicolas-grekas Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it adds 1 useless check

yes, but we don't care, readibility is more important here (thinking about the complexity of nested if/else blocks) IMHO

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer if/else than the ternary operator but it's probably because I need glasses to read on a screen.
I'll change that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (!is_array($tags)) {
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
}

if (!isset($defaults['tags'])) {
// no-op
Expand All @@ -343,34 +346,28 @@ private function parseDefinition($id, $service, $file, array $defaults)
$tags = array_merge($tags, $defaults['tags']);
}

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

foreach ($tags as $tag) {
if (!is_array($tag)) {
$tag = array('name' => $tag);
}

if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}
$name = $tag['name'];
unset($tag['name']);
if (!isset($tag['name'])) {
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}
$name = $tag['name'];
unset($tag['name']);

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

foreach ($tag as $attribute => $value) {
if (!is_scalar($value) && null !== $value) {
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));
}
foreach ($tag as $attribute => $value) {
if (!is_scalar($value) && null !== $value) {
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));
}

$definition->addTag($name, $tag);
}

$definition->addTag($name, $tag);
}

if (isset($service['decorates'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
_defaults:
tags: ['foo']

Foo\Bar:
tags: invalid
inherit_tags: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this test confusing. Intuitively, I thought that tags were inherited and then validated, so the Foo\Bar service has the valid tags: ['foo'] tags instead of tags: invalid

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this way (using a string instead of an array) to specify tags already wasn't supported before. Why should we do that now?

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
services:

foo_bar:
class: FooBarClass
configurator: foo_bar_configurator:configure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,14 @@ public function testDecoratedServicesWithWrongSyntaxThrowsException()
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('bad_decorates.yml');
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Parameter "tags" must be an array for service "Foo\Bar" in services31_invalid_tags.yml. Check your YAML syntax.
*/
public function testInvalidTagsWithDefaults()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services31_invalid_tags.yml');
}
}
0