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()) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function build() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | public function isCollection() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function addParameter(array $parameters) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function setForceSingle() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add fields that should be viewed. |
||
| 160 | * |
||
| 161 | * @param array $fields |
||
| 162 | */ |
||
| 163 | public function addFields(array $fields) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add fields that should be appended. |
||
| 170 | * |
||
| 171 | * @param array $fields |
||
| 172 | */ |
||
| 173 | public function addAppend(array $fields) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Add fields that should be hidden. |
||
| 180 | * |
||
| 181 | * @param array $fields |
||
| 182 | */ |
||
| 183 | public function addHidden(array $fields) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function buildViewModelArgs($results, array $record, $field) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get view model raw data. |
||
| 209 | * |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | public function getRawData() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | protected function fields() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | protected function append() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | protected function hidden() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Serialize record based on view model. |
||
| 243 | * |
||
| 244 | * @param mixed $results |
||
| 245 | * @param array $record |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | private function serialize($results, array $record) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Normalize array record. |
||
| 270 | * |
||
| 271 | * @param mixed $record |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | private function normalize($record) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Compute view model value. |
||
| 290 | * |
||
| 291 | * @param mixed $results |
||
| 292 | * @param array $record |
||
| 293 | * @param string $field |
||
| 294 | * |
||
| 295 | * @return mixed|null |
||
| 296 | */ |
||
| 297 | private function computeViewModelValue($results, array $record, $field) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Merged key fields. |
||
| 316 | * |
||
| 317 | * @param array $recordFields |
||
| 318 | * @param array $viewModelFields |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | private function mergeFields(array $recordFields, array $viewModelFields) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set data. |
||
| 331 | * |
||
| 332 | * @param mixed $data |
||
| 333 | */ |
||
| 334 | private function setData($data) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Build data. |
||
| 352 | * |
||
| 353 | * @param mixed $data |
||
| 354 | * |
||
| 355 | * @return mixed |
||
| 356 | */ |
||
| 357 | private function buildData($data) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Assert data type. |
||
| 372 | * |
||
| 373 | * @param mixed $data |
||
| 374 | */ |
||
| 375 | private function assertDataType($data) |
||
| 385 | } |
||
| 386 |