Complex classes like ViewModel 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 ViewModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ViewModel extends AbstractViewModel |
||
24 | { |
||
25 | /** |
||
26 | * @var Aggregate|null |
||
27 | */ |
||
28 | protected $aggregate; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $prefixMethod = 'normalize'; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $allowForceSingle = true; |
||
39 | |||
40 | /** |
||
41 | * @var ParameterBag |
||
42 | */ |
||
43 | protected $parameter; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $fields = array(); |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $append = array(); |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $hidden = array(); |
||
59 | |||
60 | /** |
||
61 | * @var mixed |
||
62 | */ |
||
63 | private $data; |
||
64 | |||
65 | /** |
||
66 | * @var array|null |
||
67 | */ |
||
68 | private $built; |
||
69 | |||
70 | /** |
||
71 | * @var bool |
||
72 | */ |
||
73 | private $forceSingle = false; |
||
74 | |||
75 | /** |
||
76 | * Constructor. |
||
77 | * |
||
78 | * @param mixed $data |
||
79 | * @param array $parameter |
||
80 | */ |
||
81 | public function __construct($data, array $parameter = array()) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function init($data) |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function build() |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function isCollection() |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function addParameter(array $parameters) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function setForceSingle() |
||
167 | |||
168 | /** |
||
169 | * Add fields that should be viewed. |
||
170 | * |
||
171 | * @param array $fields |
||
172 | */ |
||
173 | public function addFields(array $fields) |
||
177 | |||
178 | /** |
||
179 | * Add fields that should be appended. |
||
180 | * |
||
181 | * @param array $fields |
||
182 | */ |
||
183 | public function addAppend(array $fields) |
||
187 | |||
188 | /** |
||
189 | * Add fields that should be hidden. |
||
190 | * |
||
191 | * @param array $fields |
||
192 | */ |
||
193 | public function addHidden(array $fields) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function buildViewModelArgs($results, array $record, $field) |
||
216 | |||
217 | /** |
||
218 | * Get view model raw data. |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function getRawData() |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | protected function fields() |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | protected function append() |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | protected function hidden() |
||
250 | |||
251 | /** |
||
252 | * Serialize record based on view model. |
||
253 | * |
||
254 | * @param mixed $results |
||
255 | * @param array $record |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | private function serialize($results, array $record) |
||
277 | |||
278 | /** |
||
279 | * Normalize array record. |
||
280 | * |
||
281 | * @param mixed $record |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | private function normalize($record) |
||
297 | |||
298 | /** |
||
299 | * Compute view model value. |
||
300 | * |
||
301 | * @param mixed $results |
||
302 | * @param array $record |
||
303 | * @param string $field |
||
304 | * |
||
305 | * @return mixed|null |
||
306 | */ |
||
307 | private function computeViewModelValue($results, array $record, $field) |
||
323 | |||
324 | /** |
||
325 | * Merged key fields. |
||
326 | * |
||
327 | * @param array $recordFields |
||
328 | * @param array $viewModelFields |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | private function mergeFields(array $recordFields, array $viewModelFields) |
||
338 | |||
339 | /** |
||
340 | * Set data. |
||
341 | * |
||
342 | * @param mixed $data |
||
343 | */ |
||
344 | private function setData($data) |
||
359 | |||
360 | /** |
||
361 | * Build data. |
||
362 | * |
||
363 | * @param mixed $data |
||
364 | * |
||
365 | * @return mixed |
||
366 | */ |
||
367 | private function buildData($data) |
||
379 | |||
380 | /** |
||
381 | * Assert data type. |
||
382 | * |
||
383 | * @param mixed $data |
||
384 | */ |
||
385 | private function assertDataType($data) |
||
395 | } |
||
396 |