15
15
use PhpSchool \CliMenu \Util \StringUtil as s ;
16
16
17
17
/**
18
- * Class CliMenu
19
- *
20
- * @package PhpSchool\CliMenu
21
18
* @author Michael Woodward <[email protected] >
22
19
*/
23
20
class CliMenu
@@ -33,7 +30,7 @@ class CliMenu
33
30
protected $ style ;
34
31
35
32
/**
36
- * @var string
33
+ * @var ? string
37
34
*/
38
35
protected $ title ;
39
36
@@ -58,20 +55,12 @@ class CliMenu
58
55
protected $ parent ;
59
56
60
57
/**
61
- * @var Frame|null
58
+ * @var Frame
62
59
*/
63
60
private $ currentFrame ;
64
61
65
- /**
66
- * @param string $title
67
- * @param array $items
68
- * @param TerminalInterface|null $terminal
69
- * @param MenuStyle|null $style
70
- *
71
- * @throws InvalidTerminalException
72
- */
73
62
public function __construct (
74
- $ title ,
63
+ ? string $ title ,
75
64
array $ items ,
76
65
TerminalInterface $ terminal = null ,
77
66
MenuStyle $ style = null
@@ -89,7 +78,7 @@ public function __construct(
89
78
*
90
79
* @throws InvalidTerminalException
91
80
*/
92
- protected function configureTerminal ()
81
+ protected function configureTerminal () : void
93
82
{
94
83
$ this ->assertTerminalIsValidTTY ();
95
84
@@ -103,15 +92,15 @@ protected function configureTerminal()
103
92
*
104
93
* @throws InvalidTerminalException
105
94
*/
106
- protected function tearDownTerminal ()
95
+ protected function tearDownTerminal () : void
107
96
{
108
97
$ this ->assertTerminalIsValidTTY ();
109
98
110
99
$ this ->terminal ->setCanonicalMode (false );
111
100
$ this ->terminal ->enableCursor ();
112
101
}
113
102
114
- private function assertTerminalIsValidTTY ()
103
+ private function assertTerminalIsValidTTY () : void
115
104
{
116
105
if (!$ this ->terminal ->isTTY ()) {
117
106
throw new InvalidTerminalException (
@@ -120,44 +109,30 @@ private function assertTerminalIsValidTTY()
120
109
}
121
110
}
122
111
123
- /**
124
- * @param CliMenu $parent
125
- */
126
- public function setParent (CliMenu $ parent )
112
+ public function setParent (CliMenu $ parent ) : void
127
113
{
128
114
$ this ->parent = $ parent ;
129
115
}
130
116
131
- /**
132
- * @return CliMenu|null
133
- */
134
- public function getParent ()
117
+ public function getParent () : ?CliMenu
135
118
{
136
119
return $ this ->parent ;
137
120
}
138
121
139
- /**
140
- * @return TerminalInterface
141
- */
142
- public function getTerminal ()
122
+ public function getTerminal () : TerminalInterface
143
123
{
144
124
return $ this ->terminal ;
145
125
}
146
126
147
- /**
148
- * @return bool
149
- */
150
- public function isOpen ()
127
+ public function isOpen () : bool
151
128
{
152
129
return $ this ->open ;
153
130
}
154
131
155
132
/**
156
- * Add a new Item to the listing
157
- *
158
- * @param MenuItemInterface $item
133
+ * Add a new Item to the menu
159
134
*/
160
- public function addItem (MenuItemInterface $ item )
135
+ public function addItem (MenuItemInterface $ item ) : void
161
136
{
162
137
$ this ->items [] = $ item ;
163
138
@@ -169,7 +144,7 @@ public function addItem(MenuItemInterface $item)
169
144
/**
170
145
* Set the selected pointer to the first selectable item
171
146
*/
172
- private function selectFirstItem ()
147
+ private function selectFirstItem () : void
173
148
{
174
149
foreach ($ this ->items as $ key => $ item ) {
175
150
if ($ item ->canSelect ()) {
@@ -182,7 +157,7 @@ private function selectFirstItem()
182
157
/**
183
158
* Display menu and capture input
184
159
*/
185
- private function display ()
160
+ private function display () : void
186
161
{
187
162
$ this ->draw ();
188
163
@@ -202,10 +177,8 @@ private function display()
202
177
203
178
/**
204
179
* Move the selection in a given direction, up / down
205
- *
206
- * @param $direction
207
180
*/
208
- protected function moveSelection ($ direction )
181
+ protected function moveSelection (string $ direction ) : void
209
182
{
210
183
do {
211
184
$ itemKeys = array_keys ($ this ->items );
@@ -224,18 +197,15 @@ protected function moveSelection($direction)
224
197
} while (!$ this ->getSelectedItem ()->canSelect ());
225
198
}
226
199
227
- /**
228
- * @return MenuItemInterface
229
- */
230
- public function getSelectedItem ()
200
+ public function getSelectedItem () : MenuItemInterface
231
201
{
232
202
return $ this ->items [$ this ->selectedItem ];
233
203
}
234
204
235
205
/**
236
206
* Execute the current item
237
207
*/
238
- protected function executeCurrentItem ()
208
+ protected function executeCurrentItem () : void
239
209
{
240
210
$ item = $ this ->getSelectedItem ();
241
211
@@ -248,7 +218,7 @@ protected function executeCurrentItem()
248
218
/**
249
219
* Redraw the menu
250
220
*/
251
- public function redraw ()
221
+ public function redraw () : void
252
222
{
253
223
if (!$ this ->isOpen ()) {
254
224
throw new MenuNotOpenException ;
@@ -260,7 +230,7 @@ public function redraw()
260
230
/**
261
231
* Draw the menu to STDOUT
262
232
*/
263
- protected function draw ()
233
+ protected function draw () : void
264
234
{
265
235
$ this ->terminal ->clean ();
266
236
$ this ->terminal ->moveCursorToTop ();
@@ -269,7 +239,7 @@ protected function draw()
269
239
270
240
$ frame ->newLine (2 );
271
241
272
- if (is_string ( $ this ->title ) ) {
242
+ if ($ this ->title ) {
273
243
$ frame ->addRows ($ this ->drawMenuItem (new LineBreakItem ()));
274
244
$ frame ->addRows ($ this ->drawMenuItem (new StaticItem ($ this ->title )));
275
245
$ frame ->addRows ($ this ->drawMenuItem (new LineBreakItem ($ this ->style ->getTitleSeparator ())));
@@ -292,12 +262,8 @@ protected function draw()
292
262
293
263
/**
294
264
* Draw a menu item
295
- *
296
- * @param MenuItemInterface $item
297
- * @param bool|false $selected
298
- * @return array
299
265
*/
300
- protected function drawMenuItem (MenuItemInterface $ item , $ selected = false )
266
+ protected function drawMenuItem (MenuItemInterface $ item , bool $ selected = false ) : array
301
267
{
302
268
$ rows = $ item ->getRows ($ this ->style , $ selected );
303
269
@@ -326,7 +292,7 @@ protected function drawMenuItem(MenuItemInterface $item, $selected = false)
326
292
/**
327
293
* @throws InvalidTerminalException
328
294
*/
329
- public function open ()
295
+ public function open () : void
330
296
{
331
297
if ($ this ->isOpen ()) {
332
298
return ;
@@ -342,7 +308,7 @@ public function open()
342
308
*
343
309
* @throws InvalidTerminalException
344
310
*/
345
- public function close ()
311
+ public function close () : void
346
312
{
347
313
$ menu = $ this ;
348
314
@@ -354,10 +320,7 @@ public function close()
354
320
$ this ->tearDownTerminal ();
355
321
}
356
322
357
- /**
358
- * @throws InvalidTerminalException
359
- */
360
- public function closeThis ()
323
+ public function closeThis () : void
361
324
{
362
325
$ this ->terminal ->clean ();
363
326
$ this ->terminal ->moveCursorToTop ();
@@ -367,17 +330,14 @@ public function closeThis()
367
330
/**
368
331
* @return MenuItemInterface[]
369
332
*/
370
- public function getItems ()
333
+ public function getItems () : array
371
334
{
372
335
return $ this ->items ;
373
336
}
374
337
375
- /**
376
- * @param MenuItemInterface $item
377
- */
378
- public function removeItem (MenuItemInterface $ item )
338
+ public function removeItem (MenuItemInterface $ item ) : void
379
339
{
380
- $ key = array_search ($ item , $ this ->items );
340
+ $ key = array_search ($ item , $ this ->items , true );
381
341
382
342
if (false === $ key ) {
383
343
throw new \InvalidArgumentException ('Item does not exist in menu ' );
@@ -387,24 +347,17 @@ public function removeItem(MenuItemInterface $item)
387
347
$ this ->items = array_values ($ this ->items );
388
348
}
389
349
390
- /**
391
- * @return MenuStyle
392
- */
393
- public function getStyle ()
350
+ public function getStyle () : MenuStyle
394
351
{
395
352
return $ this ->style ;
396
353
}
397
354
398
- public function getCurrentFrame ()
355
+ public function getCurrentFrame () : Frame
399
356
{
400
357
return $ this ->currentFrame ;
401
358
}
402
359
403
- /**
404
- * @param string $text
405
- * @return Flash
406
- */
407
- public function flash ($ text )
360
+ public function flash (string $ text ) : Flash
408
361
{
409
362
if (strpos ($ text , "\n" ) !== false ) {
410
363
throw new \InvalidArgumentException ;
@@ -417,11 +370,7 @@ public function flash($text)
417
370
return new Flash ($ this , $ style , $ this ->terminal , $ text );
418
371
}
419
372
420
- /**
421
- * @param string $text
422
- * @return Confirm
423
- */
424
- public function confirm ($ text )
373
+ public function confirm ($ text ) : Confirm
425
374
{
426
375
if (strpos ($ text , "\n" ) !== false ) {
427
376
throw new \InvalidArgumentException ;
0 commit comments