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 CliMenuBuilder |
||
23 | */ |
||
24 | private $parentBuilder; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $selectedItemIndex; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $canBeSelected = true; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | private $margin = 2; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $blacklistedItems = [ |
||
45 | '\PhpSchool\CliMenu\MenuItem\AsciiArtItem', |
||
46 | '\PhpSchool\CliMenu\MenuItem\LineBreakItem', |
||
47 | '\PhpSchool\CliMenu\MenuItem\SplitItem', |
||
48 | ]; |
||
49 | |||
50 | |||
51 | public function __construct(CliMenuBuilder $builder, array $items = []) |
||
58 | |||
59 | /** |
||
60 | * Select default item |
||
61 | */ |
||
62 | private function setDefaultSelectedItem() |
||
75 | |||
76 | public function addMenuItem(MenuItemInterface $item) : self |
||
90 | |||
91 | public function addMenuItems(array $items) : self |
||
99 | |||
100 | public function setItems(array $items) : self |
||
107 | |||
108 | public function addItem( |
||
118 | |||
119 | public function addStaticItem(string $text) : self |
||
125 | |||
126 | public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder |
||
137 | |||
138 | public function end() : CliMenuBuilder |
||
156 | |||
157 | /** |
||
158 | * The output text for the item |
||
159 | */ |
||
160 | public function getRows(MenuStyle $style, bool $selected = false) : array |
||
227 | |||
228 | public function setSelectedItemIndex(int $index) : void |
||
232 | |||
233 | public function getSelectedItemIndex() : int |
||
240 | |||
241 | public function getSelectedItem() : MenuItem |
||
245 | |||
246 | public function getItems() : array |
||
250 | |||
251 | /** |
||
252 | * Can the item be selected |
||
253 | * Not really in this case but that's the trick |
||
254 | */ |
||
255 | public function canSelect() : bool |
||
259 | |||
260 | /** |
||
261 | * Execute the items callable if required |
||
262 | */ |
||
263 | public function getSelectAction() : ?callable |
||
267 | |||
268 | /** |
||
269 | * Whether or not the menu item is showing the menustyle extra value |
||
270 | */ |
||
271 | public function showsItemExtra() : bool |
||
275 | |||
276 | /** |
||
277 | * Enable showing item extra |
||
278 | */ |
||
279 | public function showItemExtra() : void |
||
283 | |||
284 | /** |
||
285 | * Disable showing item extra |
||
286 | */ |
||
287 | public function hideItemExtra() : void |
||
291 | |||
292 | /** |
||
293 | * Return the raw string of text |
||
294 | */ |
||
295 | public function getText() : string |
||
303 | } |
||
304 |
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.