Skip to content

Commit

Permalink
minor #35141 [Console][FormatterHelper] Use helper strlen statically …
Browse files Browse the repository at this point in the history
…and remove duplicated code (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console][FormatterHelper] Use helper strlen statically and remove duplicated code

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

All those helpers methods are static and are accessed with `self::` everywhere else. They are not an extension point.

Commits
-------

f0d227d [Console][FormatterHelper] Use helper strlen statically and remove duplicated code
  • Loading branch information
nicolas-grekas committed Dec 31, 2019
2 parents 7aba9cf + f0d227d commit c976752
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/Symfony/Component/Console/Helper/FormatterHelper.php
Expand Up @@ -54,12 +54,12 @@ public function formatBlock($messages, $style, $large = false)
foreach ($messages as $message) {
$message = OutputFormatter::escape($message);
$lines[] = sprintf($large ? ' %s ' : ' %s ', $message);
$len = max($this->strlen($message) + ($large ? 4 : 2), $len);
$len = max(self::strlen($message) + ($large ? 4 : 2), $len);
}

$messages = $large ? [str_repeat(' ', $len)] : [];
for ($i = 0; isset($lines[$i]); ++$i) {
$messages[] = $lines[$i].str_repeat(' ', $len - $this->strlen($lines[$i]));
$messages[] = $lines[$i].str_repeat(' ', $len - self::strlen($lines[$i]));
}
if ($large) {
$messages[] = str_repeat(' ', $len);
Expand All @@ -83,17 +83,13 @@ public function formatBlock($messages, $style, $large = false)
*/
public function truncate($message, $length, $suffix = '...')
{
$computedLength = $length - $this->strlen($suffix);
$computedLength = $length - self::strlen($suffix);

if ($computedLength > $this->strlen($message)) {
if ($computedLength > self::strlen($message)) {
return $message;
}

if (false === $encoding = mb_detect_encoding($message, null, true)) {
return substr($message, 0, $length).$suffix;
}

return mb_substr($message, 0, $length, $encoding).$suffix;
return self::substr($message, 0, $length).$suffix;
}

/**
Expand Down

0 comments on commit c976752

Please sign in to comment.