Complex classes like Column 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 Column, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Column implements ColumnSettableInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $name; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $properties = [ |
||
| 29 | 'type' => null, // will be defaulted to string |
||
| 30 | 'formatter' => null, |
||
| 31 | 'excluded' => false, |
||
| 32 | 'hidden' => false, |
||
| 33 | 'width' => null, |
||
| 34 | 'header' => null, // will be defaulted to name |
||
| 35 | 'filterable' => true, |
||
| 36 | 'groupable' => true, |
||
| 37 | 'sortable' => true, |
||
| 38 | 'editable' => false, |
||
| 39 | 'virtual' => true, |
||
| 40 | 'align' => null, |
||
| 41 | 'class' => null |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor. |
||
| 46 | * |
||
| 47 | * @param string $name unique identifier name for the column |
||
| 48 | * @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...) |
||
| 49 | * |
||
| 50 | * @throws Exception\InvalidArgumentException |
||
| 51 | */ |
||
| 52 | 38 | public function __construct($name, array $properties = null) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * This method ensure some properties are defaulted. |
||
| 69 | * For example header with name and type is string. |
||
| 70 | */ |
||
| 71 | 35 | protected function initDefaults() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get the name of the column. |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | 31 | public function getName() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Set column datatype. |
||
| 93 | * |
||
| 94 | * @param string|AbstractType $type |
||
| 95 | * |
||
| 96 | * @throws Exception\InvalidArgumentException when the type is not supported |
||
| 97 | * |
||
| 98 | * @return Column |
||
| 99 | */ |
||
| 100 | 35 | public function setType($type) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return AbstractType |
||
| 114 | */ |
||
| 115 | 4 | public function getType() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param FormatterInterface $formatter |
||
| 122 | * |
||
| 123 | * @return Column |
||
| 124 | */ |
||
| 125 | 4 | public function setFormatter(FormatterInterface $formatter) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @return FormatterInterface|null |
||
| 134 | */ |
||
| 135 | 11 | public function getFormatter() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param bool $virtual |
||
| 142 | * |
||
| 143 | * @return Column |
||
| 144 | */ |
||
| 145 | 31 | public function setVirtual($virtual = true) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | 6 | public function isVirtual() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param bool $excluded |
||
| 162 | * |
||
| 163 | * @return Column |
||
| 164 | */ |
||
| 165 | 11 | public function setExcluded($excluded = true) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 23 | public function isExcluded() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @param bool $editable |
||
| 182 | * |
||
| 183 | * @return Column |
||
| 184 | */ |
||
| 185 | 4 | public function setEditable($editable = true) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 4 | public function isEditable() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param bool $hidden |
||
| 202 | * |
||
| 203 | * @return Column |
||
| 204 | */ |
||
| 205 | 4 | public function setHidden($hidden = true) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | 3 | public function isHidden() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param bool $sortable |
||
| 222 | * |
||
| 223 | * @return Column |
||
| 224 | */ |
||
| 225 | 4 | public function setSortable($sortable = true) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | 4 | public function isSortable() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param bool $groupable |
||
| 242 | * |
||
| 243 | * @return Column |
||
| 244 | */ |
||
| 245 | 4 | public function setGroupable($groupable = true) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 4 | public function isGroupable() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param bool $filterable |
||
| 262 | * |
||
| 263 | * @return Column |
||
| 264 | */ |
||
| 265 | 4 | public function setFilterable($filterable = true) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | 4 | public function isFilterable() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Set recommended width for the column. |
||
| 282 | * |
||
| 283 | * @throws Exception\InvalidArgumentException |
||
| 284 | * |
||
| 285 | * @param float|int|string $width |
||
| 286 | * |
||
| 287 | * @return Column |
||
| 288 | */ |
||
| 289 | 5 | public function setWidth($width) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @return float|int|string |
||
| 301 | */ |
||
| 302 | 4 | public function getWidth() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Set table header for this column. |
||
| 309 | * |
||
| 310 | * @throws Exception\InvalidArgumentException |
||
| 311 | * |
||
| 312 | * @param string|null $header |
||
| 313 | * |
||
| 314 | * @return Column |
||
| 315 | */ |
||
| 316 | 36 | public function setHeader($header) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @return string|null |
||
| 325 | */ |
||
| 326 | 4 | public function getHeader() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Set recommended horizontal align. |
||
| 333 | * |
||
| 334 | * @throws Exception\InvalidArgumentException |
||
| 335 | * |
||
| 336 | * @param string $align can be left|center|right |
||
| 337 | * |
||
| 338 | * @return Column |
||
| 339 | */ |
||
| 340 | 1 | public function setAlign($align) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Return recommended horizontal alignment. |
||
| 352 | * |
||
| 353 | * @return string|null |
||
| 354 | */ |
||
| 355 | 1 | public function getAlign() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Set recommended css class. |
||
| 362 | * |
||
| 363 | * @throws Exception\InvalidArgumentException |
||
| 364 | * |
||
| 365 | * @param string $class css class |
||
| 366 | * |
||
| 367 | * @return Column |
||
| 368 | */ |
||
| 369 | 1 | public function setClass($class) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Return recommended css class. |
||
| 381 | * |
||
| 382 | * @return string|null |
||
| 383 | */ |
||
| 384 | 1 | public function getClass() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | 2 | public function getProperties() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Set properties for the column. |
||
| 399 | * |
||
| 400 | * @throws Exception\InvalidArgumentException |
||
| 401 | * |
||
| 402 | * @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...) |
||
| 403 | * |
||
| 404 | * @return Column |
||
| 405 | */ |
||
| 406 | 8 | public function setProperties(array $properties) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 5 | public function __toString() |
|
| 427 | } |
||
| 428 |