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 |
||
| 8 | class Column implements ColumnSettableInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $name; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $properties = [ |
||
| 21 | 'type' => null, // will be defaulted to string |
||
| 22 | 'formatter' => null, |
||
| 23 | 'excluded' => false, |
||
| 24 | 'hidden' => false, |
||
| 25 | 'width' => null, |
||
| 26 | 'header' => null, // will be defaulted to name |
||
| 27 | 'filterable' => true, |
||
| 28 | 'groupable' => true, |
||
| 29 | 'sortable' => true, |
||
| 30 | 'editable' => false, |
||
| 31 | 'virtual' => true, |
||
| 32 | 'align' => null, |
||
| 33 | 'class' => null |
||
| 34 | ]; |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructor |
||
| 39 | * @param string $name unique identifier name for the column |
||
| 40 | * @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...) |
||
| 41 | * @throws Exception\InvalidArgumentException |
||
| 42 | */ |
||
| 43 | 39 | public function __construct($name, array $properties = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * This method ensure some properties are defaulted. |
||
| 60 | * For example header with name and type is string |
||
| 61 | */ |
||
| 62 | 36 | protected function initDefaults() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get the name of the column |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 32 | public function getName() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Set column datatype |
||
| 84 | * @param string|AbstractType $type |
||
| 85 | * @throws Exception\InvalidArgumentException when the type is not supported. |
||
| 86 | * @return Column |
||
| 87 | */ |
||
| 88 | 36 | public function setType($type) |
|
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * |
||
| 102 | * @return AbstractType |
||
| 103 | */ |
||
| 104 | 4 | public function getType() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * |
||
| 111 | * @param FormatterInterface $formatter |
||
| 112 | * @return Column |
||
| 113 | */ |
||
| 114 | 4 | public function setFormatter(FormatterInterface $formatter) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * @return FormatterInterface|null |
||
| 123 | */ |
||
| 124 | 11 | public function getFormatter() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * |
||
| 131 | * @param boolean $virtual |
||
| 132 | * @return Column |
||
| 133 | */ |
||
| 134 | 32 | public function setVirtual($virtual = true) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * |
||
| 142 | * @return boolean |
||
| 143 | */ |
||
| 144 | 6 | public function isVirtual() |
|
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * |
||
| 152 | * @param boolean $excluded |
||
| 153 | * @return Column |
||
| 154 | */ |
||
| 155 | 10 | public function setExcluded($excluded = true) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * |
||
| 163 | * @return boolean |
||
| 164 | */ |
||
| 165 | 22 | public function isExcluded() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * |
||
| 172 | * @param boolean $editable |
||
| 173 | * @return Column |
||
| 174 | */ |
||
| 175 | 4 | public function setEditable($editable = true) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * |
||
| 183 | * @return boolean |
||
| 184 | */ |
||
| 185 | 4 | public function isEditable() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * |
||
| 192 | * @param boolean $hidden |
||
| 193 | * @return Column |
||
| 194 | */ |
||
| 195 | 4 | public function setHidden($hidden = true) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * |
||
| 203 | * @return boolean |
||
| 204 | */ |
||
| 205 | 3 | public function isHidden() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * |
||
| 212 | * @param boolean $sortable |
||
| 213 | * @return Column |
||
| 214 | */ |
||
| 215 | 4 | public function setSortable($sortable = true) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | 4 | public function isSortable() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * |
||
| 232 | * @param boolean $groupable |
||
| 233 | * @return Column |
||
| 234 | */ |
||
| 235 | 4 | public function setGroupable($groupable = true) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | 4 | public function isGroupable() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * |
||
| 252 | * @param boolean $filterable |
||
| 253 | * @return Column |
||
| 254 | */ |
||
| 255 | 4 | public function setFilterable($filterable = true) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * |
||
| 263 | * @return boolean |
||
| 264 | */ |
||
| 265 | 4 | public function isFilterable() |
|
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Set recommended width for the column |
||
| 273 | * |
||
| 274 | * @throws Exception\InvalidArgumentException |
||
| 275 | * @param float|int|string $width |
||
| 276 | * @return Column |
||
| 277 | */ |
||
| 278 | 5 | public function setWidth($width) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * |
||
| 289 | * @return float|int|string |
||
| 290 | */ |
||
| 291 | 4 | public function getWidth() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Set table header for this column |
||
| 298 | * |
||
| 299 | * @throws Exception\InvalidArgumentException |
||
| 300 | * @param string|null $header |
||
| 301 | * @return Column |
||
| 302 | */ |
||
| 303 | 37 | public function setHeader($header) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * |
||
| 311 | * @return string|null |
||
| 312 | */ |
||
| 313 | 4 | public function getHeader() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Set recommended horizontal align |
||
| 320 | * |
||
| 321 | * @throws Exception\InvalidArgumentException |
||
| 322 | * @param string $align can be left|center|right |
||
| 323 | * @return Column |
||
| 324 | */ |
||
| 325 | 1 | public function setAlign($align) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Return recommended horizontal alignment |
||
| 336 | * @return string|null |
||
| 337 | */ |
||
| 338 | 1 | public function getAlign() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Set recommended css class |
||
| 345 | * |
||
| 346 | * @throws Exception\InvalidArgumentException |
||
| 347 | * @param string $class css class |
||
| 348 | * @return Column |
||
| 349 | */ |
||
| 350 | 1 | public function setClass($class) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Return recommended css class |
||
| 361 | * @return string|null |
||
| 362 | */ |
||
| 363 | 1 | public function getClass() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | */ |
||
| 372 | 2 | public function getProperties() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Set properties for the column |
||
| 379 | * |
||
| 380 | * @throws Exception\InvalidArgumentException |
||
| 381 | * @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...) |
||
| 382 | * @return Column |
||
| 383 | */ |
||
| 384 | 8 | public function setProperties(array $properties) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | 5 | public function __toString() |
|
| 405 | } |
||
| 406 |