-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[RFC] Allow for an integer $type on ControllerTrait::addFlash() and FlashBagInterface::add() #27520
Comments
Changing the interface would be a bc break. See http://symfony.com/doc/current/contributing/code/bc.html To me it does not seem worth to create a bc break for this. |
@Tobion , it is not a BC as the More, the change is not code related, but only affects the DocBlock: see the code I posted. |
It is typehinted to string since 4.0, so typehint can't really be taken away now. |
Ok, the problem is this: we have a helper method in the Considering that the The fact that it was type hinted in the 4.0 version is irrelevant: if there is an error, there is an error, and it is not relevant when the error was introduced: it has to be removed. |
The method is final so it's ok changing it btw. |
It's a bc break because implementations of |
it can be just casted to string before calling add() |
@Tobion , forgive me if I insist: the BC policy involves only the methods that are directly used by the end developer. It doesn't involve the internals. |
@ostrolucky , yes, for sure: but this is "dirty". The real point I miss is why the helper has to introduce a restriction that the real method More, the type casting forces me to use |
my comment was directed to @Tobion |
@Tobion , more about internal implementation: they are simple arrays: the
These are the only two concrete implementations. Anyway, I repeat: |
It's pretty common to put objects in flash messages, like a data container for a delayed translation. I've written this code before (and I know it breaks the docblock), but it's a very clean solution. I would prefer that the interface would accept "mixed" instead. |
@iltar , are you speaking about the second param, |
@Aerendir correct |
Rereading this, I understand that you're using a number as the name of a flash message? |
@nicolas-grekas , I think the messages should be standardized in the team: a scenario like the one you are describing is caused by a poor development habit... The same thing may happen also using strings with the exact same probability... Anyway, as I already told, there is anyway an inconsistency: one method put the restriction (the one in the trait) while another doesn't (the one in the Session): there are only two alternatives: both of them put restrict to strings or both of them don't. But having one method that restricts and another that doesn't for me is an inconsistency. |
Anyway, just as a side note, I switched from But remains the inconsistency for which a clear decision should be taken, IMHO. |
There is no inconsistency, the And changing it would be a BC break because it would means other implementations of |
As "inconsistency" I meant there is no type hinting in the concrete implementations... While there is for the helper method of You are right: the interface hints for But the concrete implementations don't type hint for them: this is the "inconsistency". Without static code analysis, someone may pass an |
The lack of type hinting comes from the fact that adding them now would break extending classes for people using PHP < 7.2 so we can't add them now. And the concrete implementations does hint for |
And as a side note: I don't understand what do you mean by "BC". If until now you had to deal with only Different discourse is if you make possible to pass also, for example, But in this specific case I don't see any BC. |
The BC is for other implementations, extending these interfaces/classes: adding the type would break them. What you describe as inconsistent will be fixable with Symfony 5, when PHP will be bumped to 7.2 or higher. For now, we can't do anything I believe. |
Ok @nicolas-grekas , then this issue can be closed if reputed not relevant. I solved my specific use case, so it is all about Symfony: if you think this is something should be fixed or not... Or if the type hinting has to be added to the I made you aware that there are methods type hinted and methods that aren't: the choice is yours. Just for reference:
/**
* Adds a flash message for type.
*
* @param string $type
* @param string $message
*/
public function add($type, $message);
/**
* {@inheritdoc}
*/
public function add($type, $message)
{
$this->flashes[$type][] = $message;
}
/**
* {@inheritdoc}
*/
public function add($type, $message)
{
$this->flashes[$type][] = $message;
}
/**
* Adds a flash message to the current session for type.
*
* @throws \LogicException
*
* @final
*/
protected function addFlash(string $type, string $message)
{
if (!$this->container->has('session')) {
throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".');
}
$this->container->get('session')->getFlashBag()->add($type, $message);
} |
The difference comes from the fact that the |
@jvasseur , excuse me again if I insist again: I'm only trying to understand as much as I can... The And from what I read, the type declaration (alias type hinting) was added in PHP 7.0. So, what is the relation between the PHP version and the difference highlighted? |
With PHP < 7.2 when you add type declaration to a method argument it means all extending classes have to use the same type declaration, with PHP 7.2 the extending class can remove the type declaration (https://wiki.php.net/rfc/parameter-no-type-variance, https://3v4l.org/f26BQ). This means you can add type declarations to classes/interfaces without breaking extending classes. |
Yes, it is true! I forgot this particular! You are right, I’m wrong. Thank you for your immense patience :) |
Currently both
ControllerTrait::addFlash()
andFlashBagInterface::add()
have astring
type hint.This is not useful to set messages.
I found myself using Monolog
Logger
constants to set the type of messages:But using
declare(strict_types=1)
cause this to be considered an error (correctly).I suggest remove the method type hint and add a check inside the
ControllerTrait::addFlash()
method:According to this, also the
FlashBagInterface::add()
DocComment has to be updated:The classes that implement
FlashBagInterface
don't have any method type hint and inherit the DocBlock fromFlashBagInterface
, so no changes are required.CONCRETE PROBLEM
This type hint causes concrete problems.
In fact I could bypass the type hinting forcing using the typecasting of the constants provided by
Logger
:But this cause problems in Twig:
The text was updated successfully, but these errors were encountered: