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

Parameter name binding #87

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 24 additions & 3 deletions src/DefinitionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,33 @@
*
* @throws CircularReferenceException
*/
private function isResolvable(string $id, array $building): bool
private function isResolvable(string $id, array $building, ?string $parameterName = null): bool
{
if (isset($this->definitions[$id])) {
return true;
}

if (
Copy link
Member

@vjik vjik Mar 3, 2024

Choose a reason for hiding this comment

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

I assume that feature can degrade performance. Suggest to do it optional or need check performance.

$parameterName !== null
&& isset($this->definitions[$id . '$' . $parameterName])
xepozz marked this conversation as resolved.
Show resolved Hide resolved
) {
$buildingClass = array_key_last($building);
$definition = $this->definitions[$buildingClass] ?? null;
$temporaryDefinition = ArrayDefinition::fromConfig([

Check failure on line 111 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

ArgumentTypeCoercion

src/DefinitionStorage.php:111:64: ArgumentTypeCoercion: Argument 1 of Yiisoft\Definitions\ArrayDefinition::fromConfig expects array{'__construct()'?: array<array-key, mixed>, class: class-string}<string, mixed>, but parent type array{'__construct()': non-empty-array<string, Yiisoft\Definitions\Reference>, class: null|string} provided (see https://psalm.dev/193)

Check failure on line 111 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/DefinitionStorage.php:111:64: ArgumentTypeCoercion: Argument 1 of Yiisoft\Definitions\ArrayDefinition::fromConfig expects array{'__construct()'?: array<array-key, mixed>, class: class-string, ...<string, mixed>}, but parent type array{'__construct()': non-empty-array<string, Yiisoft\Definitions\Reference>, class: null|string} provided (see https://psalm.dev/193)

Check failure on line 111 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

ArgumentTypeCoercion

src/DefinitionStorage.php:111:64: ArgumentTypeCoercion: Argument 1 of Yiisoft\Definitions\ArrayDefinition::fromConfig expects array{'__construct()'?: array<array-key, mixed>, class: class-string}<string, mixed>, but parent type array{'__construct()': non-empty-array<string, Yiisoft\Definitions\Reference>, class: null|string} provided (see https://psalm.dev/193)

Check failure on line 111 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/DefinitionStorage.php:111:64: ArgumentTypeCoercion: Argument 1 of Yiisoft\Definitions\ArrayDefinition::fromConfig expects array{'__construct()'?: array<array-key, mixed>, class: class-string, ...<string, mixed>}, but parent type array{'__construct()': non-empty-array<string, Yiisoft\Definitions\Reference>, class: null|string} provided (see https://psalm.dev/193)
ArrayDefinition::CLASS_NAME => $buildingClass,
ArrayDefinition::CONSTRUCTOR => [
$parameterName => Reference::to($this->definitions[$id . '$' . $parameterName]),
],
]);
if ($definition instanceof ArrayDefinition) {
$this->definitions[$buildingClass] = $definition->merge($temporaryDefinition);

Check failure on line 118 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:118:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)

Check failure on line 118 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:118:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)

Check failure on line 118 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:118:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)

Check failure on line 118 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:118:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)
} else {
$this->definitions[$buildingClass] = $temporaryDefinition;

Check failure on line 120 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:120:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)

Check failure on line 120 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:120:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)

Check failure on line 120 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:120:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)

Check failure on line 120 in src/DefinitionStorage.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyNullArrayOffset

src/DefinitionStorage.php:120:17: PossiblyNullArrayOffset: Cannot access value on variable $this->definitions[$buildingClass] using possibly null offset null|string (see https://psalm.dev/125)
}

return true;
}

if ($this->useStrictMode || !class_exists($id)) {
$this->buildStack += $building + [$id => 1];
return false;
Expand Down Expand Up @@ -210,7 +231,7 @@
}

if (
!$this->isResolvable($typeName, $building)
!$this->isResolvable($typeName, $building, $parameter->getName())
&& ($this->delegateContainer === null || !$this->delegateContainer->has($typeName))
) {
$isResolvable = false;
Expand All @@ -222,7 +243,7 @@
$this->buildStack += $building;
}

if ($isResolvable) {
if ($isResolvable && !isset($this->definitions[$id])) {
$this->definitions[$id] = $id;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Helpers/Normalizer.php
Copy link
Member

Choose a reason for hiding this comment

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

Seems, this changes not need in this PR.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
return $definition;
}

if ($definition instanceof DefinitionInterface) {
return $definition;
}

if (is_string($definition)) {
// Current class
if (
Expand Down Expand Up @@ -88,7 +92,7 @@
}

// Ready object
if (is_object($definition) && !($definition instanceof DefinitionInterface)) {

Check failure on line 95 in src/Helpers/Normalizer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

RedundantCondition

src/Helpers/Normalizer.php:95:13: RedundantCondition: $definition is not Yiisoft\Definitions\Contract\DefinitionInterface has already been asserted (see https://psalm.dev/122)

Check failure on line 95 in src/Helpers/Normalizer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RedundantCondition

src/Helpers/Normalizer.php:95:13: RedundantCondition: $definition is not Yiisoft\Definitions\Contract\DefinitionInterface has already been asserted (see https://psalm.dev/122)

Check failure on line 95 in src/Helpers/Normalizer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

RedundantCondition

src/Helpers/Normalizer.php:95:13: RedundantCondition: $definition is not Yiisoft\Definitions\Contract\DefinitionInterface has already been asserted (see https://psalm.dev/122)

Check failure on line 95 in src/Helpers/Normalizer.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RedundantCondition

src/Helpers/Normalizer.php:95:13: RedundantCondition: $definition is not Yiisoft\Definitions\Contract\DefinitionInterface has already been asserted (see https://psalm.dev/122)
return new ValueDefinition($definition);
}

Expand Down
Loading