Skip to content

[DependencyInjection] Add type-hints to interfaces and implementations. #32266

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

Merged

Conversation

derrabus
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #32179
License MIT
Doc PR N/A

Part II for DependencyInjection, concentrating on the main interfaces, ContainerBuiler and related classes.

@@ -51,7 +48,7 @@ public function set($id, $service);
*
* @see Reference
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
public function get($id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: We cannot add string $id because we would violate the PSR interface otherwise.

@derrabus derrabus force-pushed the improvement/type-hints-on-di-interfaces branch from 4342375 to 1b401f9 Compare June 28, 2019 22:10
@derrabus derrabus marked this pull request as ready for review June 28, 2019 22:13
@derrabus derrabus mentioned this pull request Jun 28, 2019
57 tasks
@derrabus derrabus force-pushed the improvement/type-hints-on-di-interfaces branch from 1b401f9 to f421681 Compare June 29, 2019 15:39
@derrabus derrabus force-pushed the improvement/type-hints-on-di-interfaces branch from f421681 to 13099b9 Compare June 29, 2019 16:41
@nicolas-grekas nicolas-grekas added this to the 5.0 milestone Jun 30, 2019
fabpot added a commit that referenced this pull request Jul 3, 2019
…getInEdges()/getOutEdges() (derrabus)

This PR was merged into the 3.4 branch.

Discussion
----------

[DependencyInjection] Annotated correct return type for getInEdges()/getOutEdges()

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

This changed annotation was quite helpful when analyzing `PhpDumper` for #32266.

Commits
-------

28882f5 Annotated correct return type for getInEdges()/getOutEdges().
@@ -1334,7 +1336,7 @@ public function getParameterBag()

EOF;
if (!$this->asFiles) {
$code = preg_replace('/^.*buildParameters.*\n.*\n.*\n/m', '', $code);
$code = preg_replace('/^.*buildParameters.*\n.*\n.*\n\n?/m', '', $code);
Copy link
Member

Choose a reason for hiding this comment

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

Should be reverted as it does not look related to the purpose of this PR, is it?

Copy link
Member Author

Choose a reason for hiding this comment

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

This class generates implementations of the ContainerInterface that has been changed with this PR. This is why I've adjusted the code template above to add the new type declarations and remove the now-obsolete type cast.

This preg_replace call removes an if block from the generated code under certain circumstances. Because I've removed the type cast, this operation would leave an empty line at the beginning of the method, like this:

public function hasParameter(string $name)
{

    return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
}

I've adjusted the regular expression, so that this extra new line is also removed in case its there.

@Tobion
Copy link
Contributor

Tobion commented Jul 5, 2019

There are still several uneccessary string casts left, e.g.

@derrabus derrabus force-pushed the improvement/type-hints-on-di-interfaces branch from 492df5e to d44d0a1 Compare July 5, 2019 18:51
@Tobion
Copy link
Contributor

Tobion commented Jul 5, 2019

There seems to be something wrong in the failing tests testBindScalarValueToControllerArgument and testRegistersTraceableBusesToCollector

@derrabus
Copy link
Member Author

derrabus commented Jul 5, 2019

I'll look into it.

Tobion added a commit that referenced this pull request Jul 5, 2019
…tances as class name to Definition (derrabus)

This PR was merged into the 4.4 branch.

Discussion
----------

[DependencyInjection] Deprecated passing Parameter instances as class name to Definition

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #32279
| License       | MIT
| Doc PR        | N/A

This PR deprecates the undocumented possibility to use a `Parameter` instance instead of a string as class name for a `Definition`. This was discovered while working on #32266.

Commits
-------

edfc9d6 Deprecated passing Parameter instances as class name to Definition.
@derrabus derrabus force-pushed the improvement/type-hints-on-di-interfaces branch from d44d0a1 to d45497b Compare July 6, 2019 16:02
@derrabus
Copy link
Member Author

derrabus commented Jul 6, 2019

All right, so the failing test assumes that a service can be a scalar value.

$this->assertSame('foo_val', $container->get((string) $reference));

All docblocks on the DI classes and interfaces however promise me that services are always objects.

* @param object $service The service instance

* @return object The associated service

I trusted these docblocks and used object type-hints for services and those make the test mentioned by @Tobion fail. Interestingly enough, all tests of the DI component itself are green with the object type-hints. The failing test I linked above comes from the HttpKernel component.

I'm unsure if scalar services should be a thing, but they're possible at the moment, at least to a certain degree. In order to move forward here, I'll remove the object type-hints for now, so that we can decide afterwards if we either want to deprecate scalar services or update the docblocks.

@Tobion
Copy link
Contributor

Tobion commented Jul 6, 2019

According to PSR-11 it could also be a scalar value.

use of a container to obtain objects and parameters

But that seems strange and not the original contract of the symfony container. Also parameters are not fetched individually from the container but through a ParameterBag in Symfony which makes more sense to me.

@derrabus derrabus force-pushed the improvement/type-hints-on-di-interfaces branch from d45497b to 68f9f27 Compare July 6, 2019 18:04
@derrabus
Copy link
Member Author

derrabus commented Jul 6, 2019

The remaining failing tests are unrelated to my changes.

Status: Needs Review

@derrabus
Copy link
Member Author

derrabus commented Jul 6, 2019

Follow-up issue: #32411

@Tobion
Copy link
Contributor

Tobion commented Jul 6, 2019

Thank you @derrabus.

@Tobion Tobion merged commit 68f9f27 into symfony:master Jul 6, 2019
Tobion added a commit that referenced this pull request Jul 6, 2019
…mplementations. (derrabus)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[DependencyInjection] Add type-hints to interfaces and implementations.

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

Part II for DependencyInjection, concentrating on the main interfaces, `ContainerBuiler` and related classes.

Commits
-------

68f9f27 [DependencyInjection] Add type-hints to interfaces and implementations.
@derrabus derrabus deleted the improvement/type-hints-on-di-interfaces branch July 6, 2019 18:57
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