Complex classes like ColumnModel 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 ColumnModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ColumnModel |
||
| 24 | { |
||
| 25 | const ADD_COLUMN_AFTER = 'after'; |
||
| 26 | const ADD_COLUMN_BEFORE = 'before'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ArrayObject |
||
| 30 | */ |
||
| 31 | protected $columns; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Search |
||
| 35 | */ |
||
| 36 | protected $search; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ArrayObject |
||
| 40 | */ |
||
| 41 | protected $row_renderers; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ColumnsMetadata|null |
||
| 45 | */ |
||
| 46 | 32 | protected $metadata; |
|
| 47 | |||
| 48 | 32 | public function __construct() |
|
| 49 | 32 | { |
|
| 50 | 32 | $this->columns = new ArrayObject(); |
|
| 51 | $this->row_renderers = new ArrayObject(); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add a row renderer. |
||
| 56 | * |
||
| 57 | * @throws Exception\InvalidArgumentException |
||
| 58 | * |
||
| 59 | 6 | * @param RowRendererInterface $renderer |
|
| 60 | */ |
||
| 61 | public function addRowRenderer(RowRendererInterface $renderer): void |
||
| 62 | 6 | { |
|
| 63 | // Test if all required columns are present in column model |
||
| 64 | 6 | $required_columns = $renderer->getRequiredColumns(); |
|
| 65 | 2 | ||
| 66 | 1 | foreach ($required_columns as $column) { |
|
| 67 | 1 | if (!$this->exists($column)) { |
|
| 68 | 2 | $cls = get_class($renderer); |
|
| 69 | $msg = "Renderer '$cls' requires column '$column' to be present in column model."; |
||
| 70 | throw new Exception\MissingColumnException(__METHOD__ . ': ' . $msg); |
||
| 71 | } |
||
| 72 | 5 | } |
|
| 73 | 5 | ||
| 74 | $this->row_renderers->append($renderer); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | 9 | * @return ArrayObject |
|
| 79 | */ |
||
| 80 | 9 | public function getRowRenderers() |
|
| 81 | { |
||
| 82 | return $this->row_renderers; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Return an array object containing all |
||
| 87 | * columns that have a formatter (FormatterInterface). |
||
| 88 | * [column_name] => [FormatterInterface]. |
||
| 89 | * |
||
| 90 | * @see self::getUniqueFormatters() |
||
| 91 | */ |
||
| 92 | 9 | public function getFormatters(): ArrayObject |
|
| 103 | |||
| 104 | /** |
||
| 105 | * This method returns unique formatters set in the column model |
||
| 106 | * in an ArrayObject. |
||
| 107 | * |
||
| 108 | * |
||
| 109 | * @param bool $include_excluded_columns |
||
| 110 | * |
||
| 111 | * @see self::getFormatters() |
||
| 112 | */ |
||
| 113 | public function getUniqueFormatters(bool $include_excluded_columns = false): ArrayObject |
||
| 135 | 9 | ||
| 136 | /** |
||
| 137 | * Add a new column to the column model. |
||
| 138 | * |
||
| 139 | * @throws Exception\InvalidArgumentException when mode is not supported |
||
| 140 | * @throws Exception\DuplicateColumnException when column name already exists |
||
| 141 | * @throws Exception\ColumnNotFoundException when after_column does not exists |
||
| 142 | * |
||
| 143 | * @param string $after_column add the new column after this existing one |
||
| 144 | * @param string $mode change after to before (see self::ADD_COLUMN_AFTER, self::ADD_COLUMN_BEFORE) |
||
| 145 | */ |
||
| 146 | public function add(Column $column, string $after_column = null, string $mode = self::ADD_COLUMN_AFTER): self |
||
| 184 | 32 | ||
| 185 | /** |
||
| 186 | * Tells whether a column exists. |
||
| 187 | 32 | * |
|
| 188 | * @throws Exception\InvalidArgumentException |
||
| 189 | */ |
||
| 190 | public function exists(string $column): bool |
||
| 198 | |||
| 199 | 32 | /** |
|
| 200 | * Return column that have been excluded in getData() and getColumns(). |
||
| 201 | 32 | * |
|
| 202 | 2 | * @return array |
|
| 203 | */ |
||
| 204 | 32 | public function getExcluded(): array |
|
| 215 | |||
| 216 | 4 | /** |
|
| 217 | * Return column from identifier name. |
||
| 218 | 4 | * |
|
| 219 | 4 | * @param string $column column name |
|
| 220 | 4 | * |
|
| 221 | 4 | * @throws Exception\InvalidArgumentException |
|
| 222 | * @throws Exception\ColumnNotFoundException when column does not exists in model |
||
| 223 | */ |
||
| 224 | public function get($column): Column |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Sort columns in the order specified, columns that exists |
||
| 235 | * in the dataset but not in the sorted_columns will be |
||
| 236 | * appended to the end. |
||
| 237 | * |
||
| 238 | 14 | * @param string[] $sorted_columns |
|
| 239 | */ |
||
| 240 | 14 | public function sort(array $sorted_columns): self |
|
| 261 | 1 | ||
| 262 | /** |
||
| 263 | 6 | * Set column that must be excluded in getData() and getColumns(). |
|
| 264 | * |
||
| 265 | 6 | * @param string[] $excluded_columns column names to exclude |
|
| 266 | 6 | * @param bool $excluded whether to set exclude to true (default) or false (opposite: include) |
|
| 267 | 1 | */ |
|
| 268 | public function exclude(array $excluded_columns, bool $excluded = true): self |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Exclude all other columns that the one specified |
||
| 280 | * Column sort is preserved in getData(). |
||
| 281 | * |
||
| 282 | * @throws Exception\InvalidArgumentException |
||
| 283 | * |
||
| 284 | * @param string[] $include_only_columns |
||
| 285 | * @param bool $sort automatically apply sortColumns |
||
| 286 | * @param bool $preserve_excluded preserve excluded columns |
||
| 287 | */ |
||
| 288 | 7 | public function includeOnly(array $include_only_columns, bool $sort = true, bool $preserve_excluded = true): self |
|
| 312 | |||
| 313 | 3 | /** |
|
| 314 | * Return columns. |
||
| 315 | 3 | */ |
|
| 316 | 3 | public function getColumns($include_excluded_columns = false): ArrayObject |
|
| 327 | |||
| 328 | /** |
||
| 329 | 2 | * Set formatter to specific columns. |
|
| 330 | 2 | * |
|
| 331 | * @throws Exception\InvalidArgumentException |
||
| 332 | 2 | * |
|
| 333 | 2 | * @param FormatterInterface $formatter |
|
| 334 | * @param string[] $columns |
||
| 335 | */ |
||
| 336 | 2 | public function setFormatter(FormatterInterface $formatter, array $columns): self |
|
| 342 | |||
| 343 | public function search(): Search |
||
| 351 | |||
| 352 | 19 | public function setMetatadata(ColumnsMetadata $metadata): self |
|
| 358 | |||
| 359 | 19 | public function getMetadata(): ?ArrayObject |
|
| 363 | } |
||
| 364 |