Skip to content

Commit

Permalink
style: run code inspect for src/Component
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 29, 2021
1 parent 001ca21 commit cc52396
Show file tree
Hide file tree
Showing 27 changed files with 154 additions and 168 deletions.
4 changes: 2 additions & 2 deletions src/Component/CompletionDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class CompletionDumper extends Obj\AbstractObj
{
/**
* @var Application
* @var Application|null
*/
public $app;
public ?Application $app;
}
4 changes: 2 additions & 2 deletions src/Component/ConsoleAppIShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
class ConsoleAppIShell extends IShell
{
/**
* @var Application
* @var Application|null
*/
public $app;
public ?Application $app;

// TODO start an shell for run app.
}
4 changes: 2 additions & 2 deletions src/Component/ConsoleRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
class ConsoleRenderer
{
/**
* @var FormatterInterface
* @var FormatterInterface|null
*/
private $formatter;
private ?FormatterInterface $formatter;

/**
* @return string
Expand Down
6 changes: 3 additions & 3 deletions src/Component/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ class ErrorHandler implements ErrorHandlerInterface
/**
* @var bool
*/
protected $debug = false;
protected bool $debug = false;

/**
* @var string
*/
protected $rootPath = '';
protected string $rootPath = '';

/**
* @var bool
*/
protected $hideRootPath = false;
protected bool $hideRootPath = false;

/**
* @param array $config
Expand Down
8 changes: 4 additions & 4 deletions src/Component/Formatter/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
class Body
{
public $nodes = [];
public array $nodes = [];

public $style = '';
public string $style = '';

public $keyStyle = 'info';
public string $keyStyle = 'info';

public $valStyle = '';
public string $valStyle = '';
}
1 change: 0 additions & 1 deletion src/Component/Formatter/JSONPretty.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function str_contains;
use function str_ends_with;
use function trim;
use function vdump;

/**
* class JSONPretty
Expand Down
37 changes: 18 additions & 19 deletions src/Component/Formatter/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,48 @@
class Panel extends MessageFormatter
{
/** @var string */
public $title = '';
public string $title = '';

/** @var string */
public $titleBorder = '-';
public string $titleBorder = '-';

/** @var string */
public $titleStyle = 'bold';
public string $titleStyle = 'bold';

/** @var string */
public $titleAlign = self::ALIGN_LEFT;
public string $titleAlign = self::ALIGN_LEFT;

/** @var string|array */
public $data;
public string|array $data;

/** @var string */
public $bodyAlign = self::ALIGN_LEFT;
public string $bodyAlign = self::ALIGN_LEFT;

/** @var string */
public $footerBorder = '-';
public string $footerBorder = '-';

/** @var string */
public $footer = '';
public string $footer = '';

/** @var bool */
public $ucFirst = true;
public bool $ucFirst = true;

/** @var int */
public $width = 0;
public int $width = 0;

/** @var bool */
public $showBorder = true;
public bool $showBorder = true;

/** @var string */
public $borderYChar = '-';
public string $borderYChar = '-';

/** @var string */
public $borderXChar = '|';
public string $borderXChar = '|';

/**
* @var string Template for the panel. don't contains border
*/
public $template = <<<EOF
public string $template = <<<EOF
{%title%}
{%title-border%}
{%content%}
Expand All @@ -93,7 +93,7 @@ class Panel extends MessageFormatter
*
* @return int
*/
public static function show($data, string $title = 'Information Panel', array $opts = []): int
public static function show(mixed $data, string $title = 'Information Panel', array $opts = []): int
{
if (!$data) {
Console::write('<info>No data to display!</info>');
Expand Down Expand Up @@ -164,7 +164,7 @@ public static function show($data, string $title = 'Information Panel', array $o
$titleLength = mb_strlen($title, 'UTF-8');
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
$indentSpace = Str::pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
Console::write(" {$indentSpace}<bold>{$title}</bold>");
Console::write(" $indentSpace<bold>$title</bold>");
}

// output panel top border
Expand Down Expand Up @@ -226,7 +226,6 @@ public function format(): string
if (is_array($value)) {
$temp = '';

/** @var array $value */
foreach ($value as $key => $val) {
if (is_bool($val)) {
$val = $val ? 'True' : 'False';
Expand Down Expand Up @@ -262,7 +261,7 @@ public function format(): string
$titleLength = mb_strlen($title, 'UTF-8');
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
$indentSpace = Str::pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
$buffer->write(" {$indentSpace}<bold>{$title}</bold>\n");
$buffer->write(" $indentSpace<bold>$title</bold>\n");
}

// output panel top border
Expand Down Expand Up @@ -297,7 +296,7 @@ public function format(): string
*
* @return $this
*/
public function showBorder($border): self
public function showBorder(bool $border): self
{
$this->showBorder = (bool)$border;
return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Formatter/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class Section extends MessageFormatter
{
/**
* @param string $title The title text
* @param string|array $body The section body message
* @param array|string $body The section body message
* @param array $opts
*/
public static function show(string $title, $body, array $opts = []): void
public static function show(string $title, array|string $body, array $opts = []): void
{
$opts = array_merge([
'width' => 80,
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Formatter/SingleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SingleList extends MessageFormatter
*
* @return int|string
*/
public static function show($data, string $title = 'Information', array $opts = [])
public static function show(mixed $data, string $title = 'Information', array $opts = []): int|string
{
$string = '';
$opts = array_merge([
Expand Down
22 changes: 11 additions & 11 deletions src/Component/Formatter/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@
class Table extends MessageFormatter
{
/** @var array */
public $data = [];
public array $data = [];

/** @var array */
public $columns = [];
public array $columns = [];

/** @var string|array */
public $body;
public string|array $body;

/** @var string */
public $title = '';
public string $title = '';

/** @var string */
public $titleBorder = '-';
public string $titleBorder = '-';

/** @var string */
public $titleStyle = '-';
public string $titleStyle = '-';

/** @var string */
public $titleAlign = self::ALIGN_LEFT;
public string $titleAlign = self::ALIGN_LEFT;

/**
* Tabular data display
Expand Down Expand Up @@ -169,7 +169,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
$title = Str::ucwords(trim($title));
$titleLength = Str::utf8Len($title, 'UTF-8');
$indentSpace = Str::pad(' ', ceil($tableWidth / 2) - ceil($titleLength / 2) + ($columnCount * 2), ' ');
$buf->write(" {$indentSpace}<$tStyle>{$title}</$tStyle>\n");
$buf->write(" $indentSpace<$tStyle>$title</$tStyle>\n");
}

$border = $leftIndent . Str::pad($rowBorderChar, $tableWidth + ($columnCount * 3) + 2, $rowBorderChar);
Expand All @@ -183,7 +183,7 @@ public static function show(array $data, string $title = 'Data Table', array $op

// output table head
if ($hasHead) {
$headStr = "{$leftIndent}{$colBorderChar} ";
$headStr = "$leftIndent$colBorderChar ";

foreach ($head as $index => $name) {
$colMaxWidth = $info['columnMaxWidth'][$index];
Expand All @@ -193,7 +193,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
$name = Str::padByWidth($name, $colMaxWidth, ' ');
$name = ColorTag::wrap($name, $opts['headStyle']);
// join string
$headStr .= " {$name} {$colBorderChar}";
$headStr .= " $name $colBorderChar";
}

$buf->write($headStr . "\n");
Expand Down Expand Up @@ -227,7 +227,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
// use Str::padByWidth support zh-CN words
$value = Str::padByWidth($value, $colMaxWidth, ' ');
$value = ColorTag::wrap($value, $opts['bodyStyle']);
$rowStr .= " {$value} {$colBorderChar}";
$rowStr .= " $value $colBorderChar";
$colIndex++;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Component/Formatter/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

use Inhere\Console\Component\MessageFormatter;
use Inhere\Console\Console;
use Inhere\Console\Util\FormatUtil;
use Toolkit\Cli\Cli;
use Toolkit\Stdlib\Std;
use function array_merge;
use function is_array;
use function is_scalar;
Expand All @@ -26,10 +26,10 @@
class Tree extends MessageFormatter
{
/** @var int */
private $counter = 0;
private int $counter = 0;

/** @var bool */
private $started = false;
private bool $started = false;

/**
* Render data like tree
Expand Down Expand Up @@ -64,7 +64,7 @@ public static function show(array $data, array $opts = []): void
$counter++;
$leftString = $opts['leftPadding'] . str_pad($opts['prefix'], $opts['_level'] + 1, $opts['char']);

Console::write($leftString . ' ' . FormatUtil::typeToString($value));
Console::write($leftString . ' ' . Std::toString($value));
} elseif (is_array($value)) {
$newOpts = $opts;
$newOpts['_is_main'] = false;
Expand Down
12 changes: 6 additions & 6 deletions src/Component/Interact/AbstractQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Inhere\Console\Component\Interact;

use Inhere\Console\Component\InteractiveHandle;
use Toolkit\Stdlib\Str\StrObject;
use Toolkit\Stdlib\Str\StrValue;

/**
* Class AbstractQuestion
Expand All @@ -20,22 +20,22 @@
abstract class AbstractQuestion extends InteractiveHandle
{
/**
* @var StrObject
* @var StrValue|null
*/
protected $answer;
protected ?StrValue $answer = null;

/**
* @param string $str
*/
protected function createAnswer(string $str): void
{
$this->answer = StrObject::new($str)->trim();
$this->answer = StrValue::new($str)->trim();
}

/**
* @return StrObject
* @return StrValue
*/
public function getAnswer(): StrObject
public function getAnswer(): StrValue
{
return $this->answer;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Interact/AbstractSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ abstract class AbstractSelect extends InteractiveHandle
/**
* @var array
*/
protected $data = [];
protected array $data = [];

/**
* @var bool
*/
protected $allowExit = true;
protected bool $allowExit = true;

/**
* @return bool
Expand Down
12 changes: 6 additions & 6 deletions src/Component/Interact/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class Checkbox extends MultiSelect
* List multiple options and allow multiple selections
*
* @param string $description
* @param string|array $options
* @param null|mixed $default
* @param array|string $options
* @param mixed|null $default
* @param bool $allowExit
*
* @return array
*/
public static function select(string $description, $options, $default = null, bool $allowExit = true): array
public static function select(string $description, array|string $options, mixed $default = null, bool $allowExit = true): array
{
if (!$description = trim($description)) {
Show::error('Please provide a description text!', 1);
Expand All @@ -59,13 +59,13 @@ public static function select(string $description, $options, $default = null, bo
}

Console::write($text);
$defText = $default !== null ? "[default:<comment>{$default}</comment>]" : '';
$filter = function ($val) use ($options) {
$defText = $default !== null ? "[default:<comment>$default</comment>]" : '';
$filter = static function ($val) use ($options) {
return $val !== 'q' && isset($options[$val]);
};

beginChoice:
$r = Console::readln("Your choice{$defText} : ");
$r = Console::readln("Your choice$defText : ");
$r = $r !== '' ? str_replace(' ', '', trim($r, $sep)) : '';

// empty
Expand Down
Loading

0 comments on commit cc52396

Please sign in to comment.