Skip to content
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

[DI] Initialize properties before method calls #20566

Closed
wants to merge 3 commits into from
Closed

[DI] Initialize properties before method calls #20566

wants to merge 3 commits into from

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Nov 19, 2016

Q A
Branch? 2.7
Bug fix? yes-ish
New feature? no
BC breaks? not sure
Deprecations? no
Tests pass? not yet (only dumps seem to fail)
Fixed tickets comma-separated list of tickets fixed by the PR, if any
License MIT
Doc PR reference to the documentation PR, if any

Given

services:
    handler:
        class: AppBundle\Handler
        properties:
            debug: '%kernel.debug%'
        calls:
            - [handle]

I totally expected Handler::$debug to be set before Handler::handle is called. It was not.. and it's really annoying :)

@Tobion
Copy link
Contributor

Tobion commented Nov 19, 2016

The change makes sense to me.

@linaori
Copy link
Contributor

linaori commented Nov 19, 2016

Isn't this a case where a configurator would make more sense? (though I agree with the fix)

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 19, 2016

It's really simple (property) "configuration" (only initializing) :) i'd prefer this approach working out-of-the-box instead of setting up a configurator class, etc.

@ro0NL ro0NL changed the base branch from master to 2.7 November 20, 2016 09:01
@fabpot
Copy link
Member

fabpot commented Nov 22, 2016

Tests are broken (the dumped containers should be updated).

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 22, 2016

@fabpot green (ignore fabbot.io)

@fabpot
Copy link
Member

fabpot commented Nov 24, 2016

Now, the question is wether we need to merge this in 2.7 or 3.2. I would vote for 3.2.

@fabpot fabpot added this to the 3.2 milestone Nov 24, 2016
@xabbuh
Copy link
Member

xabbuh commented Nov 24, 2016

To me, this looks like a bugfix and should be merged into 2.7. Merging this into 3.2 also has the drawback that the behaviour does change between supported versions which might be needless additional work for maintainers who need this feature for 2.x and 3.x.

@nicolas-grekas
Copy link
Member

Would it be possible to have a test case that shows why this ordering is important? The fixture update doesn't qualify as such to me (too easy to update them without considering the actual side effect).

@nicolas-grekas
Copy link
Member

Ping @symfony/deciders , we need to make a decision on this one before the end of the week for 3.2 to be ready (1/3).

@xabbuh
Copy link
Member

xabbuh commented Nov 24, 2016

👍 for merging this into 2.7 and if possible in any way with a test like suggested by @nicolas-grekas

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 24, 2016

Ill add the test somewhere tonight 👍 this is about asserting a property is initialized within a method call right?

@nicolas-grekas nicolas-grekas modified the milestone: 3.2 Nov 24, 2016
@stof
Copy link
Member

stof commented Nov 24, 2016

@ro0NL yes

@@ -798,6 +798,20 @@ public function testLazyLoadedService()

$this->assertTrue($classInList);
}

public function testInitialziePropertiesBeforeMethodCalls()
Copy link
Member

Choose a reason for hiding this comment

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

typo: Initialize

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The approach is OK though?

Copy link
Member

Choose a reason for hiding this comment

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

looks good yes

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 24, 2016

Test added.. however it does not involve the PhpDumper i realize now :) It covers only ContainerBuilder 🤔 Should we add a similar test there as well?

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 24, 2016

Let's be safe :)

Speaking about.. what kind of case could we break in real life apps on 2.7? As it's only affected during service initialization. Imo. it qualifies a bugfix.

@Tobion
Copy link
Contributor

Tobion commented Nov 25, 2016

Considering the impact of bug fixes like #20562, it might be safer to change the behavior only in 3.2. But I'm fine either way.

@nicolas-grekas
Copy link
Member

I have the same kind of thought than @Tobion . Yet, if this happens to break something, breaking 3.2 is not better than breaking 2.7. For this reason, and also because I fail to see where this can break anything reasonable, I'm 👍 for 2.7.

@stof
Copy link
Member

stof commented Nov 25, 2016

The only case we could break is when you have a method call changing behavior when the property is initialized or no, and initializing the property as a public property. This case looks quite insane though (and if the initialization relies on this to be working, the class is quite unusable directly).

So I'm also 👍 for 2.7 as it allows methods to rely on the property values.

@nicolas-grekas
Copy link
Member

Good catch, thanks @ro0NL.

nicolas-grekas added a commit that referenced this pull request Nov 25, 2016
This PR was squashed before being merged into the 2.7 branch (closes #20566).

Discussion
----------

[DI] Initialize properties before method calls

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes-ish
| New feature?  | no
| BC breaks?    | not sure
| Deprecations? | no
| Tests pass?   | not yet (only dumps seem to fail)
| Fixed tickets | comma-separated list of tickets fixed by the PR, if any
| License       | MIT
| Doc PR        | reference to the documentation PR, if any

Given

```yml
services:
    handler:
        class: AppBundle\Handler
        properties:
            debug: '%kernel.debug%'
        calls:
            - [handle]
```

I totally expected `Handler::$debug` to be set before `Handler::handle` is called. It was not..  and it's really annoying :)

Commits
-------

0af433b [DI] Initialize properties before method calls
@ro0NL ro0NL deleted the di/props-before-calls branch November 25, 2016 09:50
@fabpot
Copy link
Member

fabpot commented Nov 25, 2016

That's yet again a very wrong argument: making such a change in 2.7.x is very different from making the change in 2.x. Also because we can document the change in the UPGRADE file. I would not have merged it in 2.7 for that reason. We are doing too much of these, we should stop. We had problems in the past and it looks like we are not learning from them.

@nicolas-grekas
Copy link
Member

May I add that it's not much better to break 2/3.x than it is to break 2.7.x. What I mean is than when we change a behavior, we must force ourselves to find a way to first deprecate the old one. Thus, this will naturally make such PRs move to master (or be rejected). Learning from past mistakes here IMHO means being stricter to what we consider a behavioral change and what we don't (not arguing about this specific PR thought.)

@fabpot
Copy link
Member

fabpot commented Nov 25, 2016

There is of course a big difference between "breaking" x.y.z and breaking x.y.

@linaori
Copy link
Contributor

linaori commented Nov 25, 2016 via email

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 26, 2016

Imo. having different behavior cross-version should be avoided as much as possible. I still think the impact minimal, as @stof mentioned.. people are doing really weird things then (if they notice at all).

However, there are also things like EOM/EOL....

And the line is really thin here.. yes it's a behavior change, yet pragmatically, it qualifies a bugfix (imo) but technically it's a new feature for sure.

@fabpot fabpot mentioned this pull request Nov 27, 2016
This was referenced Dec 13, 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.

8 participants