Skip to content

Commit f9f00b3

Browse files
committed
fix some strict error for formatter
1 parent 158755a commit f9f00b3

File tree

11 files changed

+168
-71
lines changed

11 files changed

+168
-71
lines changed

src/Component/Formatter/Padding.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Inhere\Console\Console;
77
use Inhere\Console\Util\Helper;
88
use Toolkit\Cli\ColorTag;
9+
use Toolkit\Stdlib\Str;
910
use function array_merge;
10-
use function str_pad;
1111
use function trim;
1212
use function ucfirst;
1313

@@ -50,7 +50,7 @@ public static function show(array $data, string $title = '', array $opts = []):
5050

5151
foreach ($data as $label => $value) {
5252
$value = ColorTag::wrap((string)$value, $opts['valueStyle']);
53-
$string .= $opts['indent'] . str_pad((string)$label, $paddingLen, $opts['char']) . " $value\n";
53+
$string .= $opts['indent'] . Str::pad($label, $paddingLen, $opts['char']) . " $value\n";
5454
}
5555

5656
Console::write(trim($string));

src/Component/Formatter/Panel.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Inhere\Console\Component\MessageFormatter;
1212
use Inhere\Console\Console;
1313
use Inhere\Console\Util\FormatUtil;
14+
use Toolkit\Stdlib\Str;
1415
use Toolkit\Stdlib\Str\StrBuffer;
1516
use function array_filter;
1617
use function array_merge;
@@ -20,7 +21,6 @@
2021
use function is_numeric;
2122
use function mb_strlen;
2223
use function rtrim;
23-
use function str_pad;
2424
use function strip_tags;
2525
use function trim;
2626
use function ucwords;
@@ -117,7 +117,8 @@ public static function show($data, string $title = 'Information Panel', array $o
117117
foreach ($data as $label => $value) {
118118
// label exists
119119
if (!is_numeric($label)) {
120-
$width = mb_strlen($label, 'UTF-8');
120+
$width = Str::len2($label, 'UTF-8');
121+
121122
$labelMaxWidth = $width > $labelMaxWidth ? $width : $labelMaxWidth;
122123
}
123124

@@ -158,16 +159,17 @@ public static function show($data, string $title = 'Information Panel', array $o
158159

159160
// output title
160161
if ($title) {
161-
$title = ucwords($title);
162+
$title = ucwords($title);
163+
162164
$titleLength = mb_strlen($title, 'UTF-8');
163165
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
164-
$indentSpace = str_pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
166+
$indentSpace = Str::pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
165167
Console::write(" {$indentSpace}<bold>{$title}</bold>");
166168
}
167169

168170
// output panel top border
169171
if ($borderChar) {
170-
$border = str_pad($borderChar, $panelWidth + (3 * 3), $borderChar);
172+
$border = Str::pad($borderChar, $panelWidth + (3 * 3), $borderChar);
171173
Console::write(' ' . $border);
172174
}
173175

@@ -215,7 +217,8 @@ public function format(): string
215217
foreach ($data as $label => $value) {
216218
// label exists
217219
if (!is_numeric($label)) {
218-
$width = mb_strlen($label, 'UTF-8');
220+
$width = Str::len2($label, 'UTF-8');
221+
219222
$labelMaxWidth = $width > $labelMaxWidth ? $width : $labelMaxWidth;
220223
}
221224

@@ -257,13 +260,13 @@ public function format(): string
257260
$title = ucwords($title);
258261
$titleLength = mb_strlen($title, 'UTF-8');
259262
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
260-
$indentSpace = str_pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
263+
$indentSpace = Str::pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
261264
$buffer->write(" {$indentSpace}<bold>{$title}</bold>\n");
262265
}
263266

264267
// output panel top border
265268
if ($topBorder = $this->titleBorder) {
266-
$border = str_pad($topBorder, $panelWidth + (3 * 3), $topBorder);
269+
$border = Str::pad($topBorder, $panelWidth + (3 * 3), $topBorder);
267270
$buffer->write(' ' . $border . PHP_EOL);
268271
}
269272

@@ -280,7 +283,7 @@ public function format(): string
280283

281284
// output panel bottom border
282285
if ($footBorder = $this->footerBorder) {
283-
$border = str_pad($footBorder, $panelWidth + (3 * 3), $footBorder);
286+
$border = Str::pad($footBorder, $panelWidth + (3 * 3), $footBorder);
284287
$buffer->write(' ' . $border . PHP_EOL);
285288
}
286289

src/Component/Formatter/Section.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use function ceil;
1111
use function implode;
1212
use function is_array;
13-
use function str_pad;
1413
use function trim;
1514
use function ucwords;
1615
use const PHP_EOL;
@@ -52,9 +51,9 @@ public static function show(string $title, $body, array $opts = []): void
5251
if ($tLength >= $width) {
5352
$titleIndent = Str::pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
5453
} elseif ($opts['titlePos'] === self::POS_RIGHT) {
55-
$titleIndent = str_pad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
54+
$titleIndent = Str::pad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
5655
} elseif ($opts['titlePos'] === self::POS_MIDDLE) {
57-
$titleIndent = str_pad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
56+
$titleIndent = Str::pad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
5857
} else {
5958
$titleIndent = Str::pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
6059
}
@@ -67,7 +66,7 @@ public static function show(string $title, $body, array $opts = []): void
6766
$showBBorder = (bool)$opts['bottomBorder'];
6867

6968
if ($showTBorder || $showBBorder) {
70-
$border = str_pad($char, $width, $char);
69+
$border = Str::pad($char, $width, $char);
7170

7271
if ($showTBorder) {
7372
$topBorder = "{$indentStr}$border\n";

src/Component/Formatter/Table.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Inhere\Console\Component\MessageFormatter;
1212
use Inhere\Console\Console;
1313
use Toolkit\Cli\ColorTag;
14+
use Toolkit\Stdlib\Str;
1415
use Toolkit\Stdlib\Str\StrBuffer;
1516
use function array_keys;
1617
use function array_merge;
@@ -19,7 +20,6 @@
1920
use function count;
2021
use function is_string;
2122
use function mb_strlen;
22-
use function str_pad;
2323
use function ucwords;
2424

2525
/**
@@ -164,11 +164,11 @@ public static function show(array $data, string $title = 'Data Table', array $op
164164
$tStyle = $opts['titleStyle'] ?: 'bold';
165165
$title = ucwords(trim($title));
166166
$titleLength = mb_strlen($title, 'UTF-8');
167-
$indentSpace = str_pad(' ', ceil($tableWidth / 2) - ceil($titleLength / 2) + ($columnCount * 2), ' ');
167+
$indentSpace = Str::pad(' ', ceil($tableWidth / 2) - ceil($titleLength / 2) + ($columnCount * 2), ' ');
168168
$buf->write(" {$indentSpace}<$tStyle>{$title}</$tStyle>\n");
169169
}
170170

171-
$border = $leftIndent . str_pad($rowBorderChar, $tableWidth + ($columnCount * 3) + 2, $rowBorderChar);
171+
$border = $leftIndent . Str::pad($rowBorderChar, $tableWidth + ($columnCount * 3) + 2, $rowBorderChar);
172172

173173
// output table top border
174174
if ($showBorder) {
@@ -184,7 +184,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
184184
foreach ($head as $index => $name) {
185185
$colMaxWidth = $info['columnMaxWidth'][$index];
186186
// format
187-
$name = str_pad($name, $colMaxWidth, ' ');
187+
$name = Str::pad($name, $colMaxWidth, ' ');
188188
$name = ColorTag::wrap($name, $opts['headStyle']);
189189
$headStr .= " {$name} {$colBorderChar}";
190190
}
@@ -193,7 +193,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
193193

194194
// head border: split head and body
195195
if ($headBorderChar = $opts['headBorderChar']) {
196-
$headBorder = $leftIndent . str_pad(
196+
$headBorder = $leftIndent . Str::pad(
197197
$headBorderChar,
198198
$tableWidth + ($columnCount * 3) + 2,
199199
$headBorderChar
@@ -212,7 +212,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
212212
foreach ((array)$row as $value) {
213213
$colMaxWidth = $info['columnMaxWidth'][$colIndex];
214214
// format
215-
$value = str_pad($value, $colMaxWidth, ' ');
215+
$value = Str::pad($value, $colMaxWidth, ' ');
216216
$value = ColorTag::wrap($value, $opts['bodyStyle']);
217217
$rowStr .= " {$value} {$colBorderChar}";
218218
$colIndex++;

src/Component/Formatter/Title.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Toolkit\Sys\Sys;
99
use function array_merge;
1010
use function ceil;
11-
use function str_pad;
1211

1312
/**
1413
* Class Title
@@ -63,7 +62,7 @@ public static function show(string $title, array $opts = []): void
6362
}
6463

6564
$titleLine = "$titleIndent<bold>$title</bold>\n";
66-
$border = $indentStr . str_pad($char, $width, $char);
65+
$border = $indentStr . Str::pad($char, $width, $char);
6766

6867
Console::write($titleLine . $border);
6968
}

src/IO/Input.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\IO;
1010

11+
use Toolkit\Cli\Cli;
1112
use Toolkit\Cli\Flags;
1213
use function array_map;
1314
use function array_shift;
@@ -17,8 +18,6 @@
1718
use function implode;
1819
use function preg_match;
1920
use function trim;
20-
use const STDIN;
21-
use const STDOUT;
2221

2322
/**
2423
* Class Input - The input information. by parse global var $argv.
@@ -36,9 +35,11 @@ class Input extends AbstractInput
3635
protected $commandId = '';
3736

3837
/**
38+
* Default is STDIN
39+
*
3940
* @var resource
4041
*/
41-
protected $inputStream = STDIN;
42+
protected $inputStream;
4243

4344
/**
4445
* Input constructor.
@@ -52,6 +53,7 @@ public function __construct(array $args = null, bool $parsing = true)
5253
$args = $_SERVER['argv'];
5354
}
5455

56+
$this->inputStream = Cli::getInputStream();
5557
$this->collectInfo($args);
5658

5759
if ($parsing) {
@@ -91,6 +93,11 @@ protected function doParse(array $args): void
9193
$this->findCommand();
9294
}
9395

96+
public function resetInputStream(): void
97+
{
98+
$this->inputStream = Cli::getInputStream();
99+
}
100+
94101
/**
95102
* @return string
96103
*/
@@ -130,7 +137,7 @@ protected function tokenEscape(string $token): string
130137
public function read(string $question = '', bool $nl = false): string
131138
{
132139
if ($question) {
133-
fwrite(STDOUT, $question . ($nl ? "\n" : ''));
140+
fwrite(Cli::getOutputStream(), $question . ($nl ? "\n" : ''));
134141
}
135142

136143
return trim(fgets($this->inputStream));

src/IO/Input/ArrayInput.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
use Inhere\Console\IO\Input;
1212
use Toolkit\Cli\Flags;
13-
use function array_shift;
14-
use function implode;
1513

1614
/**
1715
* Class ArrayInput
@@ -31,7 +29,7 @@ public function __construct(array $args = null, bool $parsing = true)
3129
parent::__construct([], false);
3230

3331
if ($parsing && $args) {
34-
$this->doParse($this->flags);
32+
$this->doParse($this->flags);
3533
}
3634
}
3735

src/IO/Output.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ class Output implements \Inhere\Console\Contract\OutputInterface
2525
use FormatOutputAwareTrait;
2626

2727
/**
28-
* Normal output stream
28+
* Normal output stream. Default is STDOUT
2929
*
3030
* @var resource
3131
*/
32-
protected $outputStream = STDOUT;
32+
protected $outputStream;
3333

3434
/**
35-
* Error output stream
35+
* Error output stream. Default is STDERR
3636
*
3737
* @var resource
3838
*/
39-
protected $errorStream = STDERR;
39+
protected $errorStream;
4040

4141
/**
4242
* 控制台窗口(字体/背景)颜色添加处理
@@ -55,11 +55,18 @@ public function __construct($outputStream = null)
5555
{
5656
if ($outputStream) {
5757
$this->outputStream = $outputStream;
58+
} else {
59+
$this->outputStream = Cli::getOutputStream();
5860
}
5961

6062
$this->getStyle();
6163
}
6264

65+
public function resetOutputStream(): void
66+
{
67+
$this->outputStream = Cli::getOutputStream();
68+
}
69+
6370
/***************************************************************************
6471
* Output buffer
6572
***************************************************************************/

src/IO/Output/BufferOutput.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inhere\Console\IO\Output;
44

55
use Inhere\Console\IO\Output;
6+
use function fopen;
67

78
/**
89
* Class BufferOutput
@@ -11,23 +12,13 @@
1112
*/
1213
class BufferOutput extends Output
1314
{
14-
/**
15-
* @var array
16-
*/
17-
private $buffer = [];
18-
19-
/**
20-
* @param $messages
21-
* @param bool $nl
22-
* @param bool $quit
23-
* @param array $opts
24-
*
25-
* @return int
26-
*/
27-
public function write($messages, $nl = true, $quit = false, array $opts = []): int
15+
public function __construct()
2816
{
29-
$this->buffer[] = $messages;
17+
parent::__construct(fopen('php://memory', 'rwb'));
18+
}
3019

31-
return 0;
20+
public function getBuffer(): string
21+
{
22+
return '';
3223
}
3324
}

0 commit comments

Comments
 (0)