Skip to content

[Console] Add return type to OutputFormatterInterface::format() #42382

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
merged 1 commit into from
Aug 6, 2021

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Aug 5, 2021

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets N/A
License MIT
Doc PR N/A

Spotted while working on #42381. If we look at how OutputFormatterInterface::format() is used in the codebase, we can see that an implementation of that method is supposed to return something. Yet the interface does not declare a return value and the NullOutputFormatter implementation even has a void return type which does not make sense at all, imho.

This PR attempts to fix that.

@Tobion
Copy link
Contributor

Tobion commented Aug 5, 2021

Could you also fix the test i mentioned in #42381 (comment) to make more sense please.

Signed-off-by: Alexander M. Turek <me@derrabus.de>
@derrabus derrabus force-pushed the bugfix/output-formatter-return branch from 9c75bbb to d75f5e6 Compare August 5, 2021 05:11
@derrabus
Copy link
Member Author

derrabus commented Aug 5, 2021

Done. I'm really scratching my head over this class. It clearly does not behave as the author of the test thought it would. 🤔

@@ -53,6 +53,8 @@ public function getStyle(string $name);

/**
* Formats a message according to the given styles.
*
* @return string|null
Copy link
Member

Choose a reason for hiding this comment

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

I think this must be @return string and the null formatter should be return $message; instead.

$message = $this->formatter->format($message);

Copy link
Member Author

Choose a reason for hiding this comment

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

Keep in mind that this is not an internal class. You are suggesting to significantly change its behavior here while I'm merely documenting the status quo.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, indeed. I'm not sure what is the best way forward.

If we officially document this contract as returning string|null, we have to update all code using this function to take care of the null return. Honestly, returning null (void actually) in NullOutputFormatter seems like a mistake and bug to me.

In fact, if code is build to use the normal OutputFormatter, it would be broken if NullOutputFormatter is used in the test. I can't think of any reasonable approach to build code to only work with NullOutputFormatter.

Copy link
Member Author

Choose a reason for hiding this comment

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

I understand. But string|null is what currently is returned from the implementations. Once this PR is merged, we can think about how we could narrow it down to just string in 6.0. By the way, I'd also like to do this for the $message parameter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we depreciate null in 5.4 then? Then we can keep the implied contract, which is string IMHO

Copy link
Member

Choose a reason for hiding this comment

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

I agree that the real issue is the implementation of NullOutputFormatter which was written before void was introduced. Conceptually, doing nothing was the same as returning null back then. Let's merge this one as is and see if we can deprecate null in a followup PR (same for *message).

@chalasr chalasr added this to the 5.4 milestone Aug 5, 2021
@fabpot
Copy link
Member

fabpot commented Aug 6, 2021

Thank you @derrabus.

@fabpot fabpot merged commit f041e03 into symfony:5.4 Aug 6, 2021
@derrabus derrabus deleted the bugfix/output-formatter-return branch August 6, 2021 09:49
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.

7 participants