Total Complexity | 43 |
Total Lines | 313 |
Duplicated Lines | 0 % |
Changes | 17 | ||
Bugs | 0 | Features | 1 |
Complex classes like SplitItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SplitItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class SplitItem implements MenuItemInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $items = []; |
||
18 | |||
19 | /** |
||
20 | * @var int|null |
||
21 | */ |
||
22 | private $selectedItemIndex; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $canBeSelected = true; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $gutter = 2; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private static $blacklistedItems = [ |
||
38 | \PhpSchool\CliMenu\MenuItem\AsciiArtItem::class, |
||
39 | \PhpSchool\CliMenu\MenuItem\LineBreakItem::class, |
||
40 | \PhpSchool\CliMenu\MenuItem\SplitItem::class, |
||
41 | ]; |
||
42 | |||
43 | public function __construct(array $items = []) |
||
44 | { |
||
45 | $this->addItems($items); |
||
46 | $this->setDefaultSelectedItem(); |
||
47 | } |
||
48 | |||
49 | public function setGutter(int $gutter) : void |
||
53 | } |
||
54 | |||
55 | public function getGutter() : int |
||
58 | } |
||
59 | |||
60 | public function addItem(MenuItemInterface $item) : self |
||
61 | { |
||
62 | foreach (self::$blacklistedItems as $bl) { |
||
63 | if ($item instanceof $bl) { |
||
64 | throw new \InvalidArgumentException("Cannot add a $bl to a SplitItem"); |
||
65 | } |
||
66 | } |
||
67 | $this->items[] = $item; |
||
68 | $this->setDefaultSelectedItem(); |
||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | public function addItems(array $items) : self |
||
73 | { |
||
74 | foreach ($items as $item) { |
||
75 | $this->addItem($item); |
||
76 | } |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | public function setItems(array $items) : self |
||
82 | { |
||
83 | $this->items = []; |
||
84 | $this->addItems($items); |
||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Select default item |
||
90 | */ |
||
91 | private function setDefaultSelectedItem() : void |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * The output text for the item |
||
107 | */ |
||
108 | public function getRows(MenuStyle $style, bool $selected = false) : array |
||
109 | { |
||
110 | $numberOfItems = count($this->items); |
||
111 | |||
112 | if ($numberOfItems === 0) { |
||
113 | throw new \RuntimeException(sprintf('There should be at least one item added to: %s', __CLASS__)); |
||
114 | } |
||
115 | |||
116 | if (!$selected) { |
||
117 | $this->setDefaultSelectedItem(); |
||
118 | } |
||
119 | |||
120 | $length = $style->getDisplaysExtra() |
||
121 | ? floor($style->getContentWidth() / $numberOfItems) - (mb_strlen($style->getItemExtra()) + 2) |
||
122 | : floor($style->getContentWidth() / $numberOfItems); |
||
123 | |||
124 | $length -= $this->gutter; |
||
125 | $length = (int) $length; |
||
126 | |||
127 | $missingLength = $style->getContentWidth() % $numberOfItems; |
||
128 | |||
129 | return $this->buildRows( |
||
130 | array_map(function ($index, $item) use ($selected, $length, $style) { |
||
131 | $isSelected = $selected && $index === $this->selectedItemIndex; |
||
132 | |||
133 | if ($item instanceof CheckableItem) { |
||
134 | $markerType = $item->getChecked() |
||
135 | ? $style->getCheckedMarker() |
||
136 | : $style->getUncheckedMarker(); |
||
137 | } else { |
||
138 | $markerType = $style->getMarker($isSelected); |
||
139 | } |
||
140 | |||
141 | $marker = $item->canSelect() |
||
142 | ? sprintf('%s', $markerType) |
||
143 | : ''; |
||
144 | |||
145 | $itemExtra = ''; |
||
146 | if ($style->getDisplaysExtra()) { |
||
147 | $itemExtra = $item->showsItemExtra() |
||
148 | ? sprintf(' %s', $style->getItemExtra()) |
||
149 | : sprintf(' %s', str_repeat(' ', mb_strlen($style->getItemExtra()))); |
||
150 | } |
||
151 | |||
152 | return $this->buildCell( |
||
153 | explode( |
||
154 | "\n", |
||
155 | StringUtil::wordwrap( |
||
156 | sprintf('%s%s', $marker, $item->getText()), |
||
157 | $length, |
||
158 | sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) |
||
159 | ) |
||
160 | ), |
||
161 | $length, |
||
162 | $style, |
||
163 | $isSelected, |
||
164 | $itemExtra |
||
165 | ); |
||
166 | }, array_keys($this->items), $this->items), |
||
167 | $style, |
||
168 | $missingLength, |
||
169 | $length |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | private function buildRows(array $cells, MenuStyle $style, int $missingLength, int $length) : array |
||
174 | { |
||
175 | $extraPadLength = $style->getDisplaysExtra() ? 2 + mb_strlen($style->getItemExtra()) : 0; |
||
176 | |||
177 | return array_map( |
||
178 | function ($i) use ($cells, $length, $missingLength, $extraPadLength) { |
||
179 | return $this->buildRow($cells, $i, $length, $missingLength, $extraPadLength); |
||
180 | }, |
||
181 | range(0, max(array_map('count', $cells)) - 1) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength) : string |
||
186 | { |
||
187 | return sprintf( |
||
188 | '%s%s', |
||
189 | implode( |
||
190 | '', |
||
191 | array_map( |
||
192 | function ($cell) use ($index, $length, $extraPadLength) { |
||
193 | return $cell[$index] ?? str_repeat(' ', $length + $this->gutter + $extraPadLength); |
||
194 | }, |
||
195 | $cells |
||
196 | ) |
||
197 | ), |
||
198 | str_repeat(' ', $missingLength) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | private function buildCell( |
||
203 | array $content, |
||
204 | int $length, |
||
205 | MenuStyle $style, |
||
206 | bool $isSelected, |
||
207 | string $itemExtra |
||
208 | ) : array { |
||
209 | return array_map(function ($row, $index) use ($length, $style, $isSelected, $itemExtra) { |
||
210 | $invertedColoursSetCode = $isSelected |
||
211 | ? $style->getInvertedColoursSetCode() |
||
212 | : ''; |
||
213 | $invertedColoursUnsetCode = $isSelected |
||
214 | ? $style->getInvertedColoursUnsetCode() |
||
215 | : ''; |
||
216 | |||
217 | return sprintf( |
||
218 | '%s%s%s%s%s%s', |
||
219 | $invertedColoursSetCode, |
||
220 | $row, |
||
221 | str_repeat(' ', $length - mb_strlen($row)), |
||
222 | $index === 0 ? $itemExtra : str_repeat(' ', mb_strlen($itemExtra)), |
||
223 | $invertedColoursUnsetCode, |
||
224 | str_repeat(' ', $this->gutter) |
||
225 | ); |
||
226 | }, $content, array_keys($content)); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Is there an item with this index and can it be |
||
231 | * selected? |
||
232 | */ |
||
233 | public function canSelectIndex(int $index) : bool |
||
234 | { |
||
235 | return isset($this->items[$index]) && $this->items[$index]->canSelect(); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Set the item index which should be selected. If the item does |
||
240 | * not exist then throw an exception. |
||
241 | */ |
||
242 | public function setSelectedItemIndex(int $index) : void |
||
243 | { |
||
244 | if (!isset($this->items[$index])) { |
||
245 | throw new \InvalidArgumentException(sprintf('Index: "%s" does not exist', $index)); |
||
246 | } |
||
247 | |||
248 | $this->selectedItemIndex = $index; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get the currently select item index. |
||
253 | * May be null in case of no selectable item. |
||
254 | */ |
||
255 | public function getSelectedItemIndex() : ?int |
||
256 | { |
||
257 | return $this->selectedItemIndex; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Get the currently selected item - if no items are selectable |
||
262 | * then throw an exception. |
||
263 | */ |
||
264 | public function getSelectedItem() : MenuItemInterface |
||
265 | { |
||
266 | if (null === $this->selectedItemIndex) { |
||
267 | throw new \RuntimeException('No item is selected'); |
||
268 | } |
||
269 | |||
270 | return $this->items[$this->selectedItemIndex]; |
||
271 | } |
||
272 | |||
273 | public function getItems() : array |
||
274 | { |
||
275 | return $this->items; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Can the item be selected |
||
280 | * In this case, it indicates if at least 1 item inside the SplitItem can be selected |
||
281 | */ |
||
282 | public function canSelect() : bool |
||
283 | { |
||
284 | return $this->canBeSelected; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Execute the items callable if required |
||
289 | */ |
||
290 | public function getSelectAction() : ?callable |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Whether or not the menu item is showing the menustyle extra value |
||
297 | */ |
||
298 | public function showsItemExtra() : bool |
||
299 | { |
||
300 | return false; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Enable showing item extra |
||
305 | */ |
||
306 | public function showItemExtra() : void |
||
307 | { |
||
308 | //noop |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Disable showing item extra |
||
313 | */ |
||
314 | public function hideItemExtra() : void |
||
315 | { |
||
316 | //noop |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Nothing to return with SplitItem |
||
321 | */ |
||
322 | public function getText() : string |
||
325 | } |
||
326 | } |
||
327 |