Skip to content

Commit b3457a8

Browse files
authored
PHP 8.2: strwidth() & Colors::pad()/decolorize() should always work with a string (#163)
1 parent d788a2c commit b3457a8

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/cli/Colors.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ static public function color($color) {
108108
* Colorize a string using helpful string formatters. If the `Streams::$out` points to a TTY coloring will be enabled,
109109
* otherwise disabled. You can control this check with the `$colored` parameter.
110110
*
111-
* @param string $string
111+
* @param string $string
112112
* @param boolean $colored Force enable or disable the colorized output. If left as `null` the TTY will control coloring.
113-
* @return string
113+
* @return string
114114
*/
115115
static public function colorize($string, $colored = null) {
116116
$passed = $string;
@@ -146,6 +146,8 @@ static public function colorize($string, $colored = null) {
146146
* @return string A string with color information removed.
147147
*/
148148
static public function decolorize( $string, $keep = 0 ) {
149+
$string = (string) $string;
150+
149151
if ( ! ( $keep & 1 ) ) {
150152
// Get rid of color tokens if they exist
151153
$string = str_replace('%%', '', $string);
@@ -182,7 +184,7 @@ static public function cacheString( $passed, $colorized, $deprecated = null ) {
182184
* Return the length of the string without color codes.
183185
*
184186
* @param string $string the string to measure
185-
* @return int
187+
* @return int
186188
*/
187189
static public function length($string) {
188190
return safe_strlen( self::decolorize( $string ) );
@@ -194,7 +196,7 @@ static public function length($string) {
194196
* @param string $string The string to measure.
195197
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
196198
* @param string|bool $encoding Optional. The encoding of the string. Default false.
197-
* @return int
199+
* @return int
198200
*/
199201
static public function width( $string, $pre_colorized = false, $encoding = false ) {
200202
return strwidth( $pre_colorized || self::shouldColorize() ? self::decolorize( $string, $pre_colorized ? 1 /*keep_tokens*/ : 0 ) : $string, $encoding );
@@ -208,9 +210,11 @@ static public function width( $string, $pre_colorized = false, $encoding = false
208210
* @param bool $pre_colorized Optional. Set if the string is pre-colorized. Default false.
209211
* @param string|bool $encoding Optional. The encoding of the string. Default false.
210212
* @param int $pad_type Optional. Can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
211-
* @return string
213+
* @return string
212214
*/
213215
static public function pad( $string, $length, $pre_colorized = false, $encoding = false, $pad_type = STR_PAD_RIGHT ) {
216+
$string = (string) $string;
217+
214218
$real_length = self::width( $string, $pre_colorized, $encoding );
215219
$diff = strlen( $string ) - $real_length;
216220
$length += $diff;

lib/cli/cli.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ function safe_str_pad( $string, $length, $encoding = false ) {
319319
* @return int The string's width.
320320
*/
321321
function strwidth( $string, $encoding = false ) {
322+
$string = (string) $string;
323+
322324
// Set the East Asian Width and Mark regexs.
323325
list( $eaw_regex, $m_regex ) = get_unicode_regexs();
324326

0 commit comments

Comments
 (0)