Skip to content

Commit

Permalink
more self explanatory message for factory and service mismatch (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 15, 2023
1 parent 2417bbc commit f485cae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/DI/Definitions/FactoryDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,22 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
$this->resultDefinition->getCreator()->arguments[$ctorParam->getPosition()] = new Php\Literal('$' . $ctorParam->name);

} elseif (!$this->resultDefinition->getSetup()) {
$hint = Nette\Utils\Helpers::getSuggestion(array_keys($ctorParams), $param->name);
// [param1, param2] => '$param1, $param2'
$stringifyParams = function (array $params): string {
return implode(', ', array_map(
function (string $param) { return '$' . $param; },
$params
));
};
$ctorParamsKeys = array_keys($ctorParams);
$hint = Nette\Utils\Helpers::getSuggestion($ctorParamsKeys, $param->name);
throw new ServiceCreationException(sprintf(
'Unused parameter $%s when implementing method %s::create()',
$param->name,
$interface
) . ($hint ? ", did you mean \${$hint}?" : '.'));
'Cannot implement %s::create(): factory method parameters (%s) are not matching %s::__construct() parameters (%s).',
$interface,
$stringifyParams(array_map(function (\ReflectionParameter $param) { return $param->name; }, $method->getParameters())),
$class,
$stringifyParams($ctorParamsKeys)
) . ($hint ? " Did you mean to use '\${$hint}' in factory method?" : ''));
}

$paramDef = $methodType . ' ' . $param->name;
Expand Down
4 changes: 2 additions & 2 deletions tests/DI/Compiler.generatedFactory.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ Assert::exception(function () {
$builder->addFactoryDefinition('one')
->setImplement(Bad4::class);
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Unused parameter \$baz when implementing method Bad4::create(), did you mean \$bar?");
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Cannot implement Bad4::create(): factory method parameters (\$baz) are not matching Bad3::__construct() parameters (\$bar). Did you mean to use '\$bar' in factory method?");



Expand All @@ -323,7 +323,7 @@ Assert::exception(function () {
$builder->addFactoryDefinition('one')
->setImplement(Bad6::class);
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad6): Unused parameter \$baz when implementing method Bad6::create().");
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad6): Cannot implement Bad6::create(): factory method parameters (\$baz) are not matching Bad5::__construct() parameters (\$xxx).");



Expand Down

0 comments on commit f485cae

Please sign in to comment.