1 | <?php |
||
14 | class SplitItem implements MenuItemInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $items = []; |
||
20 | |||
21 | /** |
||
22 | * @var int|null |
||
23 | */ |
||
24 | private $selectedItemIndex; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $canBeSelected = true; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | private $margin = 2; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $blacklistedItems = [ |
||
40 | \PhpSchool\CliMenu\MenuItem\AsciiArtItem::class, |
||
41 | \PhpSchool\CliMenu\MenuItem\LineBreakItem::class, |
||
42 | \PhpSchool\CliMenu\MenuItem\SplitItem::class, |
||
43 | ]; |
||
44 | |||
45 | public function __construct(array $items = []) |
||
46 | { |
||
47 | $this->addItems($items); |
||
48 | $this->setDefaultSelectedItem(); |
||
49 | } |
||
50 | |||
51 | public function addItem(MenuItemInterface $item) : self |
||
52 | { |
||
53 | foreach (self::$blacklistedItems as $bl) { |
||
54 | if ($item instanceof $bl) { |
||
55 | throw new \InvalidArgumentException("Cannot add a $bl to a SplitItem"); |
||
56 | } |
||
57 | } |
||
58 | $this->items[] = $item; |
||
59 | $this->setDefaultSelectedItem(); |
||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | public function addItems(array $items) : self |
||
64 | { |
||
65 | foreach ($items as $item) { |
||
66 | $this->addItem($item); |
||
67 | } |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | public function setItems(array $items) : self |
||
78 | |||
79 | /** |
||
80 | * Select default item |
||
81 | */ |
||
82 | private function setDefaultSelectedItem() : void |
||
95 | |||
96 | /** |
||
97 | * The output text for the item |
||
98 | */ |
||
99 | public function getRows(MenuStyle $style, bool $selected = false) : array |
||
146 | |||
147 | private function buildRows(array $cells, MenuStyle $style, int $missingLength, int $length) : array |
||
158 | |||
159 | private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength) : string |
||
175 | |||
176 | private function buildCell( |
||
202 | |||
203 | /** |
||
204 | * Is there an item with this index and can it be |
||
205 | * selected? |
||
206 | */ |
||
207 | public function canSelectIndex(int $index) : bool |
||
211 | |||
212 | /** |
||
213 | * Set the item index which should be selected. If the item does |
||
214 | * not exist then throw an exception. |
||
215 | */ |
||
216 | public function setSelectedItemIndex(int $index) : void |
||
224 | |||
225 | /** |
||
226 | * Get the currently select item index. |
||
227 | * May be null in case of no selectable item. |
||
228 | */ |
||
229 | public function getSelectedItemIndex() : ?int |
||
233 | |||
234 | /** |
||
235 | * Get the currently selected item - if no items are selectable |
||
236 | * then throw an exception. |
||
237 | */ |
||
238 | public function getSelectedItem() : MenuItemInterface |
||
246 | |||
247 | public function getItems() : array |
||
251 | |||
252 | /** |
||
253 | * Can the item be selected |
||
254 | * In this case, it indicates if at least 1 item inside the SplitItem can be selected |
||
255 | */ |
||
256 | public function canSelect() : bool |
||
260 | |||
261 | /** |
||
262 | * Execute the items callable if required |
||
263 | */ |
||
264 | public function getSelectAction() : ?callable |
||
268 | |||
269 | /** |
||
270 | * Whether or not the menu item is showing the menustyle extra value |
||
271 | */ |
||
272 | public function showsItemExtra() : bool |
||
276 | |||
277 | /** |
||
278 | * Enable showing item extra |
||
279 | */ |
||
280 | public function showItemExtra() : void |
||
284 | |||
285 | /** |
||
286 | * Disable showing item extra |
||
287 | */ |
||
288 | public function hideItemExtra() : void |
||
292 | |||
293 | /** |
||
294 | * Nothing to return with SplitItem |
||
295 | */ |
||
296 | public function getText() : string |
||
300 | } |
||
301 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.