Skip to content

Commit bd0a70c

Browse files
committed
refactor(*): use imports instead of FQN
1 parent cbfe911 commit bd0a70c

File tree

17 files changed

+359
-189
lines changed

17 files changed

+359
-189
lines changed

src/Application.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
use Ahc\Cli\Helper\OutputHelper;
1616
use Ahc\Cli\Input\Command;
1717
use Ahc\Cli\IO\Interactor;
18+
use ReflectionClass;
19+
use ReflectionFunction;
20+
use Throwable;
21+
use function array_diff_key;
22+
use function array_fill_keys;
23+
use function array_keys;
24+
use function count;
25+
use function func_num_args;
26+
use function in_array;
27+
use function is_array;
28+
use function is_int;
29+
use function method_exists;
30+
use function sprintf;
1831

1932
/**
2033
* A cli application.
@@ -48,7 +61,7 @@ class Application
4861

4962
public function __construct(protected string $name, protected string $version = '0.0.1', callable $onExit = null)
5063
{
51-
$this->onExit = $onExit ?? fn (int $exitCode = 0) => exit($exitCode);
64+
$this->onExit = $onExit ?? static fn (int $exitCode = 0) => exit($exitCode);
5265

5366
$this->command('__default__', 'Default command', '', true)->on([$this, 'showHelp'], 'help');
5467
}
@@ -100,7 +113,7 @@ public function argv(): array
100113
*/
101114
public function logo(string $logo = null)
102115
{
103-
if (\func_num_args() === 0) {
116+
if (func_num_args() === 0) {
104117
return $this->logo;
105118
}
106119

@@ -140,7 +153,7 @@ public function add(Command $command, string $alias = '', bool $default = false)
140153
$this->aliases[$alias] ??
141154
null
142155
) {
143-
throw new InvalidArgumentException(\sprintf('Command "%s" already added', $name));
156+
throw new InvalidArgumentException(sprintf('Command "%s" already added', $name));
144157
}
145158

146159
if ($alias) {
@@ -206,7 +219,7 @@ public function io(Interactor $io = null)
206219
$this->io = $io ?? new Interactor;
207220
}
208221

209-
if (\func_num_args() === 0) {
222+
if (func_num_args() === 0) {
210223
return $this->io;
211224
}
212225

@@ -229,7 +242,7 @@ public function parse(array $argv): Command
229242

230243
// Eat the cmd name!
231244
foreach ($argv as $i => $arg) {
232-
if (\in_array($arg, $aliases)) {
245+
if (in_array($arg, $aliases)) {
233246
unset($argv[$i]);
234247

235248
break;
@@ -248,7 +261,7 @@ public function parse(array $argv): Command
248261
*/
249262
public function handle(array $argv): mixed
250263
{
251-
if (\count($argv) < 2) {
264+
if (count($argv) < 2) {
252265
return $this->showHelp();
253266
}
254267

@@ -257,8 +270,8 @@ public function handle(array $argv): mixed
257270
try {
258271
$command = $this->parse($argv);
259272
$result = $this->doAction($command);
260-
$exitCode = \is_int($result) ? $result : 0;
261-
} catch (\Throwable $e) {
273+
$exitCode = is_int($result) ? $result : 0;
274+
} catch (Throwable $e) {
262275
$this->outputHelper()->printTrace($e);
263276
}
264277

@@ -272,10 +285,10 @@ protected function aliasesFor(Command $command): array
272285
{
273286
$aliases = [$name = $command->name()];
274287

275-
foreach ($this->aliases as $alias => $command) {
276-
if (\in_array($name, [$alias, $command])) {
288+
foreach ($this->aliases as $alias => $cmd) {
289+
if (in_array($name, [$alias, $cmd], true)) {
277290
$aliases[] = $alias;
278-
$aliases[] = $command;
291+
$aliases[] = $cmd;
279292
}
280293
}
281294

@@ -319,7 +332,7 @@ protected function doAction(Command $command): mixed
319332
// Let the command collect more data (if missing or needs confirmation)
320333
$command->interact($this->io());
321334

322-
if (!$command->action() && !\method_exists($command, 'execute')) {
335+
if (!$command->action() && !method_exists($command, 'execute')) {
323336
return null;
324337
}
325338

@@ -340,17 +353,17 @@ protected function doAction(Command $command): mixed
340353
*/
341354
protected function notFound(): mixed
342355
{
343-
$available = \array_keys($this->commands() + $this->aliases);
356+
$available = array_keys($this->commands() + $this->aliases);
344357
$this->outputHelper()->showCommandNotFound($this->argv[1], $available);
345358

346359
return ($this->onExit)(127);
347360
}
348361

349362
protected function getActionParameters(callable $action): array
350363
{
351-
$reflex = \is_array($action)
352-
? (new \ReflectionClass($action[0]))->getMethod($action[1])
353-
: new \ReflectionFunction($action);
364+
$reflex = is_array($action)
365+
? (new ReflectionClass($action[0]))->getMethod($action[1])
366+
: new ReflectionFunction($action);
354367

355368
return $reflex->getParameters();
356369
}

src/Exception.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Ahc\Cli;
1313

14-
interface Exception extends \Throwable
14+
use Throwable;
15+
16+
interface Exception extends Throwable
1517
{
1618
// ;)
1719
}

src/Helper/InflectsString.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
namespace Ahc\Cli\Helper;
1313

14+
use function lcfirst;
15+
use function str_replace;
16+
use function trim;
17+
use function ucwords;
18+
1419
/**
1520
* Performs inflection on strings.
1621
*
@@ -26,20 +31,20 @@ trait InflectsString
2631
*/
2732
public function toCamelCase(string $string): string
2833
{
29-
$words = \str_replace(['-', '_'], ' ', $string);
34+
$words = str_replace(['-', '_'], ' ', $string);
3035

31-
$words = \str_replace(' ', '', \ucwords($words));
36+
$words = str_replace(' ', '', ucwords($words));
3237

33-
return \lcfirst($words);
38+
return lcfirst($words);
3439
}
3540

3641
/**
3742
* Convert a string to capitalized words.
3843
*/
3944
public function toWords(string $string): string
4045
{
41-
$words = \trim(\str_replace(['-', '_'], ' ', $string));
46+
$words = trim(str_replace(['-', '_'], ' ', $string));
4247

43-
return \ucwords($words);
48+
return ucwords($words);
4449
}
4550
}

src/Helper/Normalizer.php

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

1414
use Ahc\Cli\Input\Option;
1515
use Ahc\Cli\Input\Parameter;
16+
use function array_merge;
17+
use function explode;
18+
use function implode;
19+
use function ltrim;
20+
use function preg_match;
21+
use function str_split;
1622

1723
/**
1824
* Internal value &/or argument normalizer. Has little to no usefulness as public api.
@@ -32,13 +38,13 @@ public function normalizeArgs(array $args): array
3238
$normalized = [];
3339

3440
foreach ($args as $arg) {
35-
if (\preg_match('/^\-\w=/', $arg)) {
36-
$normalized = \array_merge($normalized, explode('=', $arg));
37-
} elseif (\preg_match('/^\-\w{2,}/', $arg)) {
38-
$splitArg = \implode(' -', \str_split(\ltrim($arg, '-')));
39-
$normalized = \array_merge($normalized, \explode(' ', '-' . $splitArg));
40-
} elseif (\preg_match('/^\-\-([^\s\=]+)\=/', $arg)) {
41-
$normalized = \array_merge($normalized, explode('=', $arg));
41+
if (preg_match('/^\-\w=/', $arg)) {
42+
$normalized = array_merge($normalized, explode('=', $arg));
43+
} elseif (preg_match('/^\-\w{2,}/', $arg)) {
44+
$splitArg = implode(' -', str_split(ltrim($arg, '-')));
45+
$normalized = array_merge($normalized, explode(' ', '-' . $splitArg));
46+
} elseif (preg_match('/^\-\-([^\s\=]+)\=/', $arg)) {
47+
$normalized = array_merge($normalized, explode('=', $arg));
4248
} else {
4349
$normalized[] = $arg;
4450
}

src/Helper/OutputHelper.php

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,37 @@
1414
use Ahc\Cli\Exception;
1515
use Ahc\Cli\Input\Argument;
1616
use Ahc\Cli\Input\Command;
17+
use Ahc\Cli\Input\Groupable;
1718
use Ahc\Cli\Input\Option;
1819
use Ahc\Cli\Input\Parameter;
1920
use Ahc\Cli\Output\Writer;
21+
use Throwable;
22+
use function array_map;
23+
use function array_shift;
24+
use function asort;
25+
use function explode;
26+
use function get_class;
27+
use function gettype;
28+
use function implode;
29+
use function is_array;
30+
use function is_object;
31+
use function is_scalar;
32+
use function key;
33+
use function levenshtein;
34+
use function max;
35+
use function method_exists;
36+
use function preg_replace;
37+
use function preg_replace_callback;
38+
use function realpath;
39+
use function str_contains;
40+
use function str_pad;
41+
use function str_replace;
42+
use function strlen;
43+
use function strrpos;
44+
use function trim;
45+
use function uasort;
46+
use function var_export;
47+
use const STR_PAD_LEFT;
2048

2149
/**
2250
* This helper helps you by showing you help information :).
@@ -41,9 +69,9 @@ public function __construct(Writer $writer = null)
4169
/**
4270
* Print stack trace and error msg of an exception.
4371
*/
44-
public function printTrace(\Throwable $e): void
72+
public function printTrace(Throwable $e): void
4573
{
46-
$eClass = \get_class($e);
74+
$eClass = get_class($e);
4775

4876
$this->writer->colors(
4977
"{$eClass} <red>{$e->getMessage()}</end><eol/>" .
@@ -66,7 +94,7 @@ public function printTrace(\Throwable $e): void
6694

6795
$traceStr .= " <comment>$i)</end> <red>$symbol</end><comment>($args)</end>";
6896
if ('' !== $trace['file']) {
69-
$file = \realpath($trace['file']);
97+
$file = realpath($trace['file']);
7098
$traceStr .= "<eol/> <yellow>at $file</end><white>:{$trace['line']}</end><eol/>";
7199
}
72100
}
@@ -82,24 +110,24 @@ protected function stringifyArgs(array $args): string
82110
$holder[] = $this->stringifyArg($arg);
83111
}
84112

85-
return \implode(', ', $holder);
113+
return implode(', ', $holder);
86114
}
87115

88116
protected function stringifyArg($arg): string
89117
{
90-
if (\is_scalar($arg)) {
91-
return \var_export($arg, true);
118+
if (is_scalar($arg)) {
119+
return var_export($arg, true);
92120
}
93121

94-
if (\is_object($arg)) {
95-
return \method_exists($arg, '__toString') ? (string) $arg : \get_class($arg);
122+
if (is_object($arg)) {
123+
return method_exists($arg, '__toString') ? (string) $arg : get_class($arg);
96124
}
97125

98-
if (\is_array($arg)) {
126+
if (is_array($arg)) {
99127
return '[' . $this->stringifyArgs($arg) . ']';
100128
}
101129

102-
return \gettype($arg);
130+
return gettype($arg);
103131
}
104132

105133
/**
@@ -139,7 +167,7 @@ public function showOptionsHelp(array $options, string $header = '', string $foo
139167
*/
140168
public function showCommandsHelp(array $commands, string $header = '', string $footer = ''): self
141169
{
142-
$this->maxCmdName = $commands ? \max(\array_map(fn (Command $cmd) => \strlen($cmd->name()), $commands)) : 0;
170+
$this->maxCmdName = $commands ? max(array_map(static fn (Command $cmd) => strlen($cmd->name()), $commands)) : 0;
143171

144172
$this->showHelp('Commands', $commands, $header, $footer);
145173

@@ -189,24 +217,24 @@ protected function showHelp(string $for, array $items, string $header = '', stri
189217
*/
190218
public function showUsage(string $usage): self
191219
{
192-
$usage = \str_replace('$0', $_SERVER['argv'][0] ?? '[cmd]', $usage);
220+
$usage = str_replace('$0', $_SERVER['argv'][0] ?? '[cmd]', $usage);
193221

194-
if (!\str_contains($usage, ' ## ')) {
222+
if (!str_contains($usage, ' ## ')) {
195223
$this->writer->eol()->boldGreen('Usage Examples:', true)->colors($usage)->eol();
196224

197225
return $this;
198226
}
199227

200-
$lines = \explode("\n", \str_replace(['<eol>', '<eol/>', '</eol>', "\r\n"], "\n", $usage));
228+
$lines = explode("\n", str_replace(['<eol>', '<eol/>', '</eol>', "\r\n"], "\n", $usage));
201229
foreach ($lines as $i => &$pos) {
202-
if (false === $pos = \strrpos(\preg_replace('~</?\w+/?>~', '', $pos), ' ##')) {
230+
if (false === $pos = strrpos(preg_replace('~</?\w+/?>~', '', $pos), ' ##')) {
203231
unset($lines[$i]);
204232
}
205233
}
206234

207-
$maxlen = ($lines ? \max($lines) : 0) + 4;
208-
$usage = \preg_replace_callback('~ ## ~', function () use (&$lines, $maxlen) {
209-
return \str_pad('# ', $maxlen - \array_shift($lines), ' ', \STR_PAD_LEFT);
235+
$maxlen = ($lines ? max($lines) : 0) + 4;
236+
$usage = preg_replace_callback('~ ## ~', function () use (&$lines, $maxlen) {
237+
return str_pad('# ', $maxlen - array_shift($lines), ' ', STR_PAD_LEFT);
210238
}, $usage);
211239

212240
$this->writer->eol()->boldGreen('Usage Examples:', true)->colors($usage)->eol();
@@ -218,16 +246,16 @@ public function showCommandNotFound(string $attempted, array $available): self
218246
{
219247
$closest = [];
220248
foreach ($available as $cmd) {
221-
$lev = \levenshtein($attempted, $cmd);
249+
$lev = levenshtein($attempted, $cmd);
222250
if ($lev > 0 || $lev < 5) {
223251
$closest[$cmd] = $lev;
224252
}
225253
}
226254

227255
$this->writer->error("Command $attempted not found", true);
228256
if ($closest) {
229-
\asort($closest);
230-
$closest = \key($closest);
257+
asort($closest);
258+
$closest = key($closest);
231259
$this->writer->bgRed("Did you mean $closest?", true);
232260
}
233261

@@ -268,7 +296,7 @@ protected function getName($item): string
268296
$name = $item->name();
269297

270298
if ($item instanceof Command) {
271-
return \trim(\str_pad($name, $this->maxCmdName) . ' ' . $item->alias());
299+
return trim(str_pad($name, $this->maxCmdName) . ' ' . $item->alias());
272300
}
273301

274302
return $this->label($item);

0 commit comments

Comments
 (0)