Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Manager |
||
| 14 | {
|
||
| 15 | /** |
||
| 16 | * @var DataGrid |
||
| 17 | */ |
||
| 18 | protected $grid; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $rawData; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $data; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var RendererInterface |
||
| 32 | */ |
||
| 33 | protected $renderer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var FormBuilder |
||
| 37 | */ |
||
| 38 | protected $formBuilder; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Form |
||
| 42 | */ |
||
| 43 | protected $filtersForm; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var StorageInterface |
||
| 47 | */ |
||
| 48 | protected $cache; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $allowCreate = true; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $allowDelete = true; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $allowEdit = true; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Row actions |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $actions = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param DataGrid $grid |
||
| 74 | */ |
||
| 75 | public function __construct(DataGrid $grid) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return DataGrid |
||
| 82 | */ |
||
| 83 | public function getGrid() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param RendererInterface $renderer |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function setRenderer(RendererInterface $renderer) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return RendererInterface |
||
| 100 | */ |
||
| 101 | public function getRenderer() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param StorageInterface $cache |
||
| 108 | */ |
||
| 109 | public function setCache(StorageInterface $cache) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return StorageInterface |
||
| 116 | */ |
||
| 117 | public function getCache() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param bool $flag |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function setAllowCreate($flag = true) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Alias for setAllowCreate |
||
| 134 | * |
||
| 135 | * @param bool $flag |
||
| 136 | * @return DataGrid |
||
| 137 | */ |
||
| 138 | public function allowCreate($flag = true) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function isAllowCreate() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param bool $flag |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function setAllowDelete($flag = true) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Alias for setAllowDelete |
||
| 164 | * |
||
| 165 | * @param bool $flag |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function allowDelete($flag = true) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function isAllowDelete() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param bool $flag |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function setAllowEdit($flag = true) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Alias for setAllowEdit |
||
| 194 | * |
||
| 195 | * @param bool $flag |
||
| 196 | * @return DataGrid |
||
| 197 | */ |
||
| 198 | public function allowEdit($flag = true) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isAllowEdit() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return Form |
||
| 214 | */ |
||
| 215 | public function getFiltersForm() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return FormBuilder |
||
| 226 | */ |
||
| 227 | public function getFormBuilder() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param FormBuilder $formBuilder |
||
| 238 | */ |
||
| 239 | public function setFormBuilder($formBuilder) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return Form |
||
| 246 | */ |
||
| 247 | protected function buildFiltersForm() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param Action $action |
||
| 279 | * @return $this |
||
| 280 | * @throws \Exception |
||
| 281 | */ |
||
| 282 | public function addAction(Action $action) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param array $actions |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function addActions(array $actions = []) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param $name |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | public function removeAction($name) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $name |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function getAction($name) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function getActions() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function getBulkActions() |
||
| 349 | |||
| 350 | |||
| 351 | public function getRawData() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return array |
||
| 358 | * @throws \Exception |
||
| 359 | */ |
||
| 360 | public function getData() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return mixed |
||
| 399 | */ |
||
| 400 | public function render() |
||
| 406 | } |
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..