You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #22185 [DI] Enhance DX by throwing instead of triggering a deprecation notice (nicolas-grekas)
This PR was merged into the 3.3-dev branch.
Discussion
----------
[DI] Enhance DX by throwing instead of triggering a deprecation notice
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | no
| BC breaks? | yes - at the config file level, for edge cases
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #22143
| License | MIT
| Doc PR | -
Looking at the linked issue, I'm reconsidering our decision to trigger a deprecation notice when one uses `_instanceof` or `_defaults` as a service name. While on the BC side, this is strict - on the DX side, it looks like this opens a trap where people fill fall into.
The same occurs to me with named args: instead of silently accepting invalid args as was the case before, let's throw to help DX when people do mistakes.
Last change in this PR: the complex logic required to force strings to be given as `$id` args into `Reference` or `Alias` makes no sense to me, especially considering that a `string` type hint on PHP7 will *do* a string cast.
Commits
-------
b07da3d [DI] Enhance DX by throwing instead of triggering a deprecation notice
Copy file name to clipboardExpand all lines: UPGRADE-3.3.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,6 +80,12 @@ Debug
80
80
DependencyInjection
81
81
-------------------
82
82
83
+
*[BC BREAK]`_defaults` and `_instanceof` are now reserved service names in Yaml configurations. Please rename any services with that names.
84
+
85
+
*[BC BREAK] non-numeric keys in methods and constructors arguments have never been supported and are now forbidden. Please remove them if you happen to have one.
86
+
87
+
* Service names that start with an underscore are deprecated in Yaml files and will be reserved in 4.0. Please rename any services with such names.
88
+
83
89
* Autowiring-types have been deprecated, use aliases instead.
Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,12 @@ Debug
73
73
DependencyInjection
74
74
-------------------
75
75
76
+
*`_defaults` and `_instanceof` are now reserved service names in Yaml configurations. Please rename any services with that names.
77
+
78
+
* Non-numeric keys in methods and constructors arguments have never been supported and are now forbidden. Please remove them if you happen to have one.
79
+
80
+
* Service names that start with an underscore are now reserved in Yaml files. Please rename any services with such names.
81
+
76
82
* Autowiring-types have been removed, use aliases instead.
@trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Alias to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
@trigger_error(sprintf('Using key "%s" for defining arguments of method "%s" for service "%s" is deprecated since Symfony 3.3 and will throw an exception in 4.0. Use no keys or $named arguments instead.', $key, $method, $this->currentId), E_USER_DEPRECATED);
52
-
}
49
+
if (is_int($key)) {
53
50
$resolvedArguments[] = $argument;
54
51
continue;
55
52
}
53
+
if ('' === $key || '$' !== $key[0]) {
54
+
thrownewInvalidArgumentException(sprintf('Invalid key "%s" found in arguments of method "%s" for service "%s": only integer or $named arguments are allowed.', $key, $method, $this->currentId));
@trigger_error(sprintf('Non-string service identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for service "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
475
473
}
476
474
if (isset($this->normalizedIds[$normalizedId = strtolower($id)])) {
thrownewInvalidArgumentException(sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in %s. Check your YAML syntax.', $id, $file));
214
220
}
@@ -217,7 +223,6 @@ private function parseDefinitions(array $content, $file)
if (!$this->isUnderscoredParamValid($content, '_defaults', $file)) {
245
+
if (!isset($content['services']['_defaults'])) {
241
246
returnarray();
242
247
}
243
-
244
248
$defaults = $content['services']['_defaults'];
245
249
unset($content['services']['_defaults']);
246
250
251
+
if (!is_array($defaults)) {
252
+
thrownewInvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', gettype($defaults), $file));
253
+
}
254
+
247
255
foreach ($defaultsas$key => $default) {
248
256
if (!isset(self::$defaultsKeywords[$key])) {
249
257
thrownewInvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::$defaultsKeywords)));
@@ -281,21 +289,6 @@ private function parseDefaults(array &$content, $file)
@trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Reference to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
-12Lines changed: 0 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -229,18 +229,6 @@ public function testGetInsensitivity()
229
229
$this->assertSame($foo, $sc->get('Foo'), '->get() returns the service for the given id, and converts id to lowercase');
230
230
}
231
231
232
-
/**
233
-
* @group legacy
234
-
* @expectedDeprecation Non-string service identifiers are deprecated since Symfony 3.3 and won't be supported in 4.0 for service "foo" ("Symfony\Component\DependencyInjection\Alias" given.) Cast it to string beforehand.
235
-
* @expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0. Using "Foo" instead of "foo" is deprecated since version 3.3.
0 commit comments