Skip to content

Commit 9334a93

Browse files
committed
LocatorDefinition: removed support for create($name) methods (BC break)
1 parent 8ec49c0 commit 9334a93

File tree

4 files changed

+4
-104
lines changed

4 files changed

+4
-104
lines changed

src/DI/Definitions/LocatorDefinition.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function setImplement(string $interface): static
3535

3636
foreach ($methods as $method) {
3737
if ($method->isStatic() || !(
38-
(preg_match('#^(get|create)$#', $method->name) && $method->getNumberOfParameters() === 1)
38+
($method->name === 'get' && $method->getNumberOfParameters() === 1)
3939
|| (preg_match('#^(get|create)[A-Z]#', $method->name) && $method->getNumberOfParameters() === 0)
4040
)) {
4141
throw new Nette\InvalidArgumentException(sprintf(
@@ -46,15 +46,13 @@ public function setImplement(string $interface): static
4646
));
4747
}
4848

49-
if ($method->getNumberOfParameters() === 0) {
49+
if ($method->name !== 'get') {
5050
Nette\DI\Helpers::ensureClassType(
5151
Nette\Utils\Type::fromReflection($method),
5252
"return type of $interface::$method->name()",
5353
$this->getDescriptor(),
5454
allowNullable: true,
5555
);
56-
} elseif (str_starts_with($method->name, 'create')) {
57-
trigger_error(sprintf("Service '%s': Method %s::create(\$name) is deprecated, use createFoo().", $this->getName(), $interface), E_USER_DEPRECATED);
5856
}
5957
}
6058

@@ -155,7 +153,7 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
155153
$methodInner->setBody('if (!isset($this->mapping[$name])) {
156154
' . ($nullable ? 'return null;' : 'throw new Nette\DI\MissingServiceException("Service \'$name\' is not defined.");') . '
157155
}
158-
return $this->container->' . $m[1] . 'Service($this->mapping[$name]);')
156+
return $this->container->getService($this->mapping[$name]);')
159157
->addParameter('name');
160158

161159
} elseif (isset($this->references[$name])) {

tests/DI/Compiler.generatedLocator.phpt

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ interface LocatorFactoryN
4242
}
4343

4444

45-
// create($name) is deprecated
46-
$container = @createContainer(new DI\Compiler, '
45+
$container = createContainer(new DI\Compiler, '
4746
services:
4847
- LoremChild
4948
@@ -64,10 +63,7 @@ services:
6463
6564
one: Locator(a: @lorem1, b: LoremChild)
6665
two: Locator(tagged: a)
67-
three: LocatorFactory(a: @lorem1, b: LoremChild)
68-
four: LocatorFactory(tagged: b)
6966
five: LocatorN(tagged: a)
70-
six: LocatorFactoryN(tagged: a)
7167
seven: Locator(a: @lorem1)
7268
eight: Locator(a: LoremChild())
7369
');
@@ -96,29 +92,12 @@ Assert::exception(
9692
"Service '3' is not defined.",
9793
);
9894

99-
// factory
100-
$three = $container->getService('three');
101-
Assert::type(Lorem::class, $three->create('a'));
102-
Assert::type(LoremChild::class, $three->create('b'));
103-
Assert::notSame($three->create('a'), $three->create('a'));
104-
105-
// tagged factory
106-
$four = $container->getService('four');
107-
Assert::type(Lorem::class, $four->create('3'));
108-
Assert::notSame($container->getService('lorem3'), $four->create('3'));
109-
11095
// nullable accessor
11196
$five = $container->getService('five');
11297
Assert::type(Lorem::class, $five->get('1'));
11398
Assert::type(Lorem::class, $five->get('2'));
11499
Assert::null($five->get('3'));
115100

116-
// nullable factory
117-
$six = $container->getService('six');
118-
Assert::type(Lorem::class, $six->create('1'));
119-
Assert::type(Lorem::class, $six->create('2'));
120-
Assert::null($six->create('3'));
121-
122101
// accessor with one service
123102
$one = $container->getService('seven');
124103
Assert::type(Lorem::class, $one->get('a'));

tests/DI/Definitions.LocatorDefinition.api.phpt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ interface Good1
5050
public function get($name);
5151
}
5252

53-
interface Good2
54-
{
55-
public function create($name);
56-
}
57-
5853
interface Good3
5954
{
6055
public function createA(): stdClass;
@@ -127,14 +122,6 @@ Assert::noError(function () {
127122
});
128123

129124

130-
Assert::noError(function () {
131-
$def = new LocatorDefinition;
132-
@$def->setImplement(Good2::class); // create($name) is deprecated
133-
Assert::same(Good2::class, $def->getImplement());
134-
Assert::same(Good2::class, $def->getType());
135-
});
136-
137-
138125
Assert::noError(function () {
139126
$def = new LocatorDefinition;
140127
$def->setImplement(Good3::class);

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)