Skip to content

Commit 8140289

Browse files
committed
LocatorDefinition: deprecated support for create($name) method (BC break)
1 parent c257fa9 commit 8140289

4 files changed

+12
-9
lines changed

src/DI/Definitions/LocatorDefinition.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function setImplement(string $interface): static
3939
|| (preg_match('#^(get|create)[A-Z]#', $method->name) && $method->getNumberOfParameters() === 0)
4040
)) {
4141
throw new Nette\InvalidArgumentException(sprintf(
42-
"Service '%s': Method %s::%s() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.",
42+
"Service '%s': Method %s::%s() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.",
4343
$this->getName(),
4444
$interface,
4545
$method->name,
@@ -52,6 +52,8 @@ public function setImplement(string $interface): static
5252
"return type of $interface::$method->name()",
5353
allowNullable: true,
5454
);
55+
} elseif (str_starts_with($method->name, 'create')) {
56+
trigger_error(sprintf("Service '%s': Method %s::create(\$name) is deprecated, use createFoo().", $this->getName(), $interface), E_USER_DEPRECATED);
5557
}
5658
}
5759

tests/DI/Compiler.generatedLocator.phpt

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ interface LocatorFactoryN
4242
}
4343

4444

45-
$container = createContainer(new DI\Compiler, '
45+
// create($name) is deprecated
46+
$container = @createContainer(new DI\Compiler, '
4647
services:
4748
- LoremChild
4849

tests/DI/Definitions.LocatorDefinition.api.phpt

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,31 +84,31 @@ Assert::exception(function () {
8484
Assert::exception(function () {
8585
$def = new LocatorDefinition;
8686
$def->setImplement(Bad2::class);
87-
}, Nette\InvalidArgumentException::class, "Service '': Method Bad2::create() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.");
87+
}, Nette\InvalidArgumentException::class, "Service '': Method Bad2::create() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.");
8888

8989

9090
Assert::exception(function () {
9191
$def = new LocatorDefinition;
9292
$def->setImplement(Bad3::class);
93-
}, Nette\InvalidArgumentException::class, "Service '': Method Bad3::get() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.");
93+
}, Nette\InvalidArgumentException::class, "Service '': Method Bad3::get() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.");
9494

9595

9696
Assert::exception(function () {
9797
$def = new LocatorDefinition;
9898
$def->setImplement(Bad4::class);
99-
}, Nette\InvalidArgumentException::class, "Service '': Method Bad4::foo() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.");
99+
}, Nette\InvalidArgumentException::class, "Service '': Method Bad4::foo() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.");
100100

101101

102102
Assert::exception(function () {
103103
$def = new LocatorDefinition;
104104
$def->setImplement(Bad5::class);
105-
}, Nette\InvalidArgumentException::class, "Service '': Method Bad5::get() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.");
105+
}, Nette\InvalidArgumentException::class, "Service '': Method Bad5::get() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.");
106106

107107

108108
Assert::exception(function () {
109109
$def = new LocatorDefinition;
110110
$def->setImplement(Bad6::class);
111-
}, Nette\InvalidArgumentException::class, "Service '': Method Bad6::get() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.");
111+
}, Nette\InvalidArgumentException::class, "Service '': Method Bad6::get() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.");
112112

113113

114114
Assert::noError(function () {
@@ -121,7 +121,7 @@ Assert::noError(function () {
121121

122122
Assert::noError(function () {
123123
$def = new LocatorDefinition;
124-
$def->setImplement(Good2::class);
124+
@$def->setImplement(Good2::class); // create($name) is deprecated
125125
Assert::same(Good2::class, $def->getImplement());
126126
Assert::same(Good2::class, $def->getType());
127127
});

tests/DI/Definitions.LocatorDefinition.render.create.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface Good
2222
test('', function () {
2323
$def = new LocatorDefinition;
2424
$def->setName('abc');
25-
$def->setImplement(Good::class);
25+
@$def->setImplement(Good::class); // create($name) is deprecated
2626
$def->setReferences(['first' => '@a', 'second' => 'stdClass']);
2727

2828
$builder = new Nette\DI\ContainerBuilder;

0 commit comments

Comments
 (0)