Complex classes like InputAddonTrait 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 InputAddonTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | trait InputAddonTrait |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var string the input type |
||
27 | */ |
||
28 | public $type = null; |
||
29 | |||
30 | /** |
||
31 | * @var array addon options. The following settings can be configured: |
||
32 | * - prepend: array the prepend addon configuration |
||
33 | * - append: array the append addon configuration |
||
34 | * - content: string the prepend addon content |
||
35 | * - asButton: boolean whether the addon is a button or button group. Defaults to false. |
||
36 | * - options: array the HTML attributes to be added to the container. |
||
37 | * - groupOptions: array HTML options for the input group |
||
38 | * - contentBefore: string content placed before addon |
||
39 | * - contentAfter: string content placed after addon |
||
40 | * - icon: array HTML options for the input group |
||
41 | * - iconPosition: string icon position 'left' or 'right' |
||
42 | */ |
||
43 | public $addon = []; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Set the input field type |
||
48 | * |
||
49 | * @param string $type input field type |
||
50 | */ |
||
51 | public function setType($type) |
||
55 | |||
56 | /** |
||
57 | * Add an addon to prepend or append to the output |
||
58 | * |
||
59 | * @param string|array $config addon config or string content |
||
60 | * @param string $type [optional] default 'append' can be 'append' or 'prepend' |
||
61 | * @param boolean $first [optional] force to front of array, default false |
||
62 | * @return void |
||
63 | */ |
||
64 | public function addAddon($config, $type = 'append', $first = false) |
||
78 | |||
79 | /** |
||
80 | * Add an icon addon |
||
81 | * |
||
82 | * @param string $options HTML options for the icon element |
||
83 | * @param string $position [optional] default null (left) or right |
||
84 | * @return void |
||
85 | */ |
||
86 | public function addIconAddon($options, $position = null) |
||
91 | |||
92 | /** |
||
93 | * Merge in extra addon settings |
||
94 | * |
||
95 | * @param array $addon |
||
96 | * @return void |
||
97 | */ |
||
98 | public function mergeAddon($addon) |
||
122 | |||
123 | /** |
||
124 | * Setup or extend the groupOptions |
||
125 | * |
||
126 | * @param string $name |
||
127 | * @param mixed $value |
||
128 | * @param boolean $extend [optional] default false |
||
129 | * @return void |
||
130 | */ |
||
131 | public function setGroupOption($name, $value, $extend = false) |
||
147 | |||
148 | /** |
||
149 | * Set the default group size for the input-group |
||
150 | * |
||
151 | * @param string $size |
||
152 | * @return void |
||
153 | */ |
||
154 | public function setGroupSize($size = '') |
||
160 | |||
161 | /** |
||
162 | * Generates the addon markup |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | protected function generateAddon() |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Parses and returns addon content |
||
209 | * |
||
210 | * @param string|array $addon the addon parameter |
||
211 | * @param string $type type of addon prepend or append |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function getAddonContent($addon, $type = 'append') |
||
256 | |||
257 | protected function isUnsupportedIconInput() |
||
273 | } |
||
274 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.