Skip to content

Commit 2f1c28b

Browse files
committed
minor symfony#22683 [Console] Do not duplicate Helper::strlen() code (ogizanagi)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Do not duplicate Helper::strlen() code | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A Commits ------- 01c2c09 [Console] Do not duplicate Helper::strlen() code
2 parents 2d72b0b + 01c2c09 commit 2f1c28b

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Descriptor\XmlDescriptor;
1616
use Symfony\Component\Console\Formatter\OutputFormatter;
1717
use Symfony\Component\Console\Helper\DebugFormatterHelper;
18+
use Symfony\Component\Console\Helper\Helper;
1819
use Symfony\Component\Console\Helper\ProcessHelper;
1920
use Symfony\Component\Console\Helper\QuestionHelper;
2021
use Symfony\Component\Console\Input\InputInterface;
@@ -653,7 +654,7 @@ public function renderException($e, $output)
653654
do {
654655
$title = sprintf(' [%s] ', get_class($e));
655656

656-
$len = $this->stringWidth($title);
657+
$len = Helper::strlen($title);
657658

658659
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
659660
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
@@ -664,7 +665,7 @@ public function renderException($e, $output)
664665
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
665666
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
666667
// pre-format lines to get the right string length
667-
$lineLength = $this->stringWidth($line) + 4;
668+
$lineLength = Helper::strlen($line) + 4;
668669
$lines[] = array($line, $lineLength);
669670

670671
$len = max($lineLength, $len);
@@ -673,7 +674,7 @@ public function renderException($e, $output)
673674

674675
$messages = array();
675676
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
676-
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title))));
677+
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
677678
foreach ($lines as $line) {
678679
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
679680
}
@@ -1086,19 +1087,6 @@ public function setDefaultCommand($commandName)
10861087
$this->defaultCommand = $commandName;
10871088
}
10881089

1089-
private function stringWidth($string)
1090-
{
1091-
if (!function_exists('mb_strwidth')) {
1092-
return strlen($string);
1093-
}
1094-
1095-
if (false === $encoding = mb_detect_encoding($string, null, true)) {
1096-
return strlen($string);
1097-
}
1098-
1099-
return mb_strwidth($string, $encoding);
1100-
}
1101-
11021090
private function splitStringByWidth($string, $width)
11031091
{
11041092
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.

0 commit comments

Comments
 (0)