-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
Signed-off-by: Alexander M. Turek <me@derrabus.de>
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 ?? ''); |
There was a problem hiding this comment.
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?
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? ''); | |
$string = $string ? preg_replace("/\033\[[^m]*m/", '', $string) : ''; |
There was a problem hiding this comment.
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.
…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()
Thank you @derrabus. |
An output formatter might return
null
(seeNullOutputFormatter
). In this case, we would passnull
topreg_replace()
which would trigger a deprecation warning in PHP 8.1. Let's not do that.