Skip to content

Commit e12b8bd

Browse files
committed
refactor: add typehints, remove redundant docblocks
1 parent 8214d23 commit e12b8bd

File tree

16 files changed

+59
-368
lines changed

16 files changed

+59
-368
lines changed

src/Application.php

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public function __construct(protected string $name, protected string $version =
5555

5656
/**
5757
* Get the name.
58-
*
59-
* @return string
6058
*/
6159
public function name(): string
6260
{
@@ -65,8 +63,6 @@ public function name(): string
6563

6664
/**
6765
* Get the version.
68-
*
69-
* @return string
7066
*/
7167
public function version(): string
7268
{
@@ -89,8 +85,6 @@ public function commands(): array
8985

9086
/**
9187
* Get the raw argv.
92-
*
93-
* @return array
9488
*/
9589
public function argv(): array
9690
{
@@ -117,14 +111,6 @@ public function logo(string $logo = null)
117111

118112
/**
119113
* Add a command by its name desc alias etc.
120-
*
121-
* @param string $name
122-
* @param string $desc
123-
* @param string $alias
124-
* @param bool $allowUnknown
125-
* @param bool $default
126-
*
127-
* @return Command
128114
*/
129115
public function command(
130116
string $name,
@@ -142,12 +128,6 @@ public function command(
142128

143129
/**
144130
* Add a prepred command.
145-
*
146-
* @param Command $command
147-
* @param string $alias
148-
* @param bool $default
149-
*
150-
* @return self
151131
*/
152132
public function add(Command $command, string $alias = '', bool $default = false): self
153133
{
@@ -179,10 +159,6 @@ public function add(Command $command, string $alias = '', bool $default = false)
179159

180160
/**
181161
* Gets matching command for given argv.
182-
*
183-
* @param array $argv
184-
*
185-
* @return Command
186162
*/
187163
public function commandFor(array $argv): Command
188164
{
@@ -249,12 +225,8 @@ public function parse(array $argv): Command
249225

250226
/**
251227
* Handle the request, invoke action and call exit handler.
252-
*
253-
* @param array $argv
254-
*
255-
* @return mixed
256228
*/
257-
public function handle(array $argv)
229+
public function handle(array $argv): mixed
258230
{
259231
if (\count($argv) < 2) {
260232
return $this->showHelp();
@@ -275,10 +247,6 @@ public function handle(array $argv)
275247

276248
/**
277249
* Get aliases for given command.
278-
*
279-
* @param Command $command
280-
*
281-
* @return array
282250
*/
283251
protected function aliasesFor(Command $command): array
284252
{
@@ -296,10 +264,8 @@ protected function aliasesFor(Command $command): array
296264

297265
/**
298266
* Show help of all commands.
299-
*
300-
* @return mixed
301267
*/
302-
public function showHelp()
268+
public function showHelp(): mixed
303269
{
304270
$writer = $this->io()->writer();
305271
$header = "{$this->name}, version {$this->version}";
@@ -323,12 +289,8 @@ protected function outputHelper(): OutputHelper
323289

324290
/**
325291
* Invoke command action.
326-
*
327-
* @param Command $command
328-
*
329-
* @return mixed
330292
*/
331-
protected function doAction(Command $command)
293+
protected function doAction(Command $command): mixed
332294
{
333295
if ($command->name() === '__default__') {
334296
return $this->notFound();
@@ -338,7 +300,7 @@ protected function doAction(Command $command)
338300
$command->interact($this->io());
339301

340302
if (!$command->action() && !\method_exists($command, 'execute')) {
341-
return;
303+
return null;
342304
}
343305

344306
$params = [];
@@ -355,10 +317,8 @@ protected function doAction(Command $command)
355317

356318
/**
357319
* Command not found handler.
358-
*
359-
* @return mixed
360320
*/
361-
protected function notFound()
321+
protected function notFound(): mixed
362322
{
363323
$available = \array_keys($this->commands() + $this->aliases);
364324
$this->outputHelper()->showCommandNotFound($this->argv[1], $available);

src/Helper/InflectsString.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ trait InflectsString
2323
{
2424
/**
2525
* Convert a string to camel case.
26-
*
27-
* @param string $string
28-
*
29-
* @return string
3026
*/
3127
public function toCamelCase(string $string): string
3228
{
@@ -39,10 +35,6 @@ public function toCamelCase(string $string): string
3935

4036
/**
4137
* Convert a string to capitalized words.
42-
*
43-
* @param string $string
44-
*
45-
* @return string
4638
*/
4739
public function toWords(string $string): string
4840
{

src/Helper/Normalizer.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class Normalizer
2626
{
2727
/**
2828
* Normalize argv args. Like splitting `-abc` and `--xyz=...`.
29-
*
30-
* @param array $args
31-
*
32-
* @return array
3329
*/
3430
public function normalizeArgs(array $args): array
3531
{
@@ -53,13 +49,8 @@ public function normalizeArgs(array $args): array
5349

5450
/**
5551
* Normalizes value as per context and runs thorugh filter if possible.
56-
*
57-
* @param Parameter $parameter
58-
* @param string|null $value
59-
*
60-
* @return mixed
6152
*/
62-
public function normalizeValue(Parameter $parameter, string $value = null)
53+
public function normalizeValue(Parameter $parameter, string $value = null): mixed
6354
{
6455
if ($parameter instanceof Option && $parameter->bool()) {
6556
return !$parameter->default();

src/Helper/OutputHelper.php

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ public function __construct(Writer $writer = null)
4040

4141
/**
4242
* Print stack trace and error msg of an exception.
43-
*
44-
* @param \Throwable $e
45-
*
46-
* @return void
4743
*/
48-
public function printTrace(\Throwable $e)
44+
public function printTrace(\Throwable $e): void
4945
{
5046
$eClass = \get_class($e);
5147

@@ -78,7 +74,7 @@ public function printTrace(\Throwable $e)
7874
$this->writer->colors($traceStr);
7975
}
8076

81-
protected function stringifyArgs(array $args)
77+
protected function stringifyArgs(array $args): string
8278
{
8379
$holder = [];
8480

@@ -89,7 +85,7 @@ protected function stringifyArgs(array $args)
8985
return \implode(', ', $holder);
9086
}
9187

92-
protected function stringifyArg($arg)
88+
protected function stringifyArg($arg): string
9389
{
9490
if (\is_scalar($arg)) {
9591
return \var_export($arg, true);
@@ -152,15 +148,8 @@ public function showCommandsHelp(array $commands, string $header = '', string $f
152148

153149
/**
154150
* Show help with headers and footers.
155-
*
156-
* @param string $for
157-
* @param array $items
158-
* @param string $header
159-
* @param string $footer
160-
*
161-
* @return void
162151
*/
163-
protected function showHelp(string $for, array $items, string $header = '', string $footer = '')
152+
protected function showHelp(string $for, array $items, string $header = '', string $footer = ''): void
164153
{
165154
if ($header) {
166155
$this->writer->bold($header, true);
@@ -192,10 +181,6 @@ protected function showHelp(string $for, array $items, string $header = '', stri
192181
* Show usage examples of a Command.
193182
*
194183
* It replaces $0 with actual command name and properly pads ` ## ` segments.
195-
*
196-
* @param string $usage Usage description.
197-
*
198-
* @return self
199184
*/
200185
public function showUsage(string $usage): self
201186
{
@@ -215,11 +200,6 @@ public function showUsage(string $usage): self
215200
}
216201

217202
$maxlen = ($lines ? \max($lines) : 0) + 4;
218-
// $usage = \preg_replace_callback(
219-
// '~ ## ~',
220-
// fn() => \str_pad('# ', $maxlen - \array_shift($lines), ' ', \STR_PAD_LEFT),
221-
// $usage
222-
// );
223203
$usage = \preg_replace_callback('~ ## ~', function () use (&$lines, $maxlen) {
224204
return \str_pad('# ', $maxlen - \array_shift($lines), ' ', \STR_PAD_LEFT);
225205
}, $usage);
@@ -261,8 +241,6 @@ protected function sortItems(array $items, &$max = 0): array
261241
{
262242
$max = \max(\array_map(fn ($item) => \strlen($this->getName($item)), $items));
263243

264-
/* @var Parameter $b */
265-
/* @var Parameter $a */
266244
\uasort($items, fn ($a, $b) => $a->name() <=> $b->name());
267245

268246
return $items;
@@ -288,12 +266,8 @@ protected function getName($item): string
288266

289267
/**
290268
* Get parameter label for humans.
291-
*
292-
* @param Parameter $item
293-
*
294-
* @return string
295269
*/
296-
protected function label(Parameter $item)
270+
protected function label(Parameter $item): string
297271
{
298272
$name = $item->name();
299273

src/Helper/Shell.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,30 @@ protected function isWindows(): bool
102102
return '\\' === \DIRECTORY_SEPARATOR;
103103
}
104104

105-
protected function setInput()
105+
protected function setInput(): void
106106
{
107107
\fwrite($this->pipes[self::STDIN_DESCRIPTOR_KEY], $this->input);
108108
}
109109

110-
protected function updateProcessStatus()
110+
protected function updateProcessStatus(): void
111111
{
112-
if ($this->state !== self::STATE_STARTED) {
113-
return;
114-
}
112+
if ($this->state === self::STATE_STARTED) {
113+
$this->processStatus = \proc_get_status($this->process);
115114

116-
$this->processStatus = \proc_get_status($this->process);
117-
118-
if ($this->processStatus['running'] === false && $this->exitCode === null) {
119-
$this->exitCode = $this->processStatus['exitcode'];
115+
if ($this->processStatus['running'] === false && $this->exitCode === null) {
116+
$this->exitCode = $this->processStatus['exitcode'];
117+
}
120118
}
121119
}
122120

123-
protected function closePipes()
121+
protected function closePipes(): void
124122
{
125123
\fclose($this->pipes[self::STDIN_DESCRIPTOR_KEY]);
126124
\fclose($this->pipes[self::STDOUT_DESCRIPTOR_KEY]);
127125
\fclose($this->pipes[self::STDERR_DESCRIPTOR_KEY]);
128126
}
129127

130-
protected function wait()
128+
protected function wait(): ?int
131129
{
132130
while ($this->isRunning()) {
133131
usleep(5000);
@@ -137,7 +135,7 @@ protected function wait()
137135
return $this->exitCode;
138136
}
139137

140-
protected function checkTimeout()
138+
protected function checkTimeout(): void
141139
{
142140
if ($this->processTimeout === null) {
143141
return;
@@ -152,7 +150,6 @@ protected function checkTimeout()
152150
}
153151
// @codeCoverageIgnoreStart
154152
}
155-
156153
// @codeCoverageIgnoreEnd
157154

158155
public function setOptions(
@@ -227,7 +224,7 @@ public function getErrorOutput(): string
227224
return \stream_get_contents($this->pipes[self::STDERR_DESCRIPTOR_KEY]);
228225
}
229226

230-
public function getExitCode()
227+
public function getExitCode(): ?int
231228
{
232229
$this->updateProcessStatus();
233230

@@ -245,12 +242,12 @@ public function isRunning(): bool
245242
return $this->processStatus['running'];
246243
}
247244

248-
public function getProcessId()
245+
public function getProcessId(): ?int
249246
{
250247
return $this->isRunning() ? $this->processStatus['pid'] : null;
251248
}
252249

253-
public function stop()
250+
public function stop(): ?int
254251
{
255252
$this->closePipes();
256253

@@ -265,7 +262,7 @@ public function stop()
265262
return $this->exitCode;
266263
}
267264

268-
public function kill()
265+
public function kill(): void
269266
{
270267
if (\is_resource($this->process)) {
271268
\proc_terminate($this->process);

0 commit comments

Comments
 (0)