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
Changes from 1 commit
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
Next Next commit
[DependencyInjection] Yaml: check if $tags is an array before using it
  • Loading branch information
dunglas committed Jan 19, 2017
commit a017641e06eccef0581d9942d2f6f46ee048c5ca
48 changes: 25 additions & 23 deletions src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,15 @@ private function parseDefinition($id, $service, $file, array $defaults)
}
}

$tags = isset($service['tags']) ? $service['tags'] : array();
if (isset($service['tags'])) {
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

$tags = $service['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));
}
} else {
$tags = array();
}

if (!isset($defaults['tags'])) {
// no-op
Expand All @@ -343,34 +351,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) {
A5B3 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
0