Skip to content

Commit 9bc1e5f

Browse files
committed
add struct return type
1 parent 758033f commit 9bc1e5f

36 files changed

+203
-210
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PHP console
22

33
[![License](https://img.shields.io/packagist/l/inhere/console.svg?style=flat-square)](LICENSE)
4-
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/console)
4+
[![Php Version](https://img.shields.io/badge/php-%3E=7.1.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/inhere/console)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/inhere/console.svg)](https://packagist.org/packages/inhere/console)
66

77
A simple, full-featured php command line application library.

examples/Command/DemoCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class DemoCommand extends Command
2525
* {@inheritDoc}
2626
* @throws \LogicException
2727
*/
28-
protected function configure()
28+
protected function configure(): void
2929
{
3030
$this->createDefinition()
31-
->setExample($this->parseAnnotationVars('{script} {command} john male 43 --opt1 value1'))
31+
->setExample($this->parseCommentsVars('{script} {command} john male 43 --opt1 value1'))
3232
->addArgument('name', Input::ARG_REQUIRED, 'description for the argument [name], is required')
3333
->addArgument('sex', Input::ARG_OPTIONAL, 'description for the argument [sex], is optional')
3434
->addArgument('age', Input::ARG_OPTIONAL, 'description for the argument [age], is optional')

examples/Controller/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function disabledCommands(): array
5353
return ['disabled'];
5454
}
5555

56-
protected function afterExecute()
56+
protected function afterExecute(): void
5757
{
5858
// $this->write('after command execute');
5959
}

src/AbstractApplication.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function __construct(array $config = [], Input $input = null, Output $out
118118
/**
119119
* @throws \InvalidArgumentException
120120
*/
121-
protected function init()
121+
protected function init(): void
122122
{
123123
$this->stats = [
124124
'startTime' => \microtime(1),
@@ -145,7 +145,7 @@ public static function getGlobalOptions(): array
145145
/**
146146
* @param array $options
147147
*/
148-
public function addGlobalOptions(array $options)
148+
public function addGlobalOptions(array $options): void
149149
{
150150
if ($options) {
151151
self::$globalOptions = \array_merge(self::$globalOptions, $options);
@@ -156,7 +156,7 @@ public function addGlobalOptions(array $options)
156156
* app run
157157
**********************************************************/
158158

159-
protected function prepareRun()
159+
protected function prepareRun(): void
160160
{
161161
if ($this->input->getSameOpt(['no-color'])) {
162162
Style::setNoColor();
@@ -166,7 +166,7 @@ protected function prepareRun()
166166
// new AutoCompletion(array_merge($this->getCommandNames(), $this->getControllerNames()));
167167
}
168168

169-
protected function beforeRun()
169+
protected function beforeRun(): void
170170
{
171171
}
172172

@@ -220,7 +220,7 @@ public function run(bool $exit = true)
220220
*/
221221
abstract protected function dispatch(string $command);
222222

223-
protected function afterRun()
223+
protected function afterRun(): void
224224
{
225225
}
226226

@@ -267,7 +267,7 @@ public function subRun(string $command, InputInterface $input, OutputInterface $
267267
/**
268268
* runtime env check
269269
*/
270-
protected function runtimeCheck()
270+
protected function runtimeCheck(): void
271271
{
272272
// check env
273273
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'cli-server'], true)) {
@@ -282,7 +282,7 @@ protected function runtimeCheck()
282282
* register error handle
283283
* @throws \InvalidArgumentException
284284
*/
285-
protected function registerErrorHandle()
285+
protected function registerErrorHandle(): void
286286
{
287287
\set_error_handler([$this, 'handleError']);
288288
\set_exception_handler([$this, 'handleException']);
@@ -301,7 +301,7 @@ protected function registerErrorHandle()
301301
* @param int $line
302302
* @throws \InvalidArgumentException
303303
*/
304-
public function handleError(int $num, string $str, string $file, int $line)
304+
public function handleError(int $num, string $str, string $file, int $line): void
305305
{
306306
$this->handleException(new \ErrorException($str, 0, $num, $file, $line));
307307
$this->stop(-1);
@@ -312,7 +312,7 @@ public function handleError(int $num, string $str, string $file, int $line)
312312
* @param \Throwable $e
313313
* @throws \InvalidArgumentException
314314
*/
315-
public function handleException($e)
315+
public function handleException($e): void
316316
{
317317
// you can log error on sub class ...
318318

@@ -365,7 +365,7 @@ protected function filterSpecialCommand(string $command): bool
365365
* @param bool $isGroup
366366
* @throws \InvalidArgumentException
367367
*/
368-
protected function validateName(string $name, bool $isGroup = false)
368+
protected function validateName(string $name, bool $isGroup = false): void
369369
{
370370
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
371371

@@ -413,11 +413,7 @@ protected function getRealCommandName(string $name): string
413413
*/
414414
public function findCommand(string $name)
415415
{
416-
if (isset($this->commands[$name])) {
417-
return $this->commands[$name];
418-
}
419-
420-
return $this->controllers[$name] ?? null;
416+
return $this->commands[$name] ?? $this->controllers[$name] ?? null;
421417
}
422418

423419
/**********************************************************
@@ -444,7 +440,7 @@ public function getCommandNames(): array
444440
* @param array $controllers
445441
* @throws \InvalidArgumentException
446442
*/
447-
public function setControllers(array $controllers)
443+
public function setControllers(array $controllers): void
448444
{
449445
foreach ($controllers as $name => $controller) {
450446
if (\is_int($name)) {
@@ -476,7 +472,7 @@ public function isController(string $name): bool
476472
* @param array $commands
477473
* @throws \InvalidArgumentException
478474
*/
479-
public function setCommands(array $commands)
475+
public function setCommands(array $commands): void
480476
{
481477
foreach ($commands as $name => $handler) {
482478
if (\is_int($name)) {
@@ -507,7 +503,7 @@ public function isCommand(string $name): bool
507503
/**
508504
* @return string|null
509505
*/
510-
public function getLogoText()
506+
public function getLogoText(): ?string
511507
{
512508
return $this->config['logoText'] ?? null;
513509
}
@@ -516,7 +512,7 @@ public function getLogoText()
516512
* @param string $logoTxt
517513
* @param string|null $style
518514
*/
519-
public function setLogo(string $logoTxt, string $style = null)
515+
public function setLogo(string $logoTxt, string $style = null): void
520516
{
521517
$this->config['logoText'] = $logoTxt;
522518

@@ -528,15 +524,15 @@ public function setLogo(string $logoTxt, string $style = null)
528524
/**
529525
* @return string|null
530526
*/
531-
public function getLogoStyle()
527+
public function getLogoStyle(): ?string
532528
{
533529
return $this->config['logoStyle'] ?? 'info';
534530
}
535531

536532
/**
537533
* @param string $style
538534
*/
539-
public function setLogoStyle(string $style)
535+
public function setLogoStyle(string $style): void
540536
{
541537
$this->config['logoStyle'] = $style;
542538
}
@@ -585,7 +581,7 @@ public function getVersion(): string
585581
/**
586582
* @param array $config
587583
*/
588-
public function setConfig(array $config)
584+
public function setConfig(array $config): void
589585
{
590586
if ($config) {
591587
$this->config = \array_merge($this->config, $config);
@@ -645,7 +641,7 @@ public function getCommandAliases(string $name = null): array
645641
/**
646642
* @param array $commandAliases
647643
*/
648-
public function setCommandAliases(array $commandAliases)
644+
public function setCommandAliases(array $commandAliases): void
649645
{
650646
$this->commandAliases = $commandAliases;
651647
}
@@ -662,7 +658,7 @@ public function getCommandsMeta(): array
662658
* @param string $command
663659
* @param array $meta
664660
*/
665-
public function setCommandMeta(string $command, array $meta)
661+
public function setCommandMeta(string $command, array $meta): void
666662
{
667663
if (isset($this->commandsMeta[$command])) {
668664
$this->commandsMeta[$command] = \array_merge($this->commandsMeta[$command], $meta);
@@ -685,7 +681,7 @@ public function getCommandMeta(string $command): array
685681
* @param string $key
686682
* @param $value
687683
*/
688-
public function setCommandMetaValue(string $command, string $key, $value)
684+
public function setCommandMetaValue(string $command, string $key, $value): void
689685
{
690686
if ($value !== null) {
691687
$this->commandsMeta[$command][$key] = $value;

src/AbstractCommand.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,15 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
116116
$this->init();
117117
}
118118

119-
protected function init()
119+
protected function init(): void
120120
{
121121
}
122122

123123
/**
124124
* Configure input definition for command, like symfony console.
125-
* @return InputDefinition|null
126125
*/
127-
protected function configure()
126+
protected function configure(): void
128127
{
129-
return null;
130128
}
131129

132130
/**
@@ -232,7 +230,7 @@ public function run(string $command = '')
232230
* coroutine run by swoole go()
233231
* @return bool
234232
*/
235-
public function coroutineRun()
233+
public function coroutineRun(): bool
236234
{
237235
// $ch = new Coroutine\Channel(1);
238236
$ok = Coroutine::create(function (){
@@ -278,7 +276,7 @@ abstract protected function execute($input, $output);
278276
/**
279277
* after command execute
280278
*/
281-
protected function afterExecute()
279+
protected function afterExecute(): void
282280
{
283281
}
284282

@@ -401,7 +399,7 @@ public function validateInput(): bool
401399
* @param string $name
402400
* @param string|array $value
403401
*/
404-
protected function addCommentsVar(string $name, $value)
402+
protected function addCommentsVar(string $name, $value): void
405403
{
406404
if (!isset($this->commentsVars[$name])) {
407405
$this->setCommentsVar($name, $value);
@@ -411,7 +409,7 @@ protected function addCommentsVar(string $name, $value)
411409
/**
412410
* @param array $map
413411
*/
414-
protected function addCommentsVars(array $map)
412+
protected function addCommentsVars(array $map): void
415413
{
416414
foreach ($map as $name => $value) {
417415
$this->setCommentsVar($name, $value);
@@ -422,7 +420,7 @@ protected function addCommentsVars(array $map)
422420
* @param string $name
423421
* @param string|array $value
424422
*/
425-
protected function setCommentsVar(string $name, $value)
423+
protected function setCommentsVar(string $name, $value): void
426424
{
427425
$this->commentsVars[$name] = \is_array($value) ? \implode(',', $value) : (string)$value;
428426
}
@@ -582,7 +580,7 @@ protected function showHelpByMethodAnnotations(string $method, string $action =
582580
/**
583581
* @param string $name
584582
*/
585-
final public static function setName(string $name)
583+
final public static function setName(string $name): void
586584
{
587585
static::$name = $name;
588586
}
@@ -606,7 +604,7 @@ public static function getDescription(): string
606604
/**
607605
* @param string $description
608606
*/
609-
public static function setDescription(string $description)
607+
public static function setDescription(string $description): void
610608
{
611609
static::$description = $description;
612610
}
@@ -622,7 +620,7 @@ public static function isCoroutine(): bool
622620
/**
623621
* @param bool $coroutine
624622
*/
625-
public static function setCoroutine($coroutine)
623+
public static function setCoroutine($coroutine): void
626624
{
627625
static::$coroutine = (bool)$coroutine;
628626
}
@@ -638,7 +636,7 @@ final public static function getAnnotationTags(): array
638636
/**
639637
* @param string $name
640638
*/
641-
public static function addAnnotationTag(string $name)
639+
public static function addAnnotationTag(string $name): void
642640
{
643641
if (!isset(self::$annotationTags[$name])) {
644642
self::$annotationTags[$name] = true;
@@ -649,23 +647,23 @@ public static function addAnnotationTag(string $name)
649647
* @param array $annotationTags
650648
* @param bool $replace
651649
*/
652-
public static function setAnnotationTags(array $annotationTags, $replace = false)
650+
public static function setAnnotationTags(array $annotationTags, $replace = false): void
653651
{
654652
self::$annotationTags = $replace ? $annotationTags : array_merge(self::$annotationTags, $annotationTags);
655653
}
656654

657655
/**
658656
* @return InputDefinition|null
659657
*/
660-
public function getDefinition()
658+
public function getDefinition(): ?InputDefinition
661659
{
662660
return $this->definition;
663661
}
664662

665663
/**
666664
* @param InputDefinition $definition
667665
*/
668-
public function setDefinition(InputDefinition $definition)
666+
public function setDefinition(InputDefinition $definition): void
669667
{
670668
$this->definition = $definition;
671669
}
@@ -697,7 +695,7 @@ public function getApp(): AbstractApplication
697695
/**
698696
* @param AbstractApplication $app
699697
*/
700-
public function setApp(AbstractApplication $app)
698+
public function setApp(AbstractApplication $app): void
701699
{
702700
$this->app = $app;
703701
}
@@ -713,7 +711,7 @@ public function getProcessTitle(): string
713711
/**
714712
* @param string $processTitle
715713
*/
716-
public function setProcessTitle(string $processTitle)
714+
public function setProcessTitle(string $processTitle): void
717715
{
718716
$this->processTitle = $processTitle;
719717
}

0 commit comments

Comments
 (0)