Skip to content

Commit 45f0a1a

Browse files
committed
style: run code inspect for src/BuiltIn Util
1 parent 8195fbd commit 45f0a1a

9 files changed

+109
-218
lines changed

src/BuiltIn/DevServerCommand.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Inhere\Console\IO\Input;
1616
use Inhere\Console\IO\Output;
1717
use Inhere\Console\Util\PhpDevServe;
18-
use function strpos;
1918

2019
/**
2120
* Class DevServerCommand
@@ -24,9 +23,9 @@
2423
*/
2524
class DevServerCommand extends Command
2625
{
27-
protected static $name = 'dev:server';
26+
protected static string $name = 'dev:server';
2827

29-
protected static $description = 'Start a php built-in http server for development';
28+
protected static string $description = 'Start a php built-in http server for development';
3029

3130
public static function aliases(): array
3231
{
@@ -68,7 +67,7 @@ public function execute(Input $input, Output $output): void
6867
}
6968

7069
$port = $this->flags->getOpt('port');
71-
if ($port && strpos($serveAddr, ':') === false) {
70+
if ($port && !str_contains($serveAddr, ':')) {
7271
$serveAddr .= ':' . $port;
7372
}
7473

src/BuiltIn/PharController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
*/
3535
class PharController extends Controller
3636
{
37-
protected static $name = 'phar';
37+
protected static string $name = 'phar';
3838

39-
protected static $description = 'Pack a project directory to phar or unpack phar to directory';
39+
protected static string $description = 'Pack a project directory to phar or unpack phar to directory';
4040

4141
/**
4242
* @var Closure
4343
*/
44-
private $compilerConfiger;
44+
private Closure $compilerConfiger;
4545

4646
/**
4747
* @var string
4848
*/
49-
private $defPkgName;
49+
private string $defPkgName;
5050

5151
protected static function commandAliases(): array
5252
{

src/BuiltIn/SelfUpdateCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class SelfUpdateCommand extends Command
3535

3636
public const FILE_NAME = 'console.phar';
3737

38-
protected static $name = 'self-update';
38+
protected static string $name = 'self-update';
3939

40-
protected static $description = 'Update phar package to most recent stable, pre-release or development build.';
40+
protected static string $description = 'Update phar package to most recent stable, pre-release or development build.';
4141

4242
/**
4343
* @var string
4444
*/
45-
protected $version;
45+
protected string $version;
4646

4747
/**
4848
* Execute the command.
@@ -118,15 +118,15 @@ protected function execute(Input $input, Output $output): void
118118
$this->updateToMostRecentNonDevRemote();
119119
}
120120

121-
protected function getStableUpdater()
121+
protected function getStableUpdater(): Updater
122122
{
123123
$updater = new Updater;
124124
$updater->setStrategy(Updater::STRATEGY_GITHUB);
125125

126126
return $this->getGithubReleasesUpdater($updater);
127127
}
128128

129-
protected function getPreReleaseUpdater()
129+
protected function getPreReleaseUpdater(): Updater
130130
{
131131
$updater = new Updater;
132132
$updater->setStrategy(Updater::STRATEGY_GITHUB);
@@ -135,7 +135,7 @@ protected function getPreReleaseUpdater()
135135
return $this->getGithubReleasesUpdater($updater);
136136
}
137137

138-
protected function getMostRecentNonDevUpdater()
138+
protected function getMostRecentNonDevUpdater(): Updater
139139
{
140140
$updater = new Updater;
141141
$updater->setStrategy(Updater::STRATEGY_GITHUB);

src/Util/FormatUtil.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,18 @@
1919
use function array_merge;
2020
use function array_shift;
2121
use function explode;
22-
use function get_class;
2322
use function implode;
2423
use function is_array;
2524
use function is_bool;
2625
use function is_int;
2726
use function is_numeric;
28-
use function is_object;
2927
use function is_scalar;
3028
use function rtrim;
3129
use function str_repeat;
3230
use function str_replace;
3331
use function strpos;
3432
use function trim;
3533
use function ucfirst;
36-
use function vdump;
3734
use function wordwrap;
3835
use const STR_PAD_RIGHT;
3936

@@ -49,7 +46,7 @@ final class FormatUtil
4946
*
5047
* @return string
5148
*/
52-
public static function typeToString($val): string
49+
public static function typeToString(mixed $val): string
5350
{
5451
if (null === $val) {
5552
return '(Null)';
@@ -184,7 +181,7 @@ public static function alignOptions(array $options): array
184181
* @return string
185182
* @deprecated use Format::memory($secs);
186183
*/
187-
public static function memoryUsage($memory): string
184+
public static function memoryUsage(float|int $memory): string
188185
{
189186
return Format::memory($memory);
190187
}

src/Util/Helper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use RuntimeException;
2121
use Swoole\Coroutine;
2222
use Toolkit\Stdlib\Arr\ArrayHelper;
23+
use Traversable;
2324
use function class_exists;
2425
use function file_exists;
2526
use function is_dir;
@@ -28,7 +29,6 @@
2829
use function similar_text;
2930
use function sprintf;
3031
use function strlen;
31-
use function strpos;
3232

3333
/**
3434
* Class Helper
@@ -77,7 +77,7 @@ public static function hasMode(int $haystack, int $value): bool
7777
*/
7878
public static function isAbsPath(string $path): bool
7979
{
80-
return strpos($path, '/') === 0 || 1 === preg_match('#^[a-z]:[\/|\\\]{1}.+#i', $path);
80+
return str_starts_with($path, '/') || 1 === preg_match('#^[a-z]:[\/|\\\]{1}.+#i', $path);
8181
}
8282

8383
/**
@@ -126,15 +126,15 @@ public static function mkdir(string $dir, int $mode = 0775): void
126126
/**
127127
* @param string $srcDir
128128
* @param callable $filter
129-
* @param int $flags
129+
* @param int $flags
130130
*
131131
* @return RecursiveIteratorIterator
132132
* @throws InvalidArgumentException
133133
*/
134134
public static function directoryIterator(
135135
string $srcDir,
136136
callable $filter,
137-
$flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
137+
int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
138138
): RecursiveIteratorIterator {
139139
if (!$srcDir || !file_exists($srcDir)) {
140140
throw new InvalidArgumentException('Please provide a exists source directory.');
@@ -158,12 +158,12 @@ public static function commandSearch(string $command, array $map): void
158158
* find similar text from an array|Iterator
159159
*
160160
* @param string $need
161-
* @param Iterator|array $iterator
161+
* @param Traversable|array $iterator
162162
* @param int $similarPercent
163163
*
164164
* @return array
165165
*/
166-
public static function findSimilar(string $need, $iterator, int $similarPercent = 45): array
166+
public static function findSimilar(string $need, Traversable|array $iterator, int $similarPercent = 45): array
167167
{
168168
if (!$need) {
169169
return [];

src/Util/Interact.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Interact extends Show
3232
/**
3333
* read line from CLI input
3434
*
35-
* @param mixed $message
35+
* @param mixed|null $message
3636
* @param bool $nl
3737
* @param array $opts
3838
* [
@@ -41,31 +41,31 @@ class Interact extends Show
4141
*
4242
* @return string
4343
*/
44-
public static function readln($message = null, bool $nl = false, array $opts = []): string
44+
public static function readln(mixed $message = null, bool $nl = false, array $opts = []): string
4545
{
4646
return Cli::readln($message, $nl, $opts);
4747
}
4848

4949
/**
5050
* 读取输入信息
5151
*
52-
* @param mixed $message 若不为空,则先输出文本
52+
* @param mixed|null $message 若不为空,则先输出文本
5353
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
5454
*
5555
* @return string
5656
*/
57-
public static function readRow($message = null, bool $nl = false): string
57+
public static function readRow(mixed $message = null, bool $nl = false): string
5858
{
5959
return Cli::readln($message, $nl);
6060
}
6161

6262
/**
63-
* @param null|mixed $message
63+
* @param mixed|null $message
6464
* @param bool $nl
6565
*
6666
* @return string
6767
*/
68-
public static function readFirst($message = null, bool $nl = false): string
68+
public static function readFirst(mixed $message = null, bool $nl = false): string
6969
{
7070
return Cli::readFirst($message, $nl);
7171
}
@@ -78,14 +78,14 @@ public static function readFirst($message = null, bool $nl = false): string
7878
* alias of the `select()`
7979
*
8080
* @param string $description 说明
81-
* @param string|array $options 选项数据
81+
* @param array|string $options 选项数据
8282
* @param null $default 默认选项
8383
* @param bool $allowExit 有退出选项 默认 true
8484
* @param array $opts
8585
*
8686
* @return string
8787
*/
88-
public static function select(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
88+
public static function select(string $description, array|string $options, $default = null, bool $allowExit = true, array $opts = []): string
8989
{
9090
return self::choice($description, $options, $default, $allowExit, $opts);
9191
}
@@ -94,7 +94,7 @@ public static function select(string $description, $options, $default = null, bo
9494
* Choose one of several options
9595
*
9696
* @param string $description
97-
* @param string|array $options Option data
97+
* @param array|string $options Option data
9898
* e.g
9999
* [
100100
* // option => value
@@ -107,7 +107,7 @@ public static function select(string $description, $options, $default = null, bo
107107
*
108108
* @return string
109109
*/
110-
public static function choice(string $description, $options, $default = null, bool $allowExit = true, array $opts = []): string
110+
public static function choice(string $description, array|string $options, $default = null, bool $allowExit = true, array $opts = []): string
111111
{
112112
return Choose::one($description, $options, $default, $allowExit, $opts);
113113
}
@@ -116,13 +116,13 @@ public static function choice(string $description, $options, $default = null, bo
116116
* alias of the `multiSelect()`
117117
*
118118
* @param string $description
119-
* @param string|array $options
120-
* @param null|mixed $default
119+
* @param array|string $options
120+
* @param mixed|null $default
121121
* @param bool $allowExit
122122
*
123123
* @return array
124124
*/
125-
public static function checkbox(string $description, $options, $default = null, bool $allowExit = true): array
125+
public static function checkbox(string $description, array|string $options, mixed $default = null, bool $allowExit = true): array
126126
{
127127
return self::multiSelect($description, $options, $default, $allowExit);
128128
}
@@ -131,13 +131,13 @@ public static function checkbox(string $description, $options, $default = null,
131131
* List multiple options and allow multiple selections
132132
*
133133
* @param string $description
134-
* @param string|array $options
135-
* @param null|mixed $default
134+
* @param array|string $options
135+
* @param mixed|null $default
136136
* @param bool $allowExit
137137
*
138138
* @return array
139139
*/
140-
public static function multiSelect(string $description, $options, $default = null, bool $allowExit = true): array
140+
public static function multiSelect(string $description, array|string $options, mixed $default = null, bool $allowExit = true): array
141141
{
142142
return Checkbox::select($description, $options, $default, $allowExit);
143143
}

src/Util/PhpDevServe.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,29 @@ class PhpDevServe
4242
*
4343
* @var string
4444
*/
45-
protected $phpBin = self::PHP_BIN;
45+
protected string $phpBin = self::PHP_BIN;
4646

4747
/**
4848
* The document root dir for server
4949
*
5050
* @var string
5151
*/
52-
protected $docRoot = '';
52+
protected string $docRoot = '';
5353

5454
/**
5555
* The entry file for server. e.g web/index.php
5656
* NOTICE: if set the entry file, will need handle static file access.
5757
*
5858
* @var string
5959
*/
60-
protected $entryFile = '';
60+
protected string $entryFile = '';
6161

6262
/**
6363
* The http server address. e.g 127.0.0.1:8552
6464
*
6565
* @var string
6666
*/
67-
protected $serveAddr = '';
67+
protected string $serveAddr = '';
6868

6969
/**
7070
* Can custom message for print before start server.
@@ -79,17 +79,17 @@ class PhpDevServe
7979
*
8080
* @var array
8181
*/
82-
private $hceData = [];
82+
private array $hceData = [];
8383

8484
/**
8585
* @var string
8686
*/
87-
private $hceEnv = '';
87+
private string $hceEnv = '';
8888

8989
/**
9090
* @var array
9191
*/
92-
private $envVars = [];
92+
private array $envVars = [];
9393

9494
/**
9595
* @param string $serveAddr
@@ -243,9 +243,10 @@ public function getCommand(bool $checkEnv = true): string
243243

244244
/**
245245
* @param string $hceFile
246-
* @param bool $mustLoad
246+
* @param bool $mustLoad
247247
*
248248
* @return bool
249+
* @throws \JsonException
249250
*/
250251
public function loadHceFile(string $hceFile, bool $mustLoad = false): bool
251252
{

0 commit comments

Comments
 (0)