Skip to content

Commit

Permalink
Improved type resolution stack ordering
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Nov 27, 2023
1 parent 348a569 commit 3785611
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Improved type resolution stack ordering

## v0.1.3 (2023-11-27)
* Use normalized variable-valid parameter names
* Add parameters as types if object
Expand Down
76 changes: 50 additions & 26 deletions src/Slingshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,13 @@ public function invoke(

$ref = new ReflectionFunction($function);
$args = [];
$parameters = array_merge($this->parameters, $parameters);
$params = $parameters;
$parameters = $this->parameters;

foreach ($params as $key => $value) {
$key = $this->normalizeParameterName($key);
$parameters[$key] = $value;
}

foreach ($ref->getParameters() as $param) {
$type = $param->getType();
Expand Down Expand Up @@ -386,16 +392,53 @@ public function invoke(
// Container value
if (
$typeName !== null &&
$this->container
$this->container &&
$this->container->has($typeName) &&
null !== ($value = $this->container->get($typeName)) &&
$this->checkType($value, $type)
) {
$args[$name] = $value;
continue;
}


// Dovetail
if (
class_exists(Dovetail::class) &&
$typeName !== null &&
preg_match('/([A-Z][a-z0-9_]+)\\\\Config$/', $typeName, $matches) &&
Dovetail::canLoad($matches[1])
) {
if ($this->container instanceof PandoraContainer) {
$value = $this->container->tryGet($typeName);
} elseif ($this->container->has($typeName)) {
$value = $this->container->get($typeName);
try {
$args[$name] = Dovetail::load($matches[1]);
} catch (Throwable $e) {
self::$stack--;
throw $e;
}

continue;
}


// Nullable
if (
$typeName !== null &&
$type !== null &&
$type->allowsNull() &&
!$param->isDefaultValueAvailable()
) {
$args[$name] = null;
continue;
}

if ($value !== null) {

// Pandora
if (
$typeName !== null &&
$this->container &&
$this->container instanceof PandoraContainer &&
null !== ($value = $this->container->tryGet($typeName))
) {
$args[$name] = $value;
continue;
}
Expand Down Expand Up @@ -428,7 +471,6 @@ public function invoke(

// Null
if (
$value === null &&
$type !== null &&
$type->allowsNull()
) {
Expand All @@ -453,24 +495,6 @@ public function invoke(
}


// Dovetail
if (
class_exists(Dovetail::class) &&
$typeName !== null &&
preg_match('/([A-Z][a-z0-9_]+)\\\\Config$/', $typeName, $matches) &&
Dovetail::canLoad($matches[1])
) {
try {
$args[$name] = Dovetail::load($matches[1]);
} catch (Throwable $e) {
self::$stack--;
throw $e;
}

continue;
}


self::$stack--;

if (isset($parameters[$name])) {
Expand Down

0 comments on commit 3785611

Please sign in to comment.