Skip to content

Commit 7611aba

Browse files
committed
style: run code inspect for some class
1 parent c5bc385 commit 7611aba

18 files changed

+131
-118
lines changed

phar.build.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ $compiler->setStripFilter(static function ($file) {
3636
/** @var SplFileInfo $file */
3737
$name = $file->getFilename();
3838

39-
return false === strpos($name, 'Command.php') && false === strpos($name, 'Controller.php');
39+
return !str_contains($name, 'Command.php') && !str_contains($name, 'Controller.php');
4040
});

src/AbstractApplication.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Inhere\Console\Util\Helper;
2626
use Inhere\Console\Util\Interact;
2727
use InvalidArgumentException;
28+
use JetBrains\PhpStorm\NoReturn;
2829
use Throwable;
2930
use Toolkit\Cli\Helper\FlagHelper;
3031
use Toolkit\Cli\Style;
@@ -66,14 +67,14 @@ abstract class AbstractApplication implements ApplicationInterface
6667
use SimpleEventAwareTrait;
6768

6869
/** @var array */
69-
protected static $internalCommands = [
70+
protected static array $internalCommands = [
7071
'version' => 'Show application version information',
7172
'help' => 'Show application help information',
7273
'list' => 'List all group and alone commands',
7374
];
7475

7576
/** @var array Application runtime stats */
76-
protected $stats = [
77+
protected array $stats = [
7778
'startTime' => 0,
7879
'endTime' => 0,
7980
'startMemory' => 0,
@@ -83,19 +84,19 @@ abstract class AbstractApplication implements ApplicationInterface
8384
/**
8485
* @var string
8586
*/
86-
public $delimiter = ':'; // '/' ':'
87+
public string $delimiter = ':'; // '/' ':'
8788

8889
/**
8990
* @var string
9091
*/
91-
protected $commandName = '';
92+
protected string $commandName = '';
9293

9394
/**
9495
* @var string Command delimiter char. e.g dev:serve
9596
*/
9697

9798
/** @var array Application config data */
98-
protected $config = [
99+
protected array $config = [
99100
'name' => 'My Console Application',
100101
'desc' => 'This is my console application',
101102
'version' => '0.5.1',
@@ -124,17 +125,17 @@ abstract class AbstractApplication implements ApplicationInterface
124125
/**
125126
* @var Router
126127
*/
127-
protected $router;
128+
protected Router $router;
128129

129130
/**
130131
* @var ErrorHandlerInterface Can custom error handler
131132
*/
132-
protected $errorHandler;
133+
protected ErrorHandlerInterface $errorHandler;
133134

134135
/**
135136
* @var Controller[]
136137
*/
137-
protected $groupObjects = [];
138+
protected array $groupObjects = [];
138139

139140
/**
140141
* Class constructor.
@@ -169,12 +170,10 @@ protected function init(): void
169170
'endMemory' => 0,
170171
];
171172

172-
if (!$this->errorHandler) {
173-
$this->errorHandler = new ErrorHandler([
174-
'rootPath' => $this->config['rootPath'],
175-
'hideRootPath' => (bool)$this->config['hideRootPath'],
176-
]);
177-
}
173+
$this->errorHandler = new ErrorHandler([
174+
'rootPath' => $this->config['rootPath'],
175+
'hideRootPath' => (bool)$this->config['hideRootPath'],
176+
]);
178177

179178
$this->registerErrorHandle();
180179

@@ -288,10 +287,10 @@ protected function beforeRun(): bool
288287
*
289288
* @param bool $exit
290289
*
291-
* @return int|mixed
290+
* @return mixed
292291
* @throws InvalidArgumentException
293292
*/
294-
public function run(bool $exit = true)
293+
public function run(bool $exit = true): mixed
295294
{
296295
try {
297296
// init
@@ -334,6 +333,7 @@ protected function afterRun(): void
334333
/**
335334
* @param int $code
336335
*/
336+
#[NoReturn]
337337
public function stop(int $code = 0): void
338338
{
339339
// call 'onAppStop' event, if it is registered.
@@ -355,9 +355,9 @@ public function stop(int $code = 0): void
355355
/**
356356
* @param array $args
357357
*
358-
* @return int|mixed
358+
* @return mixed
359359
*/
360-
public function runWithArgs(array $args)
360+
public function runWithArgs(array $args): mixed
361361
{
362362
$this->input->setFlags($args);
363363
return $this->run(false);
@@ -382,9 +382,9 @@ public function runWithIO(InputInterface $input, OutputInterface $output): void
382382
* @param InputInterface $input
383383
* @param OutputInterface $output
384384
*
385-
* @return int|mixed
385+
* @return mixed
386386
*/
387-
public function subRun(string $command, InputInterface $input, OutputInterface $output)
387+
public function subRun(string $command, InputInterface $input, OutputInterface $output): mixed
388388
{
389389
$app = $this->copy();
390390
$app->setInput($input);
@@ -449,6 +449,7 @@ protected function registerErrorHandle(): void
449449
*
450450
* @throws InvalidArgumentException
451451
*/
452+
#[NoReturn]
452453
public function handleError(int $num, string $str, string $file, int $line): void
453454
{
454455
$this->handleException(new ErrorException($str, 0, $num, $file, $line));
@@ -591,11 +592,11 @@ protected function startInteractiveShell(): void
591592

592593
/**
593594
* @param string $name
594-
* @param string|array $aliases
595+
* @param array|string $aliases
595596
*
596597
* @return $this
597598
*/
598-
public function addAliases(string $name, $aliases): self
599+
public function addAliases(string $name, array|string $aliases): self
599600
{
600601
if ($name && $aliases) {
601602
$this->router->setAlias($name, $aliases, true);
@@ -768,12 +769,12 @@ public function getArrayParam(string $name, array $default = []): array
768769
/**
769770
* Get config param value
770771
*
771-
* @param string $name
772-
* @param null|string|mixed $default
772+
* @param string $name
773+
* @param mixed|null $default
773774
*
774-
* @return array|string
775+
* @return mixed
775776
*/
776-
public function getParam(string $name, $default = null)
777+
public function getParam(string $name, mixed $default = null): mixed
777778
{
778779
return $this->config[$name] ?? $default;
779780
}

src/Annotate/AnnotateRules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AnnotateRules
1919
*
2020
* @var array
2121
*/
22-
protected static $allowedTags = [
22+
protected static array $allowedTags = [
2323
// tag name => allow multi tags
2424
'desc' => false,
2525
'usage' => false,

src/Annotate/Attr/RuleArg.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class RuleArg
2121
/**
2222
* @var string
2323
*/
24-
public $name;
24+
public string $name;
2525

2626
/**
2727
* @var string
2828
* @see FlagsParser::$argRules
2929
*/
30-
public $rule;
30+
public string $rule;
3131
}

src/Annotate/Attr/RuleOpt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ class RuleOpt
2121
/**
2222
* @var string
2323
*/
24-
public $name;
24+
public string $name;
2525

2626
/**
2727
* @var string
2828
* @see FlagsParser::$optRules
2929
*/
30-
public $rule;
30+
public string $rule;
3131
}

src/Annotate/DocblockRules.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DocblockRules
2929
*
3030
* @var array
3131
*/
32-
protected static $allowedTags = [
32+
protected static array $allowedTags = [
3333
// tag name => multi line align
3434
'desc' => false,
3535
'usage' => false,
@@ -46,17 +46,17 @@ class DocblockRules
4646
* @see $allowedTags for keys
4747
* @psalm-var array<string, mixed>
4848
*/
49-
private $docTags;
49+
private array $docTags;
5050

5151
/**
5252
* @var array
5353
*/
54-
private $argRules = [];
54+
private array $argRules = [];
5555

5656
/**
5757
* @var array
5858
*/
59-
private $optRules = [];
59+
private array $optRules = [];
6060

6161
/**
6262
* @param string $doc

src/Application.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Closure;
1313
use Inhere\Console\Contract\ApplicationInterface;
14+
use Inhere\Console\Contract\CommandInterface;
1415
use Inhere\Console\Contract\ControllerInterface;
1516
use Inhere\Console\IO\Input;
1617
use Inhere\Console\IO\Output;
@@ -61,7 +62,7 @@ public function __construct(array $config = [], Input $input = null, Output $out
6162
/**
6263
* {@inheritdoc}
6364
*/
64-
public function controller(string $name, $class = null, array $config = []): ApplicationInterface
65+
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface
6566
{
6667
$this->logf(Console::VERB_CRAZY, 'register group controller: %s', $name);
6768
$this->router->addGroup($name, $class, $config);
@@ -79,7 +80,7 @@ public function controller(string $name, $class = null, array $config = []): App
7980
* @return Application|Contract\ApplicationInterface
8081
* @see controller()
8182
*/
82-
public function addGroup(string $name, $class = null, array $config = []): ApplicationInterface
83+
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface
8384
{
8485
return $this->controller($name, $class, $config);
8586
}
@@ -92,7 +93,7 @@ public function addGroup(string $name, $class = null, array $config = []): Appli
9293
* @return Application|Contract\ApplicationInterface
9394
* @see controller()
9495
*/
95-
public function addController(string $name, $class = null, array $config = []): ApplicationInterface
96+
public function addController(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface
9697
{
9798
return $this->controller($name, $class, $config);
9899
}
@@ -131,7 +132,7 @@ public function addControllers(array $controllers): void
131132
/**
132133
* {@inheritdoc}
133134
*/
134-
public function command(string $name, $handler = null, array $config = [])
135+
public function command(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
135136
{
136137
$this->logf(Console::VERB_CRAZY, 'register alone command: %s', $name);
137138
$this->router->addCommand($name, $handler, $config);
@@ -143,13 +144,13 @@ public function command(string $name, $handler = null, array $config = [])
143144
* add command
144145
*
145146
* @param string $name
146-
* @param null|mixed $handler
147+
* @param mixed|null $handler
147148
* @param array $config
148149
*
149150
* @return Application
150151
* @see command()
151152
*/
152-
public function addCommand(string $name, $handler = null, array $config = []): self
153+
public function addCommand(string $name, mixed $handler = null, array $config = []): self
153154
{
154155
return $this->command($name, $handler, $config);
155156
}
@@ -231,7 +232,7 @@ protected function getFileFilter(): callable
231232
$name = $f->getFilename();
232233

233234
// Skip hidden files and directories.
234-
if (strpos($name, '.') === 0) {
235+
if (str_starts_with($name, '.')) {
235236
return false;
236237
}
237238

@@ -241,7 +242,7 @@ protected function getFileFilter(): callable
241242
}
242243

243244
// php file
244-
return $f->isFile() && substr($name, -4) === '.php';
245+
return $f->isFile() && str_ends_with($name, '.php');
245246
};
246247
}
247248

@@ -253,10 +254,10 @@ protected function getFileFilter(): callable
253254
* @param string $name command name or command ID or command path.
254255
* @param array $args
255256
*
256-
* @return int|mixed
257+
* @return mixed
257258
* @throws Throwable
258259
*/
259-
public function dispatch(string $name, array $args = [])
260+
public function dispatch(string $name, array $args = []): mixed
260261
{
261262
if (!$name = trim($name)) {
262263
throw new InvalidArgumentException('cannot dispatch an empty command');
@@ -320,7 +321,7 @@ public function dispatch(string $name, array $args = [])
320321
* @return mixed
321322
* @throws Throwable
322323
*/
323-
protected function runCommand(array $info, array $options, array $args)
324+
protected function runCommand(array $info, array $options, array $args): mixed
324325
{
325326
/** @var Closure|string $handler Command class or handler func */
326327
$handler = $info['handler'];
@@ -373,7 +374,7 @@ protected function runCommand(array $info, array $options, array $args)
373374
* @return mixed
374375
* @throws Throwable
375376
*/
376-
protected function runAction(array $info, array $options, array $args, bool $detachedRun = false)
377+
protected function runAction(array $info, array $options, array $args, bool $detachedRun = false): mixed
377378
{
378379
$controller = $this->createController($info);
379380
$controller::setDesc($options['desc'] ?? '');

src/Command.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ abstract class Command extends AbstractHandler implements CommandInterface
3434
public const METHOD = 'execute';
3535

3636
/**
37-
* @var Controller
37+
* @var Controller|null
3838
*/
39-
protected $group;
39+
protected ?Controller $group = null;
4040

4141
/**
42-
* @var Command
42+
* @var Command|null
4343
*/
44-
protected $parent;
44+
protected ?Command $parent = null;
4545

4646
protected function init(): void
4747
{
@@ -140,8 +140,8 @@ public function getRealCName(): string
140140
* Get the group
141141
*
142142
* @return Controller
143-
*/
144-
public function getGroup()
143+
*/
144+
public function getGroup(): ?Controller
145145
{
146146
return $this->group;
147147
}
@@ -150,7 +150,7 @@ public function getGroup()
150150
* Set the value of group
151151
*
152152
* @param Controller $group
153-
*/
153+
*/
154154
public function setGroup(Controller $group): void
155155
{
156156
$this->group = $group;

0 commit comments

Comments
 (0)