@@ -16,23 +16,16 @@ class CLI
1616 * Adapter
1717 *
1818 * Type of adapter to pass the callable to
19- *
20- * @var Adapter
2119 */
2220 protected Adapter $ adapter ;
2321
2422 /**
2523 * Command
2624 *
2725 * The name of the command requested for this process
28- *
29- * @var string
3026 */
3127 protected string $ command = '' ;
3228
33- /**
34- * @var Container
35- */
3629 protected Container $ container ;
3730
3831 protected ?Container $ parentContainer = null ;
@@ -41,17 +34,13 @@ class CLI
4134 * Args
4235 *
4336 * List of arguments passed to this process
44- *
45- * @var array
4637 */
4738 protected array $ args = [];
4839
4940 /**
5041 * Tasks
5142 *
5243 * List of commands tasks for this CLI process
53- *
54- * @var array
5544 */
5645 protected array $ tasks = [];
5746
@@ -85,9 +74,6 @@ class CLI
8574 /**
8675 * CLI constructor.
8776 *
88- * @param Adapter|null $adapter
89- * @param array $args
90- * @param Container|null $container
9177 *
9278 * @throws Exception
9379 */
@@ -97,7 +83,7 @@ public function __construct(?Adapter $adapter = null, array $args = [], ?Contain
9783 throw new Exception ('CLI tasks can only work from the command line ' );
9884 }
9985
100- $ this ->args = $ this ->parse ((! empty ( $ args) || ! isset ($ _SERVER ['argv ' ])) ? $ args : $ _SERVER ['argv ' ]);
86+ $ this ->args = $ this ->parse (($ args !== [] || ! isset ($ _SERVER ['argv ' ])) ? $ args : $ _SERVER ['argv ' ]);
10187
10288 @cli_set_process_title ($ this ->command );
10389
@@ -110,8 +96,6 @@ public function __construct(?Adapter $adapter = null, array $args = [], ?Contain
11096 * Init
11197 *
11298 * Set a callback function that will be initialized on application start
113- *
114- * @return Hook
11599 */
116100 public function init (): Hook
117101 {
@@ -125,8 +109,6 @@ public function init(): Hook
125109 * Shutdown
126110 *
127111 * Set a callback function that will be initialized on application end
128- *
129- * @return Hook
130112 */
131113 public function shutdown (): Hook
132114 {
@@ -140,8 +122,6 @@ public function shutdown(): Hook
140122 * Error
141123 *
142124 * An error callback for failed or no matched requests
143- *
144- * @return Hook
145125 */
146126 public function error (): Hook
147127 {
@@ -155,9 +135,6 @@ public function error(): Hook
155135 * Task
156136 *
157137 * Add a new command task
158- *
159- * @param string $name
160- * @return Task
161138 */
162139 public function task (string $ name ): Task
163140 {
@@ -171,8 +148,6 @@ public function task(string $name): Task
171148 /**
172149 * If a resource has been created return it, otherwise create it and then return it
173150 *
174- * @param string $name
175- * @return mixed
176151 *
177152 * @throws Exception
178153 */
@@ -188,8 +163,6 @@ public function getResource(string $name): mixed
188163 /**
189164 * Get Resources By List
190165 *
191- * @param array $list
192- * @return array
193166 *
194167 * @throws Exception
195168 */
@@ -207,10 +180,6 @@ public function getResources(array $list): array
207180 /**
208181 * Set a new resource callback
209182 *
210- * @param string $name
211- * @param callable $callback
212- * @param array $dependencies
213- * @return void
214183 *
215184 * @throws Exception
216185 */
@@ -227,8 +196,6 @@ public function getContainer(): Container
227196 /**
228197 * task-name --foo=test
229198 *
230- * @param array $args
231- * @return array
232199 *
233200 * @throws Exception
234201 */
@@ -268,7 +235,7 @@ public function parse(array $args): array
268235 * If there is only one element in a particular key
269236 * unshift the value out of the array
270237 */
271- if (\count ($ value ) == 1 ) {
238+ if (\count ($ value ) === 1 ) {
272239 $ output [$ key ] = array_shift ($ output [$ key ]);
273240 }
274241 }
@@ -278,8 +245,6 @@ public function parse(array $args): array
278245
279246 /**
280247 * Find the command that should be triggered
281- *
282- * @return Task|null
283248 */
284249 public function match (): ?Task
285250 {
@@ -290,8 +255,6 @@ public function match(): ?Task
290255 * Get Params
291256 * Get runtime params for the provided Hook
292257 *
293- * @param Hook $hook
294- * @return array
295258 *
296259 * @throws Exception
297260 */
@@ -338,11 +301,11 @@ protected function getParams(Hook $hook): array
338301 */
339302 public function run (): self
340303 {
341- $ this ->adapter ->start (function () {
304+ $ this ->adapter ->start (function (): void {
342305 $ command = $ this ->match ();
343306
344307 try {
345- if ($ command ) {
308+ if ($ command instanceof \ Utopia \ CLI \Task ) {
346309 foreach ($ this ->init as $ hook ) {
347310 \call_user_func_array ($ hook ->getAction (), $ this ->getParams ($ hook ));
348311 }
@@ -358,7 +321,7 @@ public function run(): self
358321 }
359322 } catch (Exception $ e ) {
360323 foreach ($ this ->errors as $ hook ) {
361- $ this ->setResource ('error ' , fn () => $ e );
324+ $ this ->setResource ('error ' , fn (): \ Exception => $ e );
362325 \call_user_func_array ($ hook ->getAction (), $ this ->getParams ($ hook ));
363326 }
364327 }
@@ -379,8 +342,6 @@ public function getTasks(): array
379342
380343 /**
381344 * Get list of all args
382- *
383- * @return array
384345 */
385346 public function getArgs (): array
386347 {
@@ -392,8 +353,6 @@ public function getArgs(): array
392353 *
393354 * Creates an validator instance and validate given value with given rules.
394355 *
395- * @param string $key
396- * @param array $param
397356 * @param mixed $value
398357 *
399358 * @throws Exception
@@ -403,23 +362,18 @@ protected function validate(string $key, array $param, $value): void
403362 if ('' !== $ value ) {
404363 // checking whether the class exists
405364 $ validator = $ param ['validator ' ];
406-
407365 if (\is_callable ($ validator )) {
408366 $ validator = $ validator ();
409367 }
410-
411368 // is the validator object an instance of the Validator class
412369 if (! $ validator instanceof Validator) {
413370 throw new Exception ('Validator object is not an instance of the Validator class ' , 500 );
414371 }
415-
416372 if (! $ validator ->isValid ($ value )) {
417373 throw new Exception ('Invalid ' . $ key . ': ' . $ validator ->getDescription (), 400 );
418374 }
419- } else {
420- if (! $ param ['optional ' ]) {
421- throw new Exception ('Param " ' . $ key . '" is not optional. ' , 400 );
422- }
375+ } elseif (! $ param ['optional ' ]) {
376+ throw new Exception ('Param " ' . $ key . '" is not optional. ' , 400 );
423377 }
424378 }
425379
@@ -439,10 +393,6 @@ protected function validate(string $key, array $param, $value): void
439393 * empty string, which bypasses `validate()` for optional params) are
440394 * passed through unchanged so callers that use `''` as a sentinel for
441395 * "not set" keep working.
442- *
443- * @param Validator|callable $validator
444- * @param mixed $value
445- * @return mixed
446396 */
447397 protected function coerce (Validator |callable $ validator , mixed $ value ): mixed
448398 {
@@ -464,7 +414,7 @@ protected function coerce(Validator|callable $validator, mixed $value): mixed
464414
465415 $ coerced = filter_var ($ value , FILTER_VALIDATE_BOOLEAN , FILTER_NULL_ON_FAILURE );
466416
467- return $ coerced === null ? $ value : $ coerced ;
417+ return $ coerced ?? $ value ;
468418 }
469419
470420 public function setContainer (Container $ container ): self
@@ -483,8 +433,7 @@ public function reset(): void
483433 private function camelCaseIt ($ key ): string
484434 {
485435 $ key = str_replace ('- ' , '_ ' , $ key );
486- $ camelCase = lcfirst (str_replace ('_ ' , '' , ucwords ($ key , '_ ' )));
487436
488- return $ camelCase ;
437+ return lcfirst ( str_replace ( ' _ ' , '' , ucwords ( $ key , ' _ ' ))) ;
489438 }
490439}
0 commit comments