2121
2222/**
2323 * Core functionality for running, listing, etc commands.
24+ *
25+ * @phpstan-type commands_list array<string, array{'class': class-string<BaseCommand>, 'file': string, 'group': string,'description': string}>
2426 */
2527class Commands
2628{
2729 /**
2830 * The found commands.
2931 *
30- * @var array
32+ * @var commands_list
3133 */
3234 protected $ commands = [];
3335
@@ -52,6 +54,8 @@ public function __construct($logger = null)
5254 /**
5355 * Runs a command given
5456 *
57+ * @param array<int|string, string|null> $params
58+ *
5559 * @return int Exit code
5660 */
5761 public function run (string $ command , array $ params )
@@ -77,7 +81,7 @@ public function run(string $command, array $params)
7781 /**
7882 * Provide access to the list of commands.
7983 *
80- * @return array
84+ * @return commands_list
8185 */
8286 public function getCommands ()
8387 {
@@ -96,7 +100,7 @@ public function discoverCommands()
96100 return ;
97101 }
98102
99- /** @var FileLocatorInterface $locator */
103+ /** @var FileLocatorInterface */
100104 $ locator = service ('locator ' );
101105 $ files = $ locator ->listFiles ('Commands/ ' );
102106
@@ -109,6 +113,7 @@ public function discoverCommands()
109113 // Loop over each file checking to see if a command with that
110114 // alias exists in the class.
111115 foreach ($ files as $ file ) {
116+ /** @var class-string<BaseCommand>|false */
112117 $ className = $ locator ->findQualifiedNameFromPath ($ file );
113118
114119 if ($ className === false || ! class_exists ($ className )) {
@@ -122,7 +127,6 @@ public function discoverCommands()
122127 continue ;
123128 }
124129
125- /** @var BaseCommand $class */
126130 $ class = new $ className ($ this ->logger , $ this );
127131
128132 if (isset ($ class ->group ) && ! isset ($ this ->commands [$ class ->name ])) {
@@ -146,16 +150,18 @@ public function discoverCommands()
146150 /**
147151 * Verifies if the command being sought is found
148152 * in the commands list.
153+ *
154+ * @param commands_list $commands
149155 */
150156 public function verifyCommand (string $ command , array $ commands ): bool
151157 {
152158 if (isset ($ commands [$ command ])) {
153159 return true ;
154160 }
155161
156- $ message = lang ('CLI.commandNotFound ' , [$ command ]);
157-
162+ $ message = lang ('CLI.commandNotFound ' , [$ command ]);
158163 $ alternatives = $ this ->getCommandAlternatives ($ command , $ commands );
164+
159165 if ($ alternatives !== []) {
160166 if (count ($ alternatives ) === 1 ) {
161167 $ message .= "\n\n" . lang ('CLI.altCommandSingular ' ) . "\n " ;
@@ -175,11 +181,17 @@ public function verifyCommand(string $command, array $commands): bool
175181 /**
176182 * Finds alternative of `$name` among collection
177183 * of commands.
184+ *
185+ * @param commands_list $collection
186+ *
187+ * @return list<string>
178188 */
179189 protected function getCommandAlternatives (string $ name , array $ collection ): array
180190 {
191+ /** @var array<string, int> */
181192 $ alternatives = [];
182193
194+ /** @var string $commandName */
183195 foreach (array_keys ($ collection ) as $ commandName ) {
184196 $ lev = levenshtein ($ name , $ commandName );
185197
0 commit comments