auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

Patch to allow NULL values as parameter values #99

Closed cziegenberg closed 9 years ago

cziegenberg commented 9 years ago

When setting NULL values as parameters

$di->params[$class][$name] = null;

and creating an instance of the class, I got an Aura\Di\Exception\MissingParam exception.

The following patch fixes this problem (using of array_key_exists instead of isset):

Index: vendor/aura/di/src/Resolver/Resolver.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>windows-1252
===================================================================
--- vendor/aura/di/src/Resolver/Resolver.php    (revision )
+++ vendor/aura/di/src/Resolver/Resolver.php    (revision )
@@ -327,14 +327,16 @@
         $pos = $rparam->getPosition();

         // is there a positional value explicitly from the current class?
-        $explicitPos = isset($this->params[$class][$pos])
+        $explicitPos = isset($this->params[$class])
+                 && array_key_exists($pos, $this->params[$class])
                  && ! $this->params[$class][$pos] instanceof UnresolvedParam;
         if ($explicitPos) {
             return $this->params[$class][$pos];
         }

         // is there a named value explicitly from the current class?
-        $explicitNamed = isset($this->params[$class][$name])
+        $explicitNamed = isset($this->params[$class])
+                 && array_key_exists($name, $this->params[$class])
                  && ! $this->params[$class][$name] instanceof UnresolvedParam;
         if ($explicitNamed) {
             return $this->params[$class][$name];
@@ -343,7 +345,7 @@
         // is there a named value implicitly inherited from the parent class?
         // (there cannot be a positional parent. this is because the unified
         // values are stored by name, not position.)
-        $implicitNamed = isset($parent[$name])
+        $implicitNamed = array_key_exists($name, $parent)
                  && ! $parent[$name] instanceof UnresolvedParam;
         if ($implicitNamed) {
             return $parent[$name];

I couldn't directly change the file on GitHub, because all lines were shown as changed (dunno why - line breaks, spaces/tabs,...?). So please implement this patch yourself. Thanks.

harikt commented 9 years ago

Spaces are used. No tabs. May be that is the reason. In case if you are sending a PR do send a test case. I hope this is with v2 ?

cziegenberg commented 9 years ago

No, I tested with v3, but perhaps the same problem exists in v2.

dstockto commented 9 years ago

It doesn't appear to be an issue with 2.x.

pmjones commented 9 years ago

Fixed in 1bd6da6 -- thanks!