Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make ServiceSubscriberTrait compatible with AbstractController for Symfony 7 with strong types #54490

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
wants to merge 1 commit into from

Conversation

pableu
Copy link
Contributor

@pableu pableu commented Apr 4, 2024

Q A
Branch? 7.0
Bug fix? yes
New feature? no
Deprecations? 🤔
License MIT

If you have a class like this:

You get the following error in Symfony 7 with symfony/contracts 3.4.2:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Service\ServiceSubscriberTrait;

abstract class DemoController extends AbstractController
{
    use ServiceSubscriberTrait;
    // ...
}

Compile Error: Symfony\Bundle\FrameworkBundle\Controller\AbstractController and Symfony\Contracts\Service\ServiceSubscriberTrait define the same property ($container) in the composition of App\Controller\DemoController. However, the definition differs and is considered incompatible. Class was composed

ServiceSubscriberTrait::$container is defined as:

    /** @var ContainerInterface */
    protected $container;

While AbstractController::$container is:

    protected ContainerInterface $container;

This leads to the incompatible composition.

This PR would fix that, but I'm not sure which versions of symfony/framework-bundle and symfony/contracts to target. Maybe we should add a conflict against symfony/framework-bundle < 7.0 to symfony/contract's composer.json.

@derrabus
Copy link
Member

derrabus commented Apr 4, 2024

Your change would be a breaking change for other users of the trait. I'm afraid, we cannot merge it.

@pableu
Copy link
Contributor Author

pableu commented Apr 4, 2024

I see. Can we.. tag a higher version maybe? How is versioning and the bc-promise done for symfony/contracts?

My issue appeared when upgrading from 6.4 to 7.0. I can easily work around it, but I thought an easier upgrade and more type safety in the trait might be nice.

@nicolas-grekas
Copy link
Member

That's quite unfortunate!
I think we can do it more smoothly by deprecating the trait and providing another one that declares the type of the property.
See #54496
Thanks for sharing this concern and proposing a fix.

@pableu
Copy link
Contributor Author

pableu commented Apr 5, 2024

Oh lovely @nicolas-grekas, thanks for your much more complete solution in #54496, that looks great.

@pableu pableu deleted the patch-2 branch April 5, 2024 10:52
fabpot added a commit that referenced this pull request Apr 5, 2024
…thodsSubscriberTrait (nicolas-grekas)

This PR was merged into the 7.1 branch.

Discussion
----------

[Contracts] Rename ServiceSubscriberTrait to ServiceMethodsSubscriberTrait

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Issues        | Fix #54490
| License       | MIT

As described in the linked PR, AbstractController is incompatible with ServiceSubscriberTrait because of the added type to the AbstractController::$container property, while ServiceSubscriberTrait's $container property cannot have a type without a BC break.

There are two parts to this PR:
- Deprecate ServiceSubscriberTrait in favor if ServiceMethodsSubscriberTrait, which declares the type of the $container property. The new name better conveys its purpose as a bonus.
- Fix the incompatibility with AbstractController by removing the property declaration on ServiceSubscriberTrait. This means the
 trait will create a dynamic property. Those are deprecated, but since the trait is also deprecated, the upgrade path is clear.

I also tried to improve the description of the trait in the meantime.

/cc `@kbond`

Commits
-------

8f47ced [Contracts] Rename ServiceSubscriberTrait to ServiceMethodsSubscriberTrait
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.

4 participants