Skip to content
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

Improve containers types to use iterable objects #299

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Improve type checking
  • Loading branch information
xepozz committed Apr 19, 2022
commit c758bd42615dc13ac4df3de773d3c613ef38d5ef
14 changes: 11 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Traversable;
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\Exception\CircularReferenceException;
use Yiisoft\Definitions\Exception\InvalidConfigException;
Expand Down Expand Up @@ -428,9 +429,9 @@ private function setTags(iterable $tags): void
}
}
}
/** @psalm-var array<string, list<string>> $tags */
/** @psalm-var array<string, list<string>>|Traversable $tags */

$this->tags = $tags;
$this->tags = $tags instanceof Traversable ? iterator_to_array($tags) : $tags ;
}

/**
Expand All @@ -439,7 +440,14 @@ private function setTags(iterable $tags): void
private function setDefinitionTags(string $id, iterable $tags): void
{
foreach ($tags as $tag) {
if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag], true)) {
if (!isset($this->tags[$tag])) {
$this->tags[$tag] = [$id];
continue;
}

$tags = $this->tags[$tag];
$tags = $tags instanceof Traversable ? iterator_to_array($tags) : $tags;
if (!in_array($id, $tags, true)) {
$this->tags[$tag][] = $id;
}
}
Expand Down