Skip to content

[DependencyInjection] Improve ParameterNotFoundException when accessing a nested parameter #19584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Aug 10, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR n/a

A common problem under beginners is to think that the dot notation is used to access nested arrays saved in parameters (common here means someone asks help for this problem at least once a week on IRC). Adding a little extra detail to the exception message and a working alternative should help pointing these people in the right direction before spending time debugging this.

It's quite late in the night over here, so the wording of the exception message probably isn't great. I'm happy to accept better suggestions 😃

@wouterj
Copy link
Member Author

wouterj commented Aug 10, 2016

travis failure unrelated

@@ -30,12 +31,13 @@ class ParameterNotFoundException extends InvalidArgumentException
* @param \Exception $previous The previous exception
* @param string[] $alternatives Some parameter name alternatives
*/
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array())
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array(), $nonNestedAlternative = null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$nonNestedAlternative should be added to docblock, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

A common problem under beginners is to think that the dot notation is used
to access nested arrays saved in parameters. Adding a little extra detail
to the exception message and a working alternative should help pointing
these people in the right direction before spending time debugging this.
@wouterj wouterj force-pushed the di/parameters-keys-error-experience branch from 1986299 to f7a47d8 Compare August 10, 2016 07:37
throw new ParameterNotFoundException($name, null, null, null, $alternatives);
$nonNestedAlternative = null;
if (!count($alternatives) && false !== strpos($name, '.')) {
$namePartsLength = array_map('strlen', explode('.', $name));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using implode ?

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

Thank you @wouterj.

@fabpot fabpot closed this in 0e63f47 Sep 14, 2016
@wouterj wouterj deleted the di/parameters-keys-error-experience branch September 15, 2016 07:36
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants