Skip to content

Commit 220cb12

Browse files
committed
format codes, add more param type define
1 parent 1d0ea6b commit 220cb12

30 files changed

+298
-276
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- `ArtFont::class` 支持 ansi 图案字体显示(运行 `php examples/app -V` 可以看到效果)
2828
- `Download::class` 内置的简单的文件下载工具类,带有进度条
2929
- `Terminal::class` 简单的Terminal屏幕、光标控制操作类
30+
- `ProcessUtil::class` 简单的进程操作使用类(fork,run,stop,wait ... 等)
3031
- **TODO** 快速的为当前应用生成 `bash/zsh` 环境下的自动补全脚本
3132

3233
> 下面所有的特性,效果都是运行 `examples/` 中的示例代码 `php examples/app` 展示出来的。基本上涵盖了所有功能,可以直接测试运行
@@ -241,17 +242,17 @@ $app->registerGroups('App\\Console\\Controllers', dirname(__DIR__) . '/Controlle
241242

242243
命令上的注释是可被解析的
243244

244-
- 注释中的 `@usage` `@arguments` `@options` `@example` 在使用帮助命令时,会被解析并显示出来
245-
- 注释里面同样支持带颜色的文本输出 `eg: this is a command's description <info>message</info>`
246-
- 上述注释tag里,支持变量替换(例如: `{command}` 会自动替换为当前输入的命令)
247245
- 当你使用 `php examples/app home -h` 时,可以查看到 `HomeController` 的所有命令描述注释信息
248246

249247
![group-command-list](docs/screenshots/group-command-list.png)
248+
- 看到一些命令最后的 `[alias: ...]` 了吗,那是此命令拥有的别名.
249+
- 即用别名也可以访问它,当一个命令太长时可以加别名以方便使用。一个命令可以拥有多个别名
250250
- 当使用 `php examples/app home:test -h` 时,可以查看到关于 `HomeController::testCommand` 更详细的信息
251251

252252
![group-command-list](docs/screenshots/command-help.png)
253-
254-
- 看到一些命令最后的 `[alias: ...]` 了吗,那是此命令拥有的别名. 即用别名也可以访问它,当一个命令太长时可以加别名方便使用
253+
- 注释中的 `@usage` `@arguments` `@options` `@example` 在使用帮助命令时,会被解析并显示出来
254+
- 注释里面同样支持带颜色的文本输出 `eg: this is a command's description <info>message</info>`
255+
- 上述注释tag里,支持变量替换(例如: `{command}` 会自动替换为当前输入的命令)
255256

256257
更多请查看 [examples](./examples) 中的示例代码和在目录下运行示例 `php examples/app home` 来查看效果
257258

examples/Commands/TestCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ class TestCommand extends Command
2222
* test text
2323
* @usage {name} test message
2424
* @arguments
25-
* arg1 argument description 1
26-
* arg2 argument description 2
25+
* arg1 argument description 1
26+
* arg2 argument description 2
2727
* @options
28-
* --long,-s option description 1
29-
* --opt option description 2
28+
* --long,-s option description 1
29+
* --opt option description 2
3030
* @param $input
3131
* @param $output
3232
*/

examples/Controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public function pointingCommand()
289289
* @param Input $input
290290
* @return int
291291
*/
292-
public function progressCommand($input)
292+
public function progressCommand($input): int
293293
{
294294
$i = 0;
295295
$total = 120;

src/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ protected function getFileFilter(): callable
264264
* @inheritdoc
265265
* @throws \InvalidArgumentException
266266
*/
267-
protected function dispatch($name)
267+
protected function dispatch(string $name)
268268
{
269269
$sep = $this->delimiter ?: ':';
270270

@@ -357,6 +357,7 @@ public function runCommand($name, $believable = false)
357357
* @param bool $standAlone
358358
* @return mixed
359359
* @throws \InvalidArgumentException
360+
* @throws \ReflectionException
360361
*/
361362
public function runAction($name, $action, $believable = false, $standAlone = false)
362363
{

src/Base/AbstractApplication.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function run($exit = true)
174174
* @param string $command A command name
175175
* @return int|mixed
176176
*/
177-
abstract protected function dispatch($command);
177+
abstract protected function dispatch(string $command);
178178

179179
/**
180180
* run a independent command
@@ -287,7 +287,7 @@ protected function logError($e)
287287
/**
288288
* @param $command
289289
*/
290-
protected function filterSpecialCommand($command)
290+
protected function filterSpecialCommand(string $command)
291291
{
292292
if (!$command) {
293293
if ($this->input->getSameOpt(['V', 'version'])) {
@@ -484,7 +484,7 @@ public function showCommandList($quit = true)
484484
/**
485485
* @param string $name
486486
* @param string $default
487-
* @return string
487+
* @return string|null
488488
*/
489489
public function getCommandMessage($name, $default = null)
490490
{
@@ -529,7 +529,7 @@ public function addCommandAliases(string $name, $aliases)
529529
* @param string $name
530530
* @return string
531531
*/
532-
protected function getRealCommandName(string $name)
532+
protected function getRealCommandName(string $name): string
533533
{
534534
return $this->commandAliases[$name] ?? $name;
535535
}
@@ -541,15 +541,15 @@ protected function getRealCommandName(string $name)
541541
/**
542542
* @return array
543543
*/
544-
public function getControllerNames()
544+
public function getControllerNames(): array
545545
{
546546
return array_keys($this->controllers);
547547
}
548548

549549
/**
550550
* @return array
551551
*/
552-
public function getCommandNames()
552+
public function getCommandNames(): array
553553
{
554554
return array_keys($this->commands);
555555
}
@@ -581,7 +581,7 @@ public function getControllers(): array
581581
* @param $name
582582
* @return bool
583583
*/
584-
public function isController($name)
584+
public function isController(string $name): bool
585585
{
586586
return isset($this->controllers[$name]);
587587
}
@@ -613,7 +613,7 @@ public function getCommands(): array
613613
* @param $name
614614
* @return bool
615615
*/
616-
public function isCommand($name)
616+
public function isCommand(string $name): bool
617617
{
618618
return isset($this->commands[$name]);
619619
}
@@ -683,7 +683,7 @@ public function isInternalCommand(string $name): bool
683683
/**
684684
* @return string
685685
*/
686-
public function getName()
686+
public function getName(): string
687687
{
688688
return $this->meta['name'];
689689
}
@@ -705,7 +705,7 @@ public function setMeta(array $meta)
705705
* @param null|string $default
706706
* @return array|string
707707
*/
708-
public function getMeta($name = null, $default = null)
708+
public function getMeta(string $name = null, $default = null)
709709
{
710710
if (!$name) {
711711
return $this->meta;
@@ -727,7 +727,7 @@ public function isDebug()
727727
* is profile
728728
* @return boolean
729729
*/
730-
public function isProfile()
730+
public function isProfile(): bool
731731
{
732732
return (bool)$this->input->getOpt('profile', $this->getMeta('profile'));
733733
}

src/Base/AbstractCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function createDefinition()
121121
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
122122
* @return array
123123
*/
124-
public function annotationVars()
124+
public function annotationVars(): array
125125
{
126126
// e.g: `more info see {name}:index`
127127
return [
@@ -143,7 +143,7 @@ public function annotationVars()
143143
* @param string $command
144144
* @return int
145145
*/
146-
public function run($command = '')
146+
public function run(string $command = ''): int
147147
{
148148
// load input definition configure
149149
$this->configure();
@@ -209,7 +209,7 @@ protected function showHelp(): bool
209209
$help['global options:'] = FormatUtil::alignmentOptions(Application::getInternalOptions());
210210

211211
if (empty($help['description']) && $this->isAlone()) {
212-
$help['description'] = self::getDefinition();
212+
$help['description'] = self::getDescription();
213213
}
214214

215215
$this->output->mList($help, ['sepChar' => ' ']);
@@ -488,7 +488,7 @@ final public static function getName(): string
488488
/**
489489
* @return string
490490
*/
491-
public static function getDescription()
491+
public static function getDescription(): string
492492
{
493493
return static::$description;
494494
}
@@ -529,7 +529,7 @@ public static function setAnnotationTags(array $annotationTags, $replace = false
529529
}
530530

531531
/**
532-
* @return InputDefinition
532+
* @return InputDefinition|null
533533
*/
534534
public function getDefinition()
535535
{

src/Base/BaseCommandInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ interface BaseCommandInterface
2626
* @param string $command
2727
* @return int
2828
*/
29-
public function run($command = '');
29+
public function run(string $command = ''): int;
3030

3131
/**
32-
* @return InputDefinition
32+
* @return InputDefinition|null
3333
*/
3434
public function getDefinition();
3535

@@ -46,5 +46,5 @@ public static function getName(): string;
4646
/**
4747
* @return string
4848
*/
49-
public static function getDescription();
49+
public static function getDescription(): string;
5050
}

src/Command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ abstract class Command extends AbstractCommand implements CommandInterface
5151

5252
/**
5353
* @return bool
54+
* @throws \ReflectionException
5455
*/
5556
protected function showHelp(): bool
5657
{

src/Components/ArtFont.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,20 @@ protected function loadInternalFonts()
8686
* display the internal art font
8787
* @param string $name
8888
* @param array $opts
89-
* @return bool
89+
* @return int
9090
*/
91-
public function showInternal(string $name, array $opts = [])
91+
public function showInternal(string $name, array $opts = []): int
9292
{
9393
return $this->show($name, self::INTERNAL_GROUP, $opts);
9494
}
9595

9696
/**
9797
* @param string $name
9898
* @param string $group
99-
* @return string
99+
* @param array $opts
100+
* @return int
100101
*/
101-
public function showItalic(string $name, string $group = null, array $opts = [])
102+
public function showItalic(string $name, string $group = null, array $opts = []): int
102103
{
103104
$opts['type'] = 'italic';
104105

@@ -114,9 +115,9 @@ public function showItalic(string $name, string $group = null, array $opts = [])
114115
* - type => '', // 'italic'
115116
* - indent => 2,
116117
* - style => '', // 'info' 'error'
117-
* @return bool
118+
* @return int
118119
*/
119-
public function show(string $name, string $group = null, array $opts = [])
120+
public function show(string $name, string $group = null, array $opts = []): int
120121
{
121122
$opts = array_merge([
122123
'type' => '',
@@ -153,20 +154,19 @@ public function show(string $name, string $group = null, array $opts = [])
153154
return Show::write(Helper::wrapTag($txt, $opts['style']));
154155
}
155156

156-
return false;
157+
return 0;
157158
}
158159

159160
/**
160161
* @param string $name
161162
* @param string $group
162163
* @return string
163164
*/
164-
public function font(string $name, string $group = null)
165+
public function font(string $name, string $group = null): string
165166
{
166167
return '';
167168
}
168169

169-
170170
/**
171171
* @param string $group
172172
* @param string $path

src/Components/AryBuffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function setBody(array $body)
8484
/**
8585
* @return string
8686
*/
87-
public function toString()
87+
public function toString(): string
8888
{
8989
return implode($this->delimiter, $this->body);
9090
}

0 commit comments

Comments
 (0)