Complex classes like DataGrid 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 DataGrid, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 12 | class DataGrid implements \Countable, \IteratorAggregate, \ArrayAccess  | 
            ||
| 13 | { | 
            ||
| 14 | use EventManagerAwareTrait;  | 
            ||
| 15 | |||
| 16 | const EVENT_GRID_INSERT_PRE = 'at-datagrid.grid.insert.pre';  | 
            ||
| 17 | const EVENT_GRID_INSERT_POST = 'at-datagrid.grid.insert.post';  | 
            ||
| 18 | |||
| 19 | const EVENT_GRID_UPDATE_PRE = 'at-datagrid.grid.update.pre';  | 
            ||
| 20 | const EVENT_GRID_UPDATE_POST = 'at-datagrid.grid.update.post';  | 
            ||
| 21 | |||
| 22 | const EVENT_GRID_PERSIST_PRE = 'at-datagrid.grid.persist.pre';  | 
            ||
| 23 | const EVENT_GRID_PERSIST_POST = 'at-datagrid.grid.persist.post';  | 
            ||
| 24 | |||
| 25 | const EVENT_GRID_DELETE_PRE = 'at-datagrid.grid.delete.pre';  | 
            ||
| 26 | const EVENT_GRID_DELETE_POST = 'at-datagrid.grid.delete.post';  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Grid title  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var string  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $title;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * Data source  | 
            ||
| 37 | *  | 
            ||
| 38 | * @var DataSource\AbstractDataSource  | 
            ||
| 39 | */  | 
            ||
| 40 | protected $dataSource;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * Data grid columns  | 
            ||
| 44 | *  | 
            ||
| 45 | * @var array  | 
            ||
| 46 | */  | 
            ||
| 47 | protected $columns = [];  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * @var string  | 
            ||
| 51 | */  | 
            ||
| 52 | protected $identifierColumnName = 'id';  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * @var Paginator  | 
            ||
| 56 | */  | 
            ||
| 57 | protected $paginator;  | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * Order in format ['id' => 'desc']  | 
            ||
| 61 | *  | 
            ||
| 62 | * @var array  | 
            ||
| 63 | */  | 
            ||
| 64 | protected $order;  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * Current page  | 
            ||
| 68 | *  | 
            ||
| 69 | * @var integer  | 
            ||
| 70 | */  | 
            ||
| 71 | protected $currentPage = 1;  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * Items per page  | 
            ||
| 75 | *  | 
            ||
| 76 | * @var integer  | 
            ||
| 77 | */  | 
            ||
| 78 | protected $itemsPerPage = 20;  | 
            ||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * Page range  | 
            ||
| 82 | *  | 
            ||
| 83 | * @var integer  | 
            ||
| 84 | */  | 
            ||
| 85 | protected $pageRange = 10;  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * Array of column filters  | 
            ||
| 89 | *  | 
            ||
| 90 | * @var array  | 
            ||
| 91 | */  | 
            ||
| 92 | protected $filters = [];  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * Array of rows from data source  | 
            ||
| 96 | *  | 
            ||
| 97 | * @var array  | 
            ||
| 98 | */  | 
            ||
| 99 | protected $data = [];  | 
            ||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | * @param $dataSource  | 
            ||
| 103 | * @param string $title  | 
            ||
| 104 | */  | 
            ||
| 105 | public function __construct($dataSource, $title = '')  | 
            ||
| 113 | |||
| 114 | // METADATA  | 
            ||
| 115 | |||
| 116 | /**  | 
            ||
| 117 | * @param $title  | 
            ||
| 118 | * @return $this  | 
            ||
| 119 | */  | 
            ||
| 120 | public function setTitle($title)  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * @return string  | 
            ||
| 128 | */  | 
            ||
| 129 | public function getTitle()  | 
            ||
| 133 | |||
| 134 | // COLUMNS  | 
            ||
| 135 | |||
| 136 | /**  | 
            ||
| 137 | * @param $name  | 
            ||
| 138 | * @return $this  | 
            ||
| 139 | */  | 
            ||
| 140 | public function setIdentifierColumnName($name)  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * @return string  | 
            ||
| 150 | */  | 
            ||
| 151 | public function getIdentifierColumnName()  | 
            ||
| 155 | |||
| 156 | /**  | 
            ||
| 157 | * Check if grid has column by the given name  | 
            ||
| 158 | *  | 
            ||
| 159 | * @param $name  | 
            ||
| 160 | * @return bool  | 
            ||
| 161 | */  | 
            ||
| 162 | protected function hasColumn($name)  | 
            ||
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * Add a column to data grid  | 
            ||
| 169 | *  | 
            ||
| 170 | * @param Column $column  | 
            ||
| 171 | * @return $this  | 
            ||
| 172 | */  | 
            ||
| 173 | public function addColumn(Column $column)  | 
            ||
| 183 | |||
| 184 | /**  | 
            ||
| 185 | * Set column by given name with overwriting.  | 
            ||
| 186 | *  | 
            ||
| 187 | * @param Column $column  | 
            ||
| 188 | * @return $this  | 
            ||
| 189 | */  | 
            ||
| 190 | public function setColumn(Column $column)  | 
            ||
| 195 | |||
| 196 | /**  | 
            ||
| 197 | * Return column object specified by it name  | 
            ||
| 198 | *  | 
            ||
| 199 | * @param $name  | 
            ||
| 200 | * @return Column  | 
            ||
| 201 | * @throws \Exception  | 
            ||
| 202 | */  | 
            ||
| 203 | public function getColumn($name)  | 
            ||
| 211 | |||
| 212 | /**  | 
            ||
| 213 | * Return all column objects  | 
            ||
| 214 | *  | 
            ||
| 215 | * @return array  | 
            ||
| 216 | */  | 
            ||
| 217 | public function getColumns()  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * Remove column specified by it name  | 
            ||
| 224 | *  | 
            ||
| 225 | * @param $name  | 
            ||
| 226 | * @return DataGrid  | 
            ||
| 227 | */  | 
            ||
| 228 | public function removeColumn($name)  | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Remove columns specified by its names  | 
            ||
| 239 | *  | 
            ||
| 240 | * @param array $names  | 
            ||
| 241 | * @return DataGrid  | 
            ||
| 242 | */  | 
            ||
| 243 | public function removeColumns(array $names)  | 
            ||
| 251 | |||
| 252 | /**  | 
            ||
| 253 | * Set columns invisible in grid  | 
            ||
| 254 | *  | 
            ||
| 255 | * @param array $names  | 
            ||
| 256 | * @return DataGrid  | 
            ||
| 257 | */  | 
            ||
| 258 | public function hideColumns(array $names)  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Set columns invisible in form  | 
            ||
| 269 | *  | 
            ||
| 270 | * @param $names  | 
            ||
| 271 | * @return DataGrid  | 
            ||
| 272 | */  | 
            ||
| 273 | public function hideColumnsInForm(array $names)  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * Set columns visible in grid  | 
            ||
| 284 | *  | 
            ||
| 285 | * @param array $names  | 
            ||
| 286 | * @return DataGrid  | 
            ||
| 287 | */  | 
            ||
| 288 | public function showColumns(array $names)  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * Set columns visible in form  | 
            ||
| 299 | *  | 
            ||
| 300 | * @param $names  | 
            ||
| 301 | * @return DataGrid  | 
            ||
| 302 | */  | 
            ||
| 303 | public function showColumnsInForm(array $names)  | 
            ||
| 311 | |||
| 312 | // SORTING  | 
            ||
| 313 | |||
| 314 | /**  | 
            ||
| 315 | * @param array $order  | 
            ||
| 316 | */  | 
            ||
| 317 | public function setOrder(array $order)  | 
            ||
| 325 | |||
| 326 | /**  | 
            ||
| 327 | * @return array  | 
            ||
| 328 | */  | 
            ||
| 329 | public function getOrder()  | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * @return mixed  | 
            ||
| 336 | */  | 
            ||
| 337 | public function getOrderColumn()  | 
            ||
| 345 | |||
| 346 | /**  | 
            ||
| 347 | * @return mixed  | 
            ||
| 348 | */  | 
            ||
| 349 | public function getOrderDirection()  | 
            ||
| 357 | |||
| 358 | /**  | 
            ||
| 359 | * @return string  | 
            ||
| 360 | */  | 
            ||
| 361 | public function getRevertOrderDirection()  | 
            ||
| 365 | |||
| 366 | // DATA SOURCE  | 
            ||
| 367 | |||
| 368 | /**  | 
            ||
| 369 | * @param DataSource\AbstractDataSource $dataSource  | 
            ||
| 370 | * @return $this  | 
            ||
| 371 | */  | 
            ||
| 372 | public function setDataSource(DataSource\AbstractDataSource $dataSource)  | 
            ||
| 377 | |||
| 378 | /**  | 
            ||
| 379 | * @return DataSource\AbstractDataSource  | 
            ||
| 380 | */  | 
            ||
| 381 | public function getDataSource()  | 
            ||
| 385 | |||
| 386 | /**  | 
            ||
| 387 | * Find row by primary key  | 
            ||
| 388 | *  | 
            ||
| 389 | * @param $key  | 
            ||
| 390 | * @return mixed  | 
            ||
| 391 | */  | 
            ||
| 392 | public function getRow($key)  | 
            ||
| 396 | |||
| 397 | /**  | 
            ||
| 398 | * @return mixed  | 
            ||
| 399 | * @throws \Exception  | 
            ||
| 400 | */  | 
            ||
| 401 | public function getData()  | 
            ||
| 427 | |||
| 428 | /**  | 
            ||
| 429 | * @return Paginator  | 
            ||
| 430 | */  | 
            ||
| 431 | public function getPaginator()  | 
            ||
| 435 | |||
| 436 | // CRUD  | 
            ||
| 437 | |||
| 438 | /**  | 
            ||
| 439 | * @param $data  | 
            ||
| 440 | * @return mixed  | 
            ||
| 441 | */  | 
            ||
| 442 | public function save($data, $identifier = null)  | 
            ||
| 461 | |||
| 462 | /**  | 
            ||
| 463 | * @param $data  | 
            ||
| 464 | * @return mixed  | 
            ||
| 465 | */  | 
            ||
| 466 | public function insert($data)  | 
            ||
| 480 | |||
| 481 | /**  | 
            ||
| 482 | * @param $data  | 
            ||
| 483 | * @param $primary  | 
            ||
| 484 | * @return mixed  | 
            ||
| 485 | */  | 
            ||
| 486 | public function update($data, $primary)  | 
            ||
| 500 | |||
| 501 | /**  | 
            ||
| 502 | * @param $id  | 
            ||
| 503 | */  | 
            ||
| 504 | public function delete($id)  | 
            ||
| 512 | |||
| 513 | // FILTERS  | 
            ||
| 514 | |||
| 515 | /**  | 
            ||
| 516 | * @param FilterInterface $filter  | 
            ||
| 517 | * @param Column $column  | 
            ||
| 518 | * @return $this  | 
            ||
| 519 | */  | 
            ||
| 520 | public function addFilter(FilterInterface $filter, Column $column)  | 
            ||
| 534 | |||
| 535 | /**  | 
            ||
| 536 | * @param $columnName  | 
            ||
| 537 | * @return bool  | 
            ||
| 538 | */  | 
            ||
| 539 | public function hasFilter($columnName)  | 
            ||
| 543 | |||
| 544 | /**  | 
            ||
| 545 | * @param $columnName  | 
            ||
| 546 | * @return null  | 
            ||
| 547 | */  | 
            ||
| 548 | public function getFilter($columnName)  | 
            ||
| 556 | |||
| 557 | /**  | 
            ||
| 558 | * @return array  | 
            ||
| 559 | */  | 
            ||
| 560 | public function getFilters()  | 
            ||
| 564 | |||
| 565 | /**  | 
            ||
| 566 | * @return bool  | 
            ||
| 567 | */  | 
            ||
| 568 | public function hasFilters()  | 
            ||
| 572 | |||
| 573 | /**  | 
            ||
| 574 | * @param $values  | 
            ||
| 575 | */  | 
            ||
| 576 | public function setFiltersData($values)  | 
            ||
| 583 | |||
| 584 | // PAGINATOR  | 
            ||
| 585 | |||
| 586 | /**  | 
            ||
| 587 | * @param $number  | 
            ||
| 588 | * @return $this  | 
            ||
| 589 | */  | 
            ||
| 590 | public function setCurrentPage($number)  | 
            ||
| 595 | |||
| 596 | /**  | 
            ||
| 597 | * @return int  | 
            ||
| 598 | */  | 
            ||
| 599 | public function getCurrentPage()  | 
            ||
| 603 | |||
| 604 | |||
| 605 | /**  | 
            ||
| 606 | * @param $count  | 
            ||
| 607 | * @return $this  | 
            ||
| 608 | */  | 
            ||
| 609 | public function setItemsPerPage($count)  | 
            ||
| 614 | |||
| 615 | /**  | 
            ||
| 616 | * @return int  | 
            ||
| 617 | */  | 
            ||
| 618 | public function getItemsPerPage()  | 
            ||
| 622 | |||
| 623 | /**  | 
            ||
| 624 | * @param $count  | 
            ||
| 625 | * @return $this  | 
            ||
| 626 | */  | 
            ||
| 627 | public function setPageRange($count)  | 
            ||
| 632 | |||
| 633 | // INTERFACES IMPLEMENTATION  | 
            ||
| 634 | |||
| 635 | /**  | 
            ||
| 636 | * @return mixed  | 
            ||
| 637 | */  | 
            ||
| 638 | public function current()  | 
            ||
| 642 | |||
| 643 | /**  | 
            ||
| 644 | * @return bool  | 
            ||
| 645 | */  | 
            ||
| 646 | public function valid()  | 
            ||
| 650 | |||
| 651 | /**  | 
            ||
| 652 | * @return mixed  | 
            ||
| 653 | */  | 
            ||
| 654 | public function next()  | 
            ||
| 658 | |||
| 659 | /**  | 
            ||
| 660 | * @return mixed  | 
            ||
| 661 | */  | 
            ||
| 662 | public function key()  | 
            ||
| 666 | |||
| 667 | /**  | 
            ||
| 668 | * @return mixed  | 
            ||
| 669 | */  | 
            ||
| 670 | public function rewind()  | 
            ||
| 674 | |||
| 675 | /**  | 
            ||
| 676 | * @return int  | 
            ||
| 677 | */  | 
            ||
| 678 | public function count()  | 
            ||
| 682 | |||
| 683 | /**  | 
            ||
| 684 | * @param mixed $offset  | 
            ||
| 685 | * @return bool  | 
            ||
| 686 | */  | 
            ||
| 687 | public function offsetExists($offset)  | 
            ||
| 691 | |||
| 692 | /**  | 
            ||
| 693 | * @param mixed $offset  | 
            ||
| 694 | * @return bool|mixed  | 
            ||
| 695 | */  | 
            ||
| 696 | public function offsetGet($offset)  | 
            ||
| 704 | |||
| 705 | /**  | 
            ||
| 706 | * @param mixed $offset  | 
            ||
| 707 | * @param mixed $column  | 
            ||
| 708 | */  | 
            ||
| 709 | public function offsetSet($offset, $column)  | 
            ||
| 717 | |||
| 718 | /**  | 
            ||
| 719 | * @param mixed $offset  | 
            ||
| 720 | */  | 
            ||
| 721 | public function offsetUnset($offset)  | 
            ||
| 727 | |||
| 728 | /**  | 
            ||
| 729 | * Get an iterator for iterating over the elements in the collection.  | 
            ||
| 730 | *  | 
            ||
| 731 | * @return \ArrayIterator|\Traversable  | 
            ||
| 732 | */  | 
            ||
| 733 | public function getIterator()  | 
            ||
| 737 | }  | 
            
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..