Skip to content

[Console] Don't pass null to preg_replace() #42381

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 4, 2021

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

An output formatter might return null (see NullOutputFormatter). In this case, we would pass null to preg_replace() which would trigger a deprecation warning in PHP 8.1. Let's not do that.

Signed-off-by: Alexander M. Turek <me@derrabus.de>
@Tobion
Copy link
Contributor

Tobion commented Aug 5, 2021

NullOutputFormatter has a void return type. Seems wrong to me. Shouldn't this be null?
The test for it added in https://github.com/symfony/symfony/pull/34869/files#diff-260058da84f48eedd4b4792b7719ebf98ebb4f5e274c80724b6620e5555cc218R24-R32 (NullOutputFormatterTest ::testFormat) does not make sense. The param is not passed by reference, so it cannot be changed. What it really means to test is that it returns null for any input message?!

@derrabus
Copy link
Member Author

derrabus commented Aug 5, 2021

NullOutputFormatter has a void return type. Seems wrong to me. Shouldn't this be null?

I believe so: #42382

@@ -170,7 +170,7 @@ public static function removeDecoration(OutputFormatterInterface $formatter, ?st
// remove <...> formatting
$string = $formatter->format($string ?? '');
// remove already formatted characters
$string = preg_replace("/\033\[[^m]*m/", '', $string);
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
Copy link
Contributor

Choose a reason for hiding this comment

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

why call preg_replace on an empty string?

Suggested change
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
$string = $string ? preg_replace("/\033\[[^m]*m/", '', $string) : '';

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, we can save the call here.

fabpot added a commit that referenced this pull request Aug 6, 2021
…mat() (derrabus)

This PR was merged into the 5.4 branch.

Discussion
----------

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

| 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.

Commits
-------

d75f5e6 Add return type to OutputFormatterInterface::format()
@fabpot
Copy link
Member

fabpot commented Aug 6, 2021

Thank you @derrabus.

@fabpot fabpot merged commit 2b0c65f into symfony:5.3 Aug 6, 2021
@derrabus derrabus deleted the bugfix/null-output-formatter branch August 6, 2021 09:54
@fabpot fabpot mentioned this pull request Aug 30, 2021
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