Skip to content

Commit

Permalink
minor #22683 [Console] Do not duplicate Helper::strlen() code (ogizan…
Browse files Browse the repository at this point in the history
…agi)

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
  • Loading branch information
fabpot committed May 10, 2017
2 parents 2d72b0b + 01c2c09 commit 2f1c28b
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\DebugFormatterHelper;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProcessHelper;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -653,7 +654,7 @@ public function renderException($e, $output)
do {
$title = sprintf(' [%s] ', get_class($e));

$len = $this->stringWidth($title);
$len = Helper::strlen($title);

$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
// 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
Expand All @@ -664,7 +665,7 @@ public function renderException($e, $output)
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
// pre-format lines to get the right string length
$lineLength = $this->stringWidth($line) + 4;
$lineLength = Helper::strlen($line) + 4;
$lines[] = array($line, $lineLength);

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

$messages = array();
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title))));
$messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
foreach ($lines as $line) {
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
}
Expand Down Expand Up @@ -1086,19 +1087,6 @@ public function setDefaultCommand($commandName)
$this->defaultCommand = $commandName;
}

private function stringWidth($string)
{
if (!function_exists('mb_strwidth')) {
return strlen($string);
}

if (false === $encoding = mb_detect_encoding($string, null, true)) {
return strlen($string);
}

return mb_strwidth($string, $encoding);
}

private function splitStringByWidth($string, $width)
{
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
Expand Down

0 comments on commit 2f1c28b

Please sign in to comment.