-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Conversation
xepozz
commented
Feb 13, 2024
Q | A |
---|---|
Is bugfix? | ❌ |
New feature? | ✔️ |
Breaks BC? | ❌ |
Fixed issues |
PR Summary
|
Also it could be done as the patch does: Index: src/ArrayDefinition.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/ArrayDefinition.php b/src/ArrayDefinition.php
--- a/src/ArrayDefinition.php (revision 7ea68b63f82f218656c9aca735d41b4b41f8a8da)
+++ b/src/ArrayDefinition.php (date 1707842233005)
@@ -7,12 +7,14 @@
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use ReflectionMethod;
+use ReflectionNamedType;
use Yiisoft\Definitions\Contract\DefinitionInterface;
use Yiisoft\Definitions\Contract\ReferenceInterface;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Helpers\ArrayDefinitionHelper;
use Yiisoft\Definitions\Helpers\DefinitionExtractor;
use Yiisoft\Definitions\Helpers\DefinitionResolver;
+use ReflectionUnionType;
use function array_key_exists;
use function call_user_func_array;
@@ -24,7 +26,7 @@
*
* @psalm-type MethodOrPropertyItem = array{0:string,1:string,2:mixed}
* @psalm-type ArrayDefinitionConfig = array{class:class-string,'__construct()'?:array}&array<string, mixed>
- */
+*/
final class ArrayDefinition implements DefinitionInterface
{
public const CLASS_NAME = 'class';
@@ -196,6 +198,27 @@
/** @infection-ignore-all Mutation don't change behaviour. Values of `$usedArguments` not used. */
$usedArguments[$index] = 1;
}
+ if ($value instanceof ParameterDefinition) {
+ $type = $value->getReflection()->getType();
+ if ($type !== null) {
+ if ($type instanceof ReflectionUnionType) {
+ foreach ($type->getTypes() as $type) {
+ $boundDefinition = $type->getName() . '$' . $key;
+ if (isset($arguments[$boundDefinition])) {
+ $value = DefinitionResolver::ensureResolvable($arguments[$boundDefinition]);
+ $usedArguments[$boundDefinition] = 1;
+ break;
+ }
+ }
+ } elseif ($type instanceof ReflectionNamedType) {
+ $boundDefinition = $type->getName() . '$' . $key;
+ if (isset($arguments[$boundDefinition])) {
+ $value = DefinitionResolver::ensureResolvable($arguments[$boundDefinition]);
+ $usedArguments[$boundDefinition] = 1;
+ }
+ }
+ }
+ }
$dependencyIndex++;
}
unset($value); But still needed adjustments in the definition storage |
Is it possible to use scalar types? |
Nope, but it's good point
What is it? |
space between class name and variable |
…ameter-name-binding # Conflicts: # src/DefinitionStorage.php
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #87 +/- ##
=============================================
- Coverage 100.00% 99.85% -0.15%
- Complexity 242 250 +8
=============================================
Files 16 16
Lines 653 672 +19
=============================================
+ Hits 653 671 +18
- Misses 0 1 +1 ☔ View full report in Codecov by Sentry. |
Added support to resolve variable-like binding.
|
Is |
Look at this commit. Your case is still union type. It supports discovering MyClass1 $param or MyClass2 $param. Binding \My\Class1&\My\Class2 $cache => $cacheDefinition is not supported. Although binding any string-like keys supported, it's discovering isn't |
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
src/Helpers/Normalizer.php
Outdated
There was a problem hiding this comment.
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.
{ | ||
if (isset($this->definitions[$id])) { | ||
return true; | ||
} | ||
|
||
if ( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xepozz would you mind running a comparison benchmark for it?