2121use SoloTerm \Solo \Support \KeyPressListener ;
2222use SplQueue ;
2323
24+ /** @phpstan-consistent-constructor */
2425class Command implements Loopable
2526{
2627 use ManagesProcess, Ticks;
@@ -43,6 +44,7 @@ class Command implements Loopable
4344
4445 public int $ scrollIndex = 0 ;
4546
47+ /** @var SplQueue<string> */
4648 public SplQueue $ lines ;
4749
4850 public int $ height = 0 ;
@@ -108,6 +110,9 @@ public function boot(): void
108110 //
109111 }
110112
113+ /**
114+ * @return array<int|string, Hotkey>
115+ */
111116 public function allHotkeys (): array
112117 {
113118 // In interactive mode, the only hotkey that works is
@@ -125,7 +130,7 @@ public function allHotkeys(): array
125130 $ hotkeys ['interactive ' ] = Hotkey::make ('i ' , KeyHandler::Interactive)->label ('Enter interactive mode ' );
126131 }
127132
128- return array_filter ( $ hotkeys) ;
133+ return $ hotkeys ;
129134 }
130135
131136 /**
@@ -138,7 +143,7 @@ public function hotkeys(): array
138143 ];
139144 }
140145
141- public function setDimensions ($ width , $ height ): static
146+ public function setDimensions (int $ width , int $ height ): static
142147 {
143148 $ existingOutput = isset ($ this ->screen ) ? $ this ->renderScreenBuffer () : null ;
144149
@@ -228,12 +233,12 @@ public function isInteractive(): bool
228233 | Actions
229234 |--------------------------------------------------------------------------
230235 */
231- public function dd ()
236+ public function dd (): never
232237 {
233238 dd ($ this ->screen ->printable ->buffer );
234239 }
235240
236- public function addOutput ($ text )
241+ public function addOutput (string $ text ): void
237242 {
238243 if ($ this ->expectsOutputMarkers ) {
239244 $ text = Str::before ($ text , $ this ->outputEndMarker );
@@ -243,7 +248,7 @@ public function addOutput($text)
243248 $ this ->screen ->write ($ text );
244249 }
245250
246- public function addLine ($ line )
251+ public function addLine (string $ line ): void
247252 {
248253 $ this ->screen ->writeln ($ line );
249254 }
@@ -301,15 +306,15 @@ public function catchUpScroll(): void
301306 }
302307 }
303308
304- public function scrollTo ($ index ): void
309+ public function scrollTo (int | float $ index ): void
305310 {
306311 $ this ->scrollIndex = max (0 , min (
307312 $ index ,
308313 $ this ->wrappedLines ()->count () - $ this ->scrollPaneHeight ()
309314 ));
310315 }
311316
312- public function scrollDown ($ amount = 1 ): void
317+ public function scrollDown (int | float $ amount = 1 ): void
313318 {
314319 $ this ->paused = true ;
315320 $ this ->scrollTo ($ this ->scrollIndex + $ amount );
@@ -320,18 +325,18 @@ public function scrollToBottom(): void
320325 $ this ->scrollDown (INF );
321326 }
322327
323- public function pageDown ()
328+ public function pageDown (): void
324329 {
325330 $ this ->scrollDown ($ this ->scrollPaneHeight () - 1 );
326331 }
327332
328- public function scrollUp ($ amount = 1 ): void
333+ public function scrollUp (int | float $ amount = 1 ): void
329334 {
330335 $ this ->paused = true ;
331336 $ this ->scrollTo ($ this ->scrollIndex - $ amount );
332337 }
333338
334- public function pageUp ()
339+ public function pageUp (): void
335340 {
336341 $ this ->scrollUp ($ this ->scrollPaneHeight () - 1 );
337342 }
@@ -361,6 +366,9 @@ public function scrollPaneWidth(): int
361366 return max (1 , $ this ->width - 4 );
362367 }
363368
369+ /**
370+ * @return Collection<int, string>
371+ */
364372 public function wrappedLines (): Collection
365373 {
366374 // Check if the screen has changed since last render
@@ -409,7 +417,7 @@ protected function renderScreenBuffer(): string
409417 return implode (PHP_EOL , $ lines );
410418 }
411419
412- protected function makeNewScreen ()
420+ protected function makeNewScreen (): Screen
413421 {
414422 $ screen = new Screen (
415423 width: $ this ->scrollPaneWidth (),
@@ -421,7 +429,10 @@ protected function makeNewScreen()
421429 });
422430 }
423431
424- public function wrapLine ($ line , $ width = null , $ continuationIndent = 0 , $ recursive = false ): array
432+ /**
433+ * @return array<int, string>
434+ */
435+ public function wrapLine (string $ line , ?int $ width = null , int $ continuationIndent = 0 , bool $ recursive = false ): array
425436 {
426437 $ defaultWidth = $ this ->scrollPaneWidth ();
427438
@@ -446,15 +457,13 @@ public function wrapLine($line, $width = null, $continuationIndent = 0, $recursi
446457 $ first = array_shift ($ exploded );
447458 $ indent = str_repeat (' ' , $ continuationIndent );
448459
449- if ($ continuationIndent ) {
450- $ allIndented = true ;
451- foreach ($ exploded as $ continuationLine ) {
452- $ allIndented = $ allIndented && str_starts_with ($ continuationLine , $ indent );
453- }
460+ $ allIndented = true ;
461+ foreach ($ exploded as $ continuationLine ) {
462+ $ allIndented = $ allIndented && str_starts_with ($ continuationLine , $ indent );
463+ }
454464
455- if ($ allIndented ) {
456- return [$ first , ...$ exploded ];
457- }
465+ if ($ allIndented ) {
466+ return [$ first , ...$ exploded ];
458467 }
459468
460469 $ rest = $ indent . ltrim (implode ('' , $ exploded ));
@@ -465,13 +474,20 @@ public function wrapLine($line, $width = null, $continuationIndent = 0, $recursi
465474 ];
466475 }
467476
477+ /**
478+ * @param Collection<int, string> $lines
479+ * @return Collection<int, string>
480+ */
468481 protected function modifyWrappedLines (Collection $ lines ): Collection
469482 {
470483 // Primarily here for any subclasses.
471484 return $ lines ;
472485 }
473486
474- public static function __set_state (array $ data )
487+ /**
488+ * @param array<string, mixed> $data
489+ */
490+ public static function __set_state (array $ data ): static
475491 {
476492 $ instance = new static ;
477493
0 commit comments