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

Performance optimization #387

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- New #380: Add `TagReference::id()` method (@vjik)
- Enh #384: Make `$config` parameter in `Container` constructor optional (@np25071984)
- Enh #324: Make `BuildingException` and `NotFoundException` friendly (@np25071984)
- Enh #387: Improve container performance (@samdark)

## 1.3.0 October 14, 2024

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"rector/rector": "^2.0",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.26",
"vimeo/psalm": "^5.26.1",
"yiisoft/injector": "^1.0",
"yiisoft/test-support": "^3.0"
},
Expand Down
5 changes: 0 additions & 5 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<InvalidCatch>
<errorLevel type="suppress">
<referencedClass name="Psr\Container\NotFoundExceptionInterface" />
</errorLevel>
</InvalidCatch>
</issueHandlers>
</psalm>
27 changes: 27 additions & 0 deletions src/CompositeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$tags = [];
foreach ($this->containers as $container) {
if (!$container instanceof Container) {
continue;

Check warning on line 61 in src/CompositeContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ $tags = []; foreach ($this->containers as $container) { if (!$container instanceof Container) { - continue; + break; } if ($container->has($id)) { /** @psalm-suppress MixedArgument `Container::get()` always return array for tag */
}
if ($container->has($id)) {
/** @psalm-suppress MixedArgument `Container::get()` always return array for tag */
Expand All @@ -81,7 +81,7 @@
$exceptions = [];
foreach ($this->containers as $container) {
$hasException = false;
try {

Check warning on line 84 in src/CompositeContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapFinally": --- Original +++ New @@ @@ } catch (Throwable $t) { $hasException = true; $exceptions[] = [$t, $container]; - } finally { - if (!$hasException) { - $exceptions[] = [new RuntimeException('Container "has()" returned false, but no exception was thrown from "get()".'), $container]; - } + } + if (!$hasException) { + $exceptions[] = [new RuntimeException('Container "has()" returned false, but no exception was thrown from "get()".'), $container]; } } throw new CompositeNotFoundException($exceptions);
$container->get($id);
} catch (Throwable $t) {
$hasException = true;
Expand All @@ -103,11 +103,38 @@

public function has($id): bool
{
/** @psalm-suppress TypeDoesNotContainType */
if (!is_string($id)) {
throw new InvalidArgumentException(
sprintf(
'ID must be a string, %s given.',
get_debug_type($id)
)
);

Check warning on line 113 in src/CompositeContainer.php

View check run for this annotation

Codecov / codecov/patch

src/CompositeContainer.php#L108-L113

Added lines #L108 - L113 were not covered by tests
}

if ($id === StateResetter::class) {
return true;
}

if (TagReference::isTagAlias($id)) {
foreach ($this->containers as $container) {
if (!$container instanceof Container) {
continue;

Check warning on line 123 in src/CompositeContainer.php

View check run for this annotation

Codecov / codecov/patch

src/CompositeContainer.php#L121-L123

Added lines #L121 - L123 were not covered by tests
}
if ($container->has($id)) {
return true;

Check warning on line 126 in src/CompositeContainer.php

View check run for this annotation

Codecov / codecov/patch

src/CompositeContainer.php#L125-L126

Added lines #L125 - L126 were not covered by tests
}
}
return false;

Check warning on line 129 in src/CompositeContainer.php

View check run for this annotation

Codecov / codecov/patch

src/CompositeContainer.php#L129

Added line #L129 was not covered by tests
}

foreach ($this->containers as $container) {
if ($container->has($id)) {
return true;
}
}

return false;
}

Expand Down
Loading
Loading