Skip to content

Commit e78b79a

Browse files
committed
ContainerBuilder: warns when autowired service type is unknown (BC break)
1 parent bc37288 commit e78b79a

7 files changed

+25
-1
lines changed

src/DI/ContainerBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ private function resolveServiceClass($name, $recursive = array())
348348
throw new ServiceCreationException("Class or interface $def->class used in service '$name' not found.");
349349
}
350350
$def->class = Reflection\ClassType::from($def->class)->getName();
351+
352+
} elseif ($def->autowired) {
353+
trigger_error("Type of service '$name' is unknown.", E_USER_NOTICE);
351354
}
352355
return $def->class;
353356
}

tests/DI/Compiler.extensionOverride.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require __DIR__ . '/../bootstrap.php';
1313

1414
class Factory
1515
{
16+
/** @return Lorem */
1617
static function createLorem($arg = NULL)
1718
{
1819
return new Lorem($arg);
@@ -22,6 +23,7 @@ class Factory
2223

2324
class IpsumFactory
2425
{
26+
/** @return Ipsum */
2527
static function create($arg = NULL)
2628
{
2729
return new Ipsum($arg);

tests/DI/Compiler.services.factory.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require __DIR__ . '/../bootstrap.php';
1313

1414
class Factory
1515
{
16+
/** @return Lorem */
1617
static function createLorem($arg)
1718
{
1819
return new Lorem(__METHOD__ . ' ' . $arg);
@@ -32,6 +33,7 @@ class Lorem
3233
$this->arg = $arg;
3334
}
3435

36+
/** @return Lorem */
3537
function foo()
3638
{
3739
$this->foo = func_get_args();

tests/DI/ContainerBuilder.basic.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Service
1515
{
1616
public $methods;
1717

18+
/** @return Service */
1819
static function create(DI\Container $container = NULL)
1920
{
2021
return new self(array_slice(func_get_args(), 1));

tests/DI/ContainerBuilder.basic2.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Factory
1616
private function __construct()
1717
{}
1818

19+
/** @return Factory */
1920
static function create()
2021
{
2122
return new self;

tests/DI/ContainerBuilder.factory.error.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,16 @@ Assert::exception(function() {
119119
$builder->addDefinition('one')->setFactory('Bad6::create');
120120
$builder->generateClasses();
121121
}, 'Nette\InvalidStateException', "Factory 'Bad6::create' used in service 'one' is not callable.");
122+
123+
124+
class Bad7
125+
{
126+
static function create()
127+
{}
128+
}
129+
130+
Assert::error(function() {
131+
$builder = new DI\ContainerBuilder;
132+
$builder->addDefinition('one')->setFactory('Bad7::create');
133+
$builder->generateClasses();
134+
}, E_USER_NOTICE, "Type of service 'one' is unknown.");

tests/DI/files/compiler.services.factory.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ services:
5454

5555
rich5: Factory()(1)
5656

57-
rich6: Factory::getClass(1)(1)
57+
rich6:
58+
factory: Factory::getClass(1)(1)
59+
class: Lorem

0 commit comments

Comments
 (0)