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 |
||
12 | class Manager |
||
13 | { |
||
14 | /** |
||
15 | * @var DataGrid |
||
16 | */ |
||
17 | protected $grid; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $rawData; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $data; |
||
28 | |||
29 | /** |
||
30 | * @var RendererInterface |
||
31 | */ |
||
32 | protected $renderer; |
||
33 | |||
34 | /** |
||
35 | * @var FormBuilder |
||
36 | */ |
||
37 | protected $formBuilder; |
||
38 | |||
39 | /** |
||
40 | * @var Form |
||
41 | */ |
||
42 | protected $filtersForm; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $allowCreate = true; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $allowDelete = true; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $allowEdit = true; |
||
58 | |||
59 | /** |
||
60 | * Row actions |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $actions = []; |
||
65 | |||
66 | /** |
||
67 | * @param DataGrid $grid |
||
68 | */ |
||
69 | public function __construct(DataGrid $grid) |
||
73 | |||
74 | /** |
||
75 | * @return DataGrid |
||
76 | */ |
||
77 | public function getGrid() |
||
81 | |||
82 | /** |
||
83 | * @param RendererInterface $renderer |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function setRenderer(RendererInterface $renderer) |
||
91 | |||
92 | /** |
||
93 | * @return RendererInterface |
||
94 | */ |
||
95 | public function getRenderer() |
||
99 | |||
100 | /** |
||
101 | * @param bool $flag |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function setAllowCreate($flag = true) |
||
109 | |||
110 | /** |
||
111 | * Alias for setAllowCreate |
||
112 | * |
||
113 | * @param bool $flag |
||
114 | * @return DataGrid |
||
115 | */ |
||
116 | public function allowCreate($flag = true) |
||
121 | |||
122 | /** |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function isAllowCreate() |
||
129 | |||
130 | /** |
||
131 | * @param bool $flag |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function setAllowDelete($flag = true) |
||
139 | |||
140 | /** |
||
141 | * Alias for setAllowDelete |
||
142 | * |
||
143 | * @param bool $flag |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function allowDelete($flag = true) |
||
151 | |||
152 | /** |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function isAllowDelete() |
||
159 | |||
160 | /** |
||
161 | * @param bool $flag |
||
162 | * @return $this |
||
163 | */ |
||
164 | public function setAllowEdit($flag = true) |
||
169 | |||
170 | /** |
||
171 | * Alias for setAllowEdit |
||
172 | * |
||
173 | * @param bool $flag |
||
174 | * @return DataGrid |
||
175 | */ |
||
176 | public function allowEdit($flag = true) |
||
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function isAllowEdit() |
||
189 | |||
190 | /** |
||
191 | * @return Form |
||
192 | */ |
||
193 | public function getFiltersForm() |
||
201 | |||
202 | /** |
||
203 | * @return FormBuilder |
||
204 | */ |
||
205 | public function getFormBuilder() |
||
213 | |||
214 | /** |
||
215 | * @param FormBuilder $formBuilder |
||
216 | */ |
||
217 | public function setFormBuilder($formBuilder) |
||
221 | |||
222 | /** |
||
223 | * @return Form |
||
224 | */ |
||
225 | protected function buildFiltersForm() |
||
254 | |||
255 | /** |
||
256 | * @param Action $action |
||
257 | * @return $this |
||
258 | * @throws \Exception |
||
259 | */ |
||
260 | public function addAction(Action $action) |
||
265 | |||
266 | /** |
||
267 | * @param array $actions |
||
268 | * @return $this |
||
269 | */ |
||
270 | public function addActions(array $actions = []) |
||
278 | |||
279 | /** |
||
280 | * @param $name |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function removeAction($name) |
||
291 | |||
292 | /** |
||
293 | * @param $name |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function getAction($name) |
||
304 | |||
305 | /** |
||
306 | * @return array |
||
307 | */ |
||
308 | public function getActions() |
||
312 | |||
313 | /** |
||
314 | * @return array |
||
315 | */ |
||
316 | public function getBulkActions() |
||
327 | |||
328 | |||
329 | public function getRawData() |
||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | * @throws \Exception |
||
337 | */ |
||
338 | public function getData() |
||
374 | |||
375 | /** |
||
376 | * @return mixed |
||
377 | */ |
||
378 | public function render() |
||
384 | } |
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..