Skip to content

Commit ceea028

Browse files
committed
Resolver: improved error messages
1 parent ff3ed29 commit ceea028

6 files changed

+37
-26
lines changed

src/DI/Resolver.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,15 @@ private function completeException(\Exception $e, Definition $def): ServiceCreat
391391
if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "Service '")) {
392392
return $e;
393393
} else {
394-
$message = "Service '{$def->getName()}'" . ($def->getType() ? " (type of {$def->getType()})" : '') . ': ' . $e->getMessage();
394+
$name = $def->getName();
395+
$type = $def->getType();
396+
if (!$type) {
397+
$message = "Service '$name': " . $e->getMessage();
398+
} elseif (!$name || ctype_digit($name)) {
399+
$message = "Service of type $type: " . str_replace("$type::", '', $e->getMessage());
400+
} else {
401+
$message = "Service '$name' (type of $type): " . str_replace("$type::", '', $e->getMessage());
402+
}
395403
return $e instanceof ServiceCreationException
396404
? $e->setMessage($message)
397405
: new ServiceCreationException($message, 0, $e);
@@ -407,14 +415,17 @@ private function entityToString($entity): string
407415
: '@' . $ref->getValue();
408416
};
409417
if (is_string($entity)) {
410-
return $entity . '::__construct';
418+
return $entity . '::__construct()';
411419
} elseif ($entity instanceof Reference) {
412420
$entity = $referenceToText($entity);
413421
} elseif (is_array($entity)) {
422+
if (strpos($entity[1], '$') === false) {
423+
$entity[1] .= '()';
424+
}
414425
if ($entity[0] instanceof Reference) {
415426
$entity[0] = $referenceToText($entity[0]);
416427
} elseif (!is_string($entity[0])) {
417-
return 'method ' . $entity[1];
428+
return $entity[1];
418429
}
419430
return implode('::', $entity);
420431
}

tests/DI/Compiler.generatedFactory.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Assert::exception(function () {
287287
->getResultDefinition()
288288
->setFactory('Bad1');
289289
$builder->complete();
290-
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad2): Type hint for \$bar in Bad2::create() doesn't match type hint in Bad1 constructor.");
290+
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad2): Type hint for \$bar in create() doesn't match type hint in Bad1 constructor.");
291291

292292

293293

@@ -310,7 +310,7 @@ Assert::exception(function () {
310310
->getResultDefinition()
311311
->setFactory('Bad3');
312312
$builder->complete();
313-
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Unused parameter \$baz when implementing method Bad4::create(), did you mean \$bar?");
313+
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Unused parameter \$baz when implementing method create(), did you mean \$bar?");
314314

315315

316316

@@ -333,7 +333,7 @@ Assert::exception(function () {
333333
->getResultDefinition()
334334
->setFactory('Bad5');
335335
$builder->complete();
336-
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad6): Unused parameter \$baz when implementing method Bad6::create().");
336+
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad6): Unused parameter \$baz when implementing method create().");
337337

338338

339339

tests/DI/ContainerBuilder.autowiring.novalue.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Assert::exception(function () {
2424
$builder = new DI\ContainerBuilder;
2525
$builder->addDefinition('foo')->setType('Foo');
2626
$container = createContainer($builder);
27-
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Foo): Parameter \$x in Foo::__construct() has no class type hint or default value, so its value must be specified.");
27+
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Foo): Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");
2828

2929

3030
class Bar
@@ -38,7 +38,7 @@ Assert::exception(function () {
3838
$builder = new DI\ContainerBuilder;
3939
$builder->addDefinition('foo')->setType('Bar');
4040
$container = createContainer($builder);
41-
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Bar): Parameter \$x in Bar::__construct() has no class type hint or default value, so its value must be specified.");
41+
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Bar): Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");
4242

4343

4444
class Bar2

tests/DI/ContainerBuilder.factory.error.phpt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Assert::exception(function () {
6969
$builder->addFactoryDefinition('one')
7070
->setImplement('Bad4');
7171
$builder->complete();
72-
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Method Bad4::create() has not return type hint or annotation @return.");
72+
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Method create() has not return type hint or annotation @return.");
7373

7474

7575
interface Bad5
@@ -139,14 +139,14 @@ Assert::exception(function () {
139139
$builder = new DI\ContainerBuilder;
140140
$builder->addDefinition('one')->setFactory('Good', [new Statement('Unknown')]);
141141
$builder->complete();
142-
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Unknown not found. (used in Good::__construct)");
142+
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Unknown not found. (used in __construct())");
143143

144144
// fail in argument
145145
Assert::exception(function () {
146146
$builder = new DI\ContainerBuilder;
147147
$builder->addDefinition('one')->setFactory('Good', [new Statement('Bad8')]);
148148
$builder->complete();
149-
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Bad8 has private constructor. (used in Good::__construct)");
149+
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Bad8 has private constructor. (used in __construct())");
150150

151151

152152
abstract class Bad9
@@ -201,7 +201,7 @@ services:
201201
b: stdClass
202202
bad: ConstructorParam
203203
');
204-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct())");
204+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (needed by \$x in __construct())");
205205

206206

207207
// forced autowiring fail
@@ -212,7 +212,7 @@ services:
212212
b: stdClass
213213
bad: ConstructorParam(@\stdClass)
214214
');
215-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct)");
215+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (used in __construct())");
216216

217217

218218
// autowiring fail in chain
@@ -223,7 +223,7 @@ services:
223223
b: stdClass
224224
bad: MethodParam()::foo()
225225
');
226-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo())");
226+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in foo())");
227227

228228

229229
// forced autowiring fail in chain
@@ -234,7 +234,7 @@ services:
234234
b: stdClass
235235
bad: MethodParam()::foo(@\stdClass)
236236
');
237-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (used in method foo)");
237+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (used in foo())");
238238

239239

240240
// autowiring fail in argument
@@ -245,7 +245,7 @@ services:
245245
b: stdClass
246246
bad: Good(ConstructorParam())
247247
');
248-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct()) (used in Good::__construct)");
248+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct()) (used in __construct())");
249249

250250

251251
// forced autowiring fail in argument
@@ -256,7 +256,7 @@ services:
256256
b: stdClass
257257
bad: Good(ConstructorParam(@\stdClass))
258258
');
259-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct)");
259+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct())");
260260

261261

262262
// autowiring fail in chain in argument
@@ -267,7 +267,7 @@ services:
267267
b: stdClass
268268
bad: Good(MethodParam()::foo())
269269
');
270-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo()) (used in Good::__construct)");
270+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo()) (used in __construct())");
271271

272272

273273
// forced autowiring fail in chain in argument
@@ -278,7 +278,7 @@ services:
278278
b: stdClass
279279
bad: Good(MethodParam()::foo(@\stdClass))
280280
');
281-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in method foo)");
281+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())");
282282

283283

284284
// forced autowiring fail in property passing
@@ -306,7 +306,7 @@ services:
306306
setup:
307307
- $a = MethodParam()::foo(@\stdClass)
308308
');
309-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in method foo)");
309+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())");
310310

311311

312312
// autowiring fail in method calling
@@ -320,7 +320,7 @@ services:
320320
setup:
321321
- foo
322322
');
323-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo())");
323+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in foo())");
324324

325325

326326
// forced autowiring fail in method calling
@@ -334,7 +334,7 @@ services:
334334
setup:
335335
- bar(@\stdClass)
336336
');
337-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in @bad::bar)");
337+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in @bad::bar())");
338338

339339

340340
// autowiring fail in rich method calling
@@ -348,4 +348,4 @@ services:
348348
setup:
349349
- bar(MethodParam()::foo(@\stdClass))
350350
');
351-
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in method foo)");
351+
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())");

tests/DI/Definitions.AccessorDefinition.resolve.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Assert::exception(function () {
3737
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
3838
$resolver->resolveDefinition($def);
3939
$resolver->completeDefinition($def);
40-
}, Nette\DI\ServiceCreationException::class, "Service '' (type of Good1): Method Good1::get() has not return type hint or annotation @return.");
40+
}, Nette\DI\ServiceCreationException::class, 'Service of type Good1: Method get() has not return type hint or annotation @return.');
4141

4242

4343
Assert::noError(function () {
@@ -66,4 +66,4 @@ Assert::exception(function () {
6666
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
6767
$resolver->resolveDefinition($def);
6868
$resolver->completeDefinition($def);
69-
}, Nette\DI\ServiceCreationException::class, "Service '' (type of Good2): Service of type 'stdClass' not found.");
69+
}, Nette\DI\ServiceCreationException::class, "Service of type Good2: Service of type 'stdClass' not found.");

tests/DI/Definitions.FactoryDefinition.resolve.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Assert::exception(function () {
3636
$def->setImplement('Good1');
3737
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
3838
$resolver->resolveDefinition($def);
39-
}, Nette\DI\ServiceCreationException::class, "Service '' (type of Good1): Method Good1::create() has not return type hint or annotation @return.");
39+
}, Nette\DI\ServiceCreationException::class, 'Service of type Good1: Method create() has not return type hint or annotation @return.');
4040

4141

4242
Assert::noError(function () {

0 commit comments

Comments
 (0)