Skip to content

Commit

Permalink
LocatorDefinition: deprecated support for create($name) method (BC br…
Browse files Browse the repository at this point in the history
…eak)
  • Loading branch information
dg committed Feb 6, 2024
1 parent c257fa9 commit 8140289
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/DI/Definitions/LocatorDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setImplement(string $interface): static
|| (preg_match('#^(get|create)[A-Z]#', $method->name) && $method->getNumberOfParameters() === 0)
)) {
throw new Nette\InvalidArgumentException(sprintf(
"Service '%s': Method %s::%s() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.",
"Service '%s': Method %s::%s() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.",
$this->getName(),
$interface,
$method->name,
Expand All @@ -52,6 +52,8 @@ public function setImplement(string $interface): static
"return type of $interface::$method->name()",
allowNullable: true,
);
} elseif (str_starts_with($method->name, 'create')) {
trigger_error(sprintf("Service '%s': Method %s::create(\$name) is deprecated, use createFoo().", $this->getName(), $interface), E_USER_DEPRECATED);
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/DI/Compiler.generatedLocator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ interface LocatorFactoryN
}


$container = createContainer(new DI\Compiler, '
// create($name) is deprecated
$container = @createContainer(new DI\Compiler, '
services:
- LoremChild
Expand Down
12 changes: 6 additions & 6 deletions tests/DI/Definitions.LocatorDefinition.api.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,31 @@ Assert::exception(function () {
Assert::exception(function () {
$def = new LocatorDefinition;
$def->setImplement(Bad2::class);
}, Nette\InvalidArgumentException::class, "Service '': Method Bad2::create() does not meet the requirements: is create(\$name), get(\$name), create*() or get*() and is non-static.");
}, Nette\InvalidArgumentException::class, "Service '': Method Bad2::create() does not meet the requirements: is create*(), get*() or get(\$name) and is non-static.");


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


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


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


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


Assert::noError(function () {
Expand All @@ -121,7 +121,7 @@ Assert::noError(function () {

Assert::noError(function () {
$def = new LocatorDefinition;
$def->setImplement(Good2::class);
@$def->setImplement(Good2::class); // create($name) is deprecated
Assert::same(Good2::class, $def->getImplement());
Assert::same(Good2::class, $def->getType());
});
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Definitions.LocatorDefinition.render.create.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Good
test('', function () {
$def = new LocatorDefinition;
$def->setName('abc');
$def->setImplement(Good::class);
@$def->setImplement(Good::class); // create($name) is deprecated
$def->setReferences(['first' => '@a', 'second' => 'stdClass']);

$builder = new Nette\DI\ContainerBuilder;
Expand Down

0 comments on commit 8140289

Please sign in to comment.