Skip to content

Commit

Permalink
do not use reflection to expand parameters, use parameter settings to…
Browse files Browse the repository at this point in the history
… find out if a parameter is variadic
  • Loading branch information
frederikbosch committed May 16, 2024
1 parent dc90cfc commit d3175ef
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
38 changes: 32 additions & 6 deletions src/Resolver/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ final class Blueprint
/**
* @var string
*/
private $className;
private string $className;

/**
* @var array
*/
private $params;
private array $params;

/**
* @var array
*/
private $setters;
private array $setters;

/**
* @var array
*/
private $mutations;
private array $mutations;

/**
* @var array
*/
private array $paramSettings = [];

/**
* @param string $className
Expand All @@ -47,7 +52,7 @@ public function __construct(
string $className,
array $params = [],
array $setters = [],
array $mutations = []
array $mutations = [],
)
{
$this->className = $className;
Expand All @@ -65,12 +70,14 @@ public function __construct(
*/
public function merge(Blueprint $mergeBlueprint): Blueprint
{
return new Blueprint(
$blueprint = new Blueprint(
$this->className,
$this->mergeParams($mergeBlueprint),
$this->mergeSetters($mergeBlueprint),
$this->mergeMutations($mergeBlueprint)
);
$blueprint->paramSettings = array_merge($this->paramSettings, $mergeBlueprint->paramSettings);
return $blueprint;
}

/**
Expand Down Expand Up @@ -159,6 +166,25 @@ public function getMutations(): array
return $this->mutations;
}

/**
* @param array $paramSettings
* @return Blueprint
*/
public function withParamSettings(array $paramSettings): self
{
$clone = clone $this;
$clone->paramSettings = $paramSettings;
return $clone;
}

/**
* @return array
*/
public function getParamSettings(): array
{
return $this->paramSettings;
}

/**
* @param array $params
* @return Blueprint
Expand Down
26 changes: 17 additions & 9 deletions src/Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,15 @@ public function getUnified(string $class): Blueprint
$spec = new Blueprint($class);
}

// stores the unified params and setters
$this->unified[$class] = new Blueprint(
$unified = new Blueprint(
$class,
$this->getUnifiedParams($class, $spec->getParams()),
$this->getUnifiedSetters($class, $spec->getSetters()),
$this->getUnifiedMutations($class, $spec->getMutations())
$this->getUnifiedMutations($class, $spec->getMutations()),
);

// done, return the unified values
return $this->unified[$class];
// stores and returns the unified params and setters
return $this->unified[$class] = $unified->withParamSettings($this->getParamSettings($class));
}

/**
Expand Down Expand Up @@ -502,6 +501,17 @@ protected function getUnifiedSetters(string $class, array $parent): array
return $unified;
}

private function getParamSettings(string $class): array
{
$unified = [];
$rparams = $this->reflector->getParams($class);
foreach ($rparams as $rparam) {
$unified[$rparam->getName()] = $rparam->isVariadic();
}

return $unified;
}

/**
* Expands variadic parameters onto the end of a contructor parameters array.
*
Expand All @@ -511,13 +521,11 @@ protected function getUnifiedSetters(string $class, array $parent): array
*/
protected function expandParams(Blueprint $blueprint): Blueprint
{
$class = $blueprint->getClassName();
$params = $blueprint->getParams();

$variadicParams = [];
foreach ($this->reflector->getParams($class) as $reflectParam) {
$paramName = $reflectParam->getName();
if ($reflectParam->isVariadic() && is_array($params[$paramName])) {
foreach ($blueprint->getParamSettings() as $paramName => $isVariadic) {
if ($isVariadic && is_array($params[$paramName])) {
$variadicParams = array_merge($variadicParams, $params[$paramName]);
unset($params[$paramName]);
break; // There can only be one
Expand Down

0 comments on commit d3175ef

Please sign in to comment.