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 = null, array $parameter = array()) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function init($data) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function build() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function isCollection() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function addParameter(array $parameters) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function setForceSingle() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add fields that should be viewed. |
||
| 167 | * |
||
| 168 | * @param array $fields |
||
| 169 | */ |
||
| 170 | public function addFields(array $fields) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add fields that should be appended. |
||
| 177 | * |
||
| 178 | * @param array $fields |
||
| 179 | */ |
||
| 180 | public function addAppend(array $fields) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add fields that should be hidden. |
||
| 187 | * |
||
| 188 | * @param array $fields |
||
| 189 | */ |
||
| 190 | public function addHidden(array $fields) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritdoc} |
||
| 197 | */ |
||
| 198 | public function buildViewModelArgs($results, array $record, $field) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get view model raw data. |
||
| 216 | * |
||
| 217 | * @return mixed |
||
| 218 | */ |
||
| 219 | public function getRawData() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | protected function fields() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | protected function append() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | protected function hidden() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Serialize record based on view model. |
||
| 250 | * |
||
| 251 | * @param mixed $results |
||
| 252 | * @param array $record |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | private function serialize($results, array $record) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Normalize array record. |
||
| 281 | * |
||
| 282 | * @param mixed $record |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | private function normalize($record) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Compute view model value. |
||
| 301 | * |
||
| 302 | * @param mixed $results |
||
| 303 | * @param array $record |
||
| 304 | * @param string $field |
||
| 305 | * |
||
| 306 | * @return mixed|null |
||
| 307 | */ |
||
| 308 | private function computeViewModelValue($results, array $record, $field) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Merged key fields. |
||
| 327 | * |
||
| 328 | * @param array $recordFields |
||
| 329 | * @param array $viewModelFields |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | private function mergeFields(array $recordFields, array $viewModelFields) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Set data. |
||
| 342 | * |
||
| 343 | * @param mixed $data |
||
| 344 | */ |
||
| 345 | private function setData($data) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Build data. |
||
| 363 | * |
||
| 364 | * @param mixed $data |
||
| 365 | * |
||
| 366 | * @return mixed |
||
| 367 | */ |
||
| 368 | private function buildData($data) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Assert data type. |
||
| 385 | * |
||
| 386 | * @param mixed $data |
||
| 387 | */ |
||
| 388 | private function assertDataType($data) |
||
| 398 | } |
||
| 399 |