diff --git a/src/DI/Definitions/LocatorDefinition.php b/src/DI/Definitions/LocatorDefinition.php index fe8b3aecd..f4fee5d66 100644 --- a/src/DI/Definitions/LocatorDefinition.php +++ b/src/DI/Definitions/LocatorDefinition.php @@ -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, @@ -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); } } diff --git a/tests/DI/Compiler.generatedLocator.phpt b/tests/DI/Compiler.generatedLocator.phpt index 34816729e..940f25432 100644 --- a/tests/DI/Compiler.generatedLocator.phpt +++ b/tests/DI/Compiler.generatedLocator.phpt @@ -42,7 +42,8 @@ interface LocatorFactoryN } -$container = createContainer(new DI\Compiler, ' +// create($name) is deprecated +$container = @createContainer(new DI\Compiler, ' services: - LoremChild diff --git a/tests/DI/Definitions.LocatorDefinition.api.phpt b/tests/DI/Definitions.LocatorDefinition.api.phpt index 50a771bd0..aaef81002 100644 --- a/tests/DI/Definitions.LocatorDefinition.api.phpt +++ b/tests/DI/Definitions.LocatorDefinition.api.phpt @@ -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 () { @@ -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()); }); diff --git a/tests/DI/Definitions.LocatorDefinition.render.create.phpt b/tests/DI/Definitions.LocatorDefinition.render.create.phpt index 4c649129f..833fdbf75 100644 --- a/tests/DI/Definitions.LocatorDefinition.render.create.phpt +++ b/tests/DI/Definitions.LocatorDefinition.render.create.phpt @@ -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;