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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
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 $gutter = 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 = []) |
||
50 | |||
51 | public function setGutter(int $gutter) : void |
||
56 | |||
57 | public function addItem(MenuItemInterface $item) : self |
||
68 | |||
69 | public function addItems(array $items) : self |
||
77 | |||
78 | public function setItems(array $items) : self |
||
84 | |||
85 | /** |
||
86 | * Select default item |
||
87 | */ |
||
88 | private function setDefaultSelectedItem() : void |
||
101 | |||
102 | /** |
||
103 | * The output text for the item |
||
104 | */ |
||
105 | public function getRows(MenuStyle $style, bool $selected = false) : array |
||
159 | |||
160 | private function buildRows(array $cells, MenuStyle $style, int $missingLength, int $length) : array |
||
171 | |||
172 | private function buildRow(array $cells, int $index, int $length, int $missingLength, int $extraPadLength) : string |
||
188 | |||
189 | private function buildCell( |
||
215 | |||
216 | /** |
||
217 | * Is there an item with this index and can it be |
||
218 | * selected? |
||
219 | */ |
||
220 | public function canSelectIndex(int $index) : bool |
||
224 | |||
225 | /** |
||
226 | * Set the item index which should be selected. If the item does |
||
227 | * not exist then throw an exception. |
||
228 | */ |
||
229 | public function setSelectedItemIndex(int $index) : void |
||
237 | |||
238 | /** |
||
239 | * Get the currently select item index. |
||
240 | * May be null in case of no selectable item. |
||
241 | */ |
||
242 | public function getSelectedItemIndex() : ?int |
||
246 | |||
247 | /** |
||
248 | * Get the currently selected item - if no items are selectable |
||
249 | * then throw an exception. |
||
250 | */ |
||
251 | public function getSelectedItem() : MenuItemInterface |
||
259 | |||
260 | public function getItems() : array |
||
264 | |||
265 | /** |
||
266 | * Can the item be selected |
||
267 | * In this case, it indicates if at least 1 item inside the SplitItem can be selected |
||
268 | */ |
||
269 | public function canSelect() : bool |
||
273 | |||
274 | /** |
||
275 | * Execute the items callable if required |
||
276 | */ |
||
277 | public function getSelectAction() : ?callable |
||
281 | |||
282 | /** |
||
283 | * Whether or not the menu item is showing the menustyle extra value |
||
284 | */ |
||
285 | public function showsItemExtra() : bool |
||
289 | |||
290 | /** |
||
291 | * Enable showing item extra |
||
292 | */ |
||
293 | public function showItemExtra() : void |
||
297 | |||
298 | /** |
||
299 | * Disable showing item extra |
||
300 | */ |
||
301 | public function hideItemExtra() : void |
||
305 | |||
306 | /** |
||
307 | * Nothing to return with SplitItem |
||
308 | */ |
||
309 | public function getText() : string |
||
313 | } |
||
314 |
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.