Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DI][Bug] Autowiring thinks optional args on core classes are required#23605

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:3.3fromweaverryan:optional-args-core-classes
Jul 21, 2017

Conversation

@weaverryan
Copy link
Member

QA
Branch?3,3
Bug fix?yes
New feature?no
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsnone
LicenseMIT
Doc PRn/a

Currently, the following fails:

services:PDO:class:PDOarguments:            -'sqlite:/foo.db'

The error:

Cannot autowire service "PDO": argument "$username" of method "__construct()" must have a type-hint or be given a value explicitly

$username is the second argument toPDO, and it's optional. Here's the reason: it appears that$parameter->isDefaultValueAvailable() returns false for optional arguments of core classes. But,$parameter->isOptional() returns true.

This allows optional arguments to not throw an exception. I can't think of any edge cases this will cause - but it's possible I'm not thinking of something :).

Cheers!

… autowire optional args as if they were required
// For core classes, isDefaultValueAvailable() can
// be false when isOptional() returns true. If the
// argument *is* optional, allow it to be missing
if ($parameter->isOptional()) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not change the!$parameter->isDefaultValueAvailable() condition instead?

Copy link
Member

@dunglasdunglasJul 21, 2017
edited
Loading

Choose a reason for hiding this comment

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

@fabpot it was the default behavior, but@nicolas-grekas changed it to make the pass more predictible:https://github.com/symfony/symfony/pull/22256/files#diff-62df969ae028c559d33ffd256de1ac49R253

Copy link
Member

Choose a reason for hiding this comment

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

But yes, this change basically reverts@nicolas-grekas's change.

Choose a reason for hiding this comment

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

nope, it's doesn't revert it, it makes it stronger - while reading, the previous was unclear ("why does this imply that" questions) - now it's doubly clearer :)

sstok reacted with thumbs up emojisstok reacted with laugh emoji
// be false when isOptional() returns true. If the
// argument *is* optional, allow it to be missing
if ($parameter->isOptional()) {
continue;
Copy link
Member

@nicolas-grekasnicolas-grekasJul 21, 2017
edited
Loading

Choose a reason for hiding this comment

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

this has to be a "break", because we cannot have holes in the parameters list

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

There could be holes withbreak orcontinue. The already-specified$arguments are passed into this function (imagine that thanks to named arguments, someone has specified the 1st and 4th arguments to something (i.e. $arguments has0 and3 indexes). If we hit this code when trying to autowire the 2nd argument (index1), then abreak would still mean that$arguments ultimately has indexes0 and3 (the same with acontinue: continue would loop again, but not fill in any args).

So I think changing tobreak makes sense... but we still have holes. This is caught later byCheckArgumentsValidityPass with:

Invalid constructor argument 4 for service "foo_bar": argument 2 must be defined before. Check your service definition.

tl;dr This DOES allow for holes in the parameters list in this pass... but that problem is caught by a later pass.

Copy link
Member

@nicolas-grekasnicolas-grekasJul 21, 2017
edited
Loading

Choose a reason for hiding this comment

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

it does notcreate holes - of course, if the input has, it can only fill some of them :)

@fabpot
Copy link
Member

Thank you@weaverryan.

@fabpotfabpot merged commit178a0f7 intosymfony:3.3Jul 21, 2017
fabpot added a commit that referenced this pull requestJul 21, 2017
…are required (weaverryan)This PR was merged into the 3.3 branch.Discussion----------[DI][Bug] Autowiring thinks optional args on core classes are required| Q             | A| ------------- | ---| Branch?       | 3,3| Bug fix?      | yes| New feature?  | no| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | none| License       | MIT| Doc PR        | n/aCurrently, the following fails:```ymlservices:    PDO:        class: PDO        arguments:            - 'sqlite:/foo.db'```The error:> Cannot autowire service "PDO": argument "$username" of method "__construct()" must have a type-hint or be given a value explicitly`$username` is the second argument to `PDO`, and it's optional. Here's the reason: it appears that `$parameter->isDefaultValueAvailable()` returns false for optional arguments of core classes. But, `$parameter->isOptional()` returns true.This allows optional arguments to not throw an exception. I can't think of any edge cases this will cause - but it's possible I'm not thinking of something :).Cheers!Commits-------178a0f7 Fixing a bug where if a core class was autowired, autowiring tried to autowire optional args as if they were required
@weaverryanweaverryan deleted the optional-args-core-classes branchJuly 21, 2017 17:58
@nicolas-grekas
Copy link
Member

It looks like we missed checking appveyor, where we hit a php 5.5.9 bug, see eg
https://ci.appveyor.com/project/fabpot/symfony/build/1.0.24693#L1598

which fails because $username is not optional in 5.5.9 (but it is in all following versions):
https://3v4l.org/n8ihn

@nicolas-grekas
Copy link
Member

Fixed in#23631

@fabpotfabpot mentioned this pull requestAug 1, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasdunglas approved these changes

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@xabbuhxabbuhxabbuh approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

6 participants

@weaverryan@fabpot@nicolas-grekas@dunglas@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp