Complex classes like MetaBox 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 MetaBox, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class MetaBox extends Component |
||
16 | { |
||
17 | use Condition; |
||
18 | use Instruction; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | const ID = 'metaboxes'; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | public $metaboxes = []; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function init() |
||
47 | |||
48 | /** |
||
49 | * @param string $name |
||
50 | * @param mixed ...$args |
||
51 | * @return void |
||
52 | */ |
||
53 | public function action( $name, ...$args ) |
||
57 | |||
58 | /** |
||
59 | * @param string $name |
||
60 | * @param mixed ...$args |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function filter( $name, ...$args ) |
||
67 | |||
68 | /** |
||
69 | * @param string $key |
||
70 | * @param mixed $fallback |
||
71 | * @param string $group |
||
72 | * @return string|array |
||
73 | */ |
||
74 | public function getMetaValue( $key, $fallback = '', $group = '' ) |
||
80 | |||
81 | /** |
||
82 | * @param array $field |
||
83 | * @return array |
||
84 | */ |
||
85 | public function normalizeMapField( $field ) |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | * @filter rwmb_meta_boxes |
||
103 | */ |
||
104 | public function register() |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | * @filter rwmb_outer_html |
||
121 | */ |
||
122 | public function renderField( $html, $field ) |
||
128 | |||
129 | /** |
||
130 | * @return bool |
||
131 | * @filter rwmb_show |
||
132 | */ |
||
133 | public function show( $bool, array $metabox ) |
||
142 | |||
143 | /** |
||
144 | * @return int |
||
145 | */ |
||
146 | protected function getPostId() |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | protected function getPostTypes() |
||
166 | |||
167 | /** |
||
168 | * @return bool |
||
169 | */ |
||
170 | protected function hasPostType( array $metabox ) |
||
177 | |||
178 | /** |
||
179 | * @return void |
||
180 | */ |
||
181 | protected function normalize( array $metaboxes, array $defaults = [] ) |
||
196 | |||
197 | /** |
||
198 | * @param string $depends |
||
199 | * @param string $parentId |
||
200 | * @return string |
||
201 | */ |
||
202 | protected function normalizeDepends( $depends, array $data, $parentId ) |
||
208 | |||
209 | /** |
||
210 | * @param string $name |
||
211 | * @param string $parentId |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function normalizeFieldName( $name, array $data, $parentId ) |
||
218 | |||
219 | /** |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function normalizeFields( array $fields, array $data, $parentId ) |
||
237 | |||
238 | /** |
||
239 | * @param string $id |
||
240 | * @param string $parentId |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function normalizeId( $id, array $data, $parentId ) |
||
247 | |||
248 | /** |
||
249 | * @param mixed $types |
||
250 | * @return array |
||
251 | */ |
||
252 | protected function normalizePostTypes( $types ) |
||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function normalizeValidation( array $validation, array $data, $parentId ) |
||
271 | |||
272 | /** |
||
273 | * @return array |
||
274 | */ |
||
275 | protected function setDependencies( array $metabox ) |
||
289 | } |
||
290 |