1414use Ahc \Cli \Exception ;
1515use Ahc \Cli \Input \Argument ;
1616use Ahc \Cli \Input \Command ;
17+ use Ahc \Cli \Input \Groupable ;
1718use Ahc \Cli \Input \Option ;
1819use Ahc \Cli \Input \Parameter ;
1920use 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